本文转载自:https://blog.csdn.net/qq_37023928/article/details/116777630
RSA
加密简介
RSA
加密是一种 非对称加密
。可以在不直接传递密钥的情况下,完成解密。这能够确保信息的安全性,避免了直接传递密钥所造成的被破解的风险。是由一对密钥来进行加解密的过程,分别称为公钥和私钥。两者之间有数学相关,该加密算法的原理就是对一极大整数做因数分解的困难性来保证安全性。通常个人保存私钥,公钥是公开的(可能同时多人持有)
RSA
加密、解密流程
通过 RSA
实现用户密码加密传输,核心思路
- 点击登录,先请求后端,生成一对公私钥,将公钥返回给前台
- 前台使用开源的
jsencrypt.js
对密码进行加密,加密后传输到后台 - 后台对加密的密码进行解密
示例
使用的是 sprngboot + thymeleaf
模板进行整合的
前端
获取开源的 js
文件:https://github.com/travist/jsencrypt/tree/master/bin,文件名称为:jsencrypt.js
获取到开源文件后,我们把它放在:static/js/
,创建页面 templates/login.html
文件
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="js/jsencrypt.js"></script>
<body>
<h1 th:text="${msg}">RSA测试</h1>
<form>
用户账号:
<input type="text" name="username" id="username">
<br>
用户密码:
<input type="text" name="password" id="password">
<br>
<input type="button" th:onclick="login()" th:value="提交">
</form>
</body>
<script>
function login() {
var username = $('#username').val();
var password = $('#password').val();
var encrypt = new JSEncrypt();
$.ajax({
type: "get", // 提交方式
url: "/getPublicKey",// 访问路径
contentType: 'application/json;charset=utf-8',//返回json结果
success: function (data) {
console.log(data)
encrypt.setPublicKey(data)
var encryptPwd = encrypt.encrypt(password)
console.log("encryptPwd:"+encryptPwd)
$.ajax({
type: "post", //提交方式
url: "/loginRequest",//访问路径
contentType: 'application/json;charset=utf-8',//返回json结果
data: JSON.stringify({
"username":username,"password":encryptPwd}),
success: function (data) {
console.log(data)
}
});
}
});
}
</script>
</html>
后端
RSA
工具类
/**
* 备注,解密前台公钥加密的数据,请调用decryptWithPrivate方法
* 每次重启之后,都会生成一个一对新的公私钥
*/
public class RSAUtil {
//秘钥大小
private static final int KEY_SIZE = 1024;
//后续放到常量类中去
public static final String PRIVATE_KEY = "privateKey";
public static final String PUBLIC_KEY = "publicKey";
private static KeyPair keyPair;
private static Map<String,String> rsaMap;
//生成RSA,并存放
static {
try {
Provider provider =new org.bouncycastle.jce.provider.BouncyCastleProvider();
Security.addProvider(provider);
SecureRandom random = new SecureRandom();
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", provider);
generator.initialize(KEY_SIZE,random);
keyPair = generator.generateKeyPair();
//将公钥和私钥存放,登录时会不断请求获取公钥,我们可以将其放到缓存中,而不放入数据库了
//我在想,这个是不是有必要存放到Redis,在分布式场景中?
//貌似有些必要,万一获取到的pubkey是server1中的,拿着server1的pubkey去server2去解密?
storeRSA();
} catch(NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
/**
* 将RSA存入缓存
*/
private static void storeRSA() {
rsaMap = new HashMap<>();
PublicKey publicKey = keyPair.getPublic();
String publicKeyStr = new String(Base64.encodeBase64(publicKey.getEncoded()));
rsaMap.put(PRIVATE_KEY, publicKeyStr);
PrivateKey privateKey = keyPair.getPrivate();
String privateKeyStr = new String(Base64.encodeBase64(privateKey.getEncoded()));
rsaMap.put(PUBLIC_KEY, privateKeyStr);
}
/**
* 私钥解密(解密前台公钥加密的密文)
*
* @param encryptText 公钥加密的数据
* @return 私钥解密出来的数据
* @throws Exception e
*/
public static String decryptWithPrivate(String encryptText) throws Exception {
if(StringUtils.isBlank(encryptText)){
return null;
}
byte[] en_byte = Base64.decodeBase64(encryptText.getBytes());
//byte[] en_byte = Hex.decode(encryptText);
Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
Security.addProvider(provider);
//之前我只写了RSA,出现了乱码+明文 参考:https://blog.csdn.net/qq_39420411/article/details/94056654
Cipher ci = Cipher.getInstance("RSA/ECB/PKCS1Padding", provider);
PrivateKey privateKey = keyPair.getPrivate();
ci.init(Cipher.DECRYPT_MODE, privateKey);
byte[] res = ci.doFinal(en_byte);
return new String(res);
}
/**
* java端 使用公钥加密(此方法暂时用不到)
* @param plaintext 明文内容
* @return byte[]
* @throws UnsupportedEncodingException e
*/
public static byte[] encrypt(String plaintext) throws UnsupportedEncodingException {
String encode = URLEncoder.encode(plaintext, "utf-8");
RSAPublicKey rsaPublicKey = (RSAPublicKey)keyPair.getPublic();
//获取公钥指数
BigInteger e = rsaPublicKey.getPublicExponent();
//获取公钥系数
BigInteger n = rsaPublicKey.getModulus();
//获取明文字节数组
BigInteger m = new BigInteger(encode.getBytes());
//进行明文加密
BigInteger res = m.modPow(e, n);
return res.toByteArray();
}
/**
* java端 使用私钥解密(此方法暂时用不到)
* @param cipherText 加密后的字节数组
* @return 解密后的数据
* @throws UnsupportedEncodingException e
*/
public static String decrypt(byte[] cipherText) throws UnsupportedEncodingException {
RSAPrivateKey prk = (RSAPrivateKey) keyPair.getPrivate();
// 获取私钥参数-指数/系数
BigInteger d = prk.getPrivateExponent();
BigInteger n = prk.getModulus();
// 读取密文
BigInteger c = new BigInteger(cipherText);
// 进行解密
BigInteger m = c.modPow(d, n);
// 解密结果-字节数组
byte[] mt = m.toByteArray();
//转成String,此时是乱码
String en = new String(mt);
//再进行编码,最后返回解密后得到的明文
return URLDecoder.decode(en, "UTF-8");
}
/**
* 获取公钥
* @return 公钥
*/
public static String getPublicKey(){
return rsaMap.get(PUBLIC_KEY);
}
/**
* 获取私钥
* @return 私钥
*/
public static String getPrivateKey(){
return rsaMap.get(PRIVATE_KEY);
}
public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println(RSAUtil.getPrivateKey());
System.out.println(RSAUtil.getPublicKey());
byte[] usernames = RSAUtil.encrypt("username66");
System.out.println(RSAUtil.decrypt(usernames));
}
}
controller
@RestController
public class RSAController {
@RequestMapping("/getPublicKey")
public String getPublicKey(){
return RSAUtil.getPublicKey();
}
}
@Controller
public class LoginController {
@RequestMapping("/login")
public String login(Model model){
model.addAttribute("msg", "RSA前端加密,后端解密测试");
return "login";
}
@RequestMapping(value = "/loginRequest",method= RequestMethod.POST)
@ResponseBody
public String loginRequest(@RequestBody JSONObject json){
String username = json.getString("username");
String password = json.getString("password");
System.out.println(username);
System.out.println(password);
String res = null;
try {
// 这里就是解密后的密码了
res = RSAUtil.decryptWithPrivate(password);
System.out.println(res);
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
}