RSA是非对称加密,所以需要生成一对密钥对(公钥+私钥:公钥加密,私钥解密)
整个过程可以简述为:前端通过请求,拿到公钥,在前端将敏感信息(如密码等)进行加密,然后将密文传输到后端,后端通过私钥对密文进行解密,拿到明文的信息。
上代码:
1:RSA工具类(密钥对生成、通过私钥对密文解密)
public class RSASecurityUtils {
public static final String UTF_8 = "UTF-8";
public static final String RSA_ALGORITHM_NO_PADDING = "RSA";
private static final String PUBLIC_KEY = "RSAPublicKey";
private static final String PRIVATE_KEY = "RSAPrivateKey";
/**
* 密文解密
* @param privateKeyStr
* @param data
* @return
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws UnsupportedEncodingException
*/
public static String decrypt(String privateKeyStr, String data) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM_NO_PADDING);
byte[] privateKeyArray = privateKeyStr.getBytes();
byte[] dataArray = data.getBytes();
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyArray));
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM_NO_PADDING);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return new String(cipher.doFinal(Base64.decodeBase64(dataArray)), UTF_8);
}
//获得公钥
public static String getPublicKey(Map<String, Object> keyMap) throws Exception {
//获得map中的公钥对象 转为key对象
Key key = (Key) keyMap.get(PUBLIC_KEY);
//byte[] publicKey = key.getEncoded();
//编码返回字符串
return encryptBASE64(key.getEncoded());
}
//获得私钥
public static String getPrivateKey(Map<String, Object> keyMap) throws Exception {
//获得map中的私钥对象 转为key对象
Key key = (Key) keyMap.get(PRIVATE_KEY);
//byte[] privateKey = key.getEncoded();
//编码返回字符串
return encryptBASE64(key.getEncoded());
}
//解码返回byte
public static byte[] decryptBASE64(String key) throws Exception {
return (new BASE64Decoder()).decodeBuffer(key);
}
//编码返回字符串
public static String encryptBASE64(byte[] key) throws Exception {
return (new BASE64Encoder()).encodeBuffer(key);
}
//map对象中存放公私钥
public static Map<String, Object> initKey() throws Exception {
//获得对象 KeyPairGenerator 参数 RSA 1024个字节
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(RSA_ALGORITHM_NO_PADDING);
keyPairGen.initialize(1024);
//通过对象 KeyPairGenerator 获取对象KeyPair
KeyPair keyPair = keyPairGen.generateKeyPair();
//通过对象 KeyPair 获取RSA公私钥对象RSAPublicKey RSAPrivateKey
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
//公私钥对象存入map中
Map<String, Object> keyMap = new HashMap<String, Object>(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
}
public static void main(String[] args) {
Map<String, Object> keyMap;
try {
keyMap = initKey();
String publicKey = getPublicKey(keyMap);
System.out.println(publicKey);
String privateKey = getPrivateKey(keyMap);
System.out.println(privateKey);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2:解密工具类调用
//privateKey私钥,encryptPassword加密后的密文信息
String decryptPassword = RSASecurityUtils.decrypt(privateKey, encryptPassword);
到这里后端的RSA就已经处理完成了,前端需要做的就是将敏感信息进行加密然后传输到后端即可,前端处理可以参考:
注:其中的公钥以及password根据实际情况设定,这里只是示例,需要引入的js可自己从网上找资源
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="t"></div>
<script src="./js/jquery-3.3.1.min.js"></script>
<script src="./js/jsencrypt.js"></script>
<script>
generateEntype();
function generateEntype() {
let password = "XXXX";
let publicKey = "XXXX";
let encrypt = new JSEncrypt();
encrypt.setPublicKey(publicKey);
let encryptPwd = encrypt.encryptLong(password);
$("#t").text(encryptPwd);
}
</script>
</body>
</html>
到这里,RSA的内容就已经结束了,对明文进行加密入库继续往下看
后端拿到明文之后,是不能直接将这些明文直接进行入库的,这太不安全,到这里还需要对明文的密码在进行一次加密,然后入库。
可以参考密码盐的方式,通过随机生成一个密码盐,然后对明文密码进行加密,然后将盐和加密之后的密码进行入库,这里的解密也是通过密码盐对加密之后的密文进行解密。这里就不粘具体的方案了。