天天看點

java,jsencrypt 非對稱RSA加解密

1、前端jsencrypt加密

    var jsencrypt = new JSEncrypt();

    jsencrypt.setPublicKey(publicKey);

    jsencrypt.encrypt("pwd");//pwd為需要加密傳輸的字元串

2、背景java解密擷取

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;

import java.security.*;

import java.security.spec.PKCS8EncodedKeySpec;

import java.security.spec.X509EncodedKeySpec;

import java.util.HashMap;

import java.util.Map;

public class RSAUtils {

    public static final String KEY_ALGORITHM = "RSA";

    public static final String SIGNATURE_ALGORITHM = "MD5withRSA";

    private static final String PUBLIC_KEY = "RSAPublicKey";

    private static final String PRIVATE_KEY = "RSAPrivateKey";

    public static byte[] decryptBASE64(String key) {

        return Base64.decodeBase64(key);

    }

    public static String encryptBASE64(byte[] bytes) {

        return Base64.encodeBase64String(bytes);

    }

    public static String sign(byte[] data, String privateKey) throws Exception {

        // 解密由base64編碼的私鑰

        byte[] keyBytes = decryptBASE64(privateKey);

        // 構造PKCS8EncodedKeySpec對象

        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);

        // KEY_ALGORITHM 指定的加密算法

        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

        // 取私鑰匙對象

        PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);

        // 用私鑰對資訊生成數字簽名

        Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);

        signature.initSign(priKey);

        signature.update(data);

        return encryptBASE64(signature.sign());

    }

    public static boolean verify(byte[] data, String publicKey, String sign)

            throws Exception {

        // 解密由base64編碼的公鑰

        byte[] keyBytes = decryptBASE64(publicKey);

        // 構造X509EncodedKeySpec對象

        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);

        // KEY_ALGORITHM 指定的加密算法

        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

        // 取公鑰匙對象

        PublicKey pubKey = keyFactory.generatePublic(keySpec);

        Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);

        signature.initVerify(pubKey);

        signature.update(data);

        // 驗證簽名是否正常

        return signature.verify(decryptBASE64(sign));

    }

    public static byte[] decryptByPrivateKey(byte[] data, String key) throws Exception{

        // 對密鑰解密

        byte[] keyBytes = decryptBASE64(key);

        // 取得私鑰

        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);

        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

        Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

        // 對資料解密

        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        return cipher.doFinal(data);

    }

    public static byte[] decryptByPrivateKey(String data, String key)

            throws Exception {

        return decryptByPrivateKey(decryptBASE64(data),key);

    }

    public static byte[] decryptByPublicKey(byte[] data, String key)

            throws Exception {

        // 對密鑰解密

        byte[] keyBytes = decryptBASE64(key);

        // 取得公鑰

        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);

        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

        Key publicKey = keyFactory.generatePublic(x509KeySpec);

        // 對資料解密

        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

        cipher.init(Cipher.DECRYPT_MODE, publicKey);

        return cipher.doFinal(data);

    }

    public static byte[] encryptByPublicKey(String data, String key)

            throws Exception {

        // 對公鑰解密

        byte[] keyBytes = decryptBASE64(key);

        // 取得公鑰

        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);

        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

        Key publicKey = keyFactory.generatePublic(x509KeySpec);

        // 對資料加密

        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

        cipher.init(Cipher.ENCRYPT_MODE, publicKey);

        return cipher.doFinal(data.getBytes());

    }

    public static byte[] encryptByPrivateKey(byte[] data, String key)

            throws Exception {

        // 對密鑰解密

        byte[] keyBytes = decryptBASE64(key);

        // 取得私鑰

        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);

        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

        Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

        // 對資料加密

        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

        cipher.init(Cipher.ENCRYPT_MODE, privateKey);

        return cipher.doFinal(data);

    }

    public static String getPrivateKey(Map<String, Key> keyMap)

            throws Exception {

        Key key = (Key) keyMap.get(PRIVATE_KEY);

        return encryptBASE64(key.getEncoded());

    }

    public static String getPublicKey(Map<String, Key> keyMap)

            throws Exception {

        Key key = keyMap.get(PUBLIC_KEY);

        return encryptBASE64(key.getEncoded());

    }

    public static Map<String, Key> initKey() throws Exception {

        KeyPairGenerator keyPairGen = KeyPairGenerator

                .getInstance(KEY_ALGORITHM);

        keyPairGen.initialize(1024);

        KeyPair keyPair = keyPairGen.generateKeyPair();

        Map<String, Key> keyMap = new HashMap(2);

        keyMap.put(PUBLIC_KEY, keyPair.getPublic());// 公鑰

        keyMap.put(PRIVATE_KEY, keyPair.getPrivate());// 私鑰

        return keyMap;

    }

    public static void main(String[] args) throws Exception{

        Map<String, Key> keyMap = initKey();

        System.out.println("公鑰:" + getPublicKey(keyMap));

        System.out.println("私鑰:" + getPrivateKey(keyMap));

        String pwd = "pwd";

        System.out.println("加密後:");

        byte[] encryptData = encryptByPublicKey(pwd, getPublicKey(keyMap));//公鑰加密

        String encryptStr = encryptBASE64(encryptData);

        System.out.println(encryptStr);

        System.out.println("解密後:");

        byte[] decryptData = decryptByPrivateKey(decryptBASE64(encryptStr), getPrivateKey(keyMap));//私鑰解密

        System.out.println(new String(decryptData));

    }

}