SpringBoot通过RSA实现用户名和密码的加密和解密
前言
在我们输入用户名和密码时,在传输的过程中应以加密的方式去传递到后台,尤其是密码,避免在登录的过程中,打开浏览器的控制台,便能轻松取得密码。
一、RSA是什么?
RSA 加密是一种 非对称加密,可以在不直接传递密钥的情况下,完成解密。这能够确保信息的安全性,避免了直接传递密钥所造成的被破解的风险。是由一对密钥来进行加解密的过程,分别称为公钥和私钥。两者之间有数学相关,该加密算法的原理就是对一极大整数做因数分解的困难性来保证安全性。通常个人保存私钥,公钥是公开的(可能同时多人持有)。
通过 RSA
实现用户密码加密传输,核心思路
- 点击登录,先请求后端,生成一对公私钥,将公钥返回给前台
- 前台使用开源的
jsencrypt.js
对密码进行加密,加密后传输到后台 - 后台对加密的密码进行解密
二、使用步骤
使用的是 sprngboot + thymeleaf
模板进行整合的。
1.前端:
1.1创建前端页面:login.html,文件位置templates/login.html
代码如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="./js/jsencrypt.js"></script>
<body>
<h1 >RSA测试</h1>
<form>
用户账号:
<input type="text" name="username" id="username">
<br>
用户密码:
<input type="password" 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);
var encryptUsername = encrypt.encrypt(username);
console.log("encryptPwd:"+encryptPwd)
$.ajax({
type: "post", //提交方式
url: "/loginRequest",//访问路径
contentType: 'application/json;charset=utf-8',//返回json结果
data: JSON.stringify({"username":encryptUsername,"password":encryptPwd}),
headers: { "content-type": "application/json;charset=utf-8" },
success: function (data) {
console.log(data)
}
});
}
});
}
</script>
1.2 引入: jsencrypt.js 文件,位置:/resources/static/js/ jsencrypt.js,附上开源文件地址:获取开源的 js
文件:https://github.com/travist/jsencrypt/tree/master/bin。
2.后端
2.1 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;
private static org.bouncycastle.jce.provider.BouncyCastleProvider bouncyCastleProvider = null;
//BouncyCastleProvider内的方法都为静态方法,GC不会回收
public static synchronized org.bouncycastle.jce.provider.BouncyCastleProvider getInstance() {
if (bouncyCastleProvider == null) {
bouncyCastleProvider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
}
return bouncyCastleProvider;
}
//生成RSA,并存放
static {
try {
//通过以下方法,将每次New一个BouncyCastleProvider,可能导致的内存泄漏
/* Provider provider =new org.bouncycastle.jce.provider.BouncyCastleProvider();
Security.addProvider(provider);
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", provider);*/
//解决方案
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", getInstance());
SecureRandom random = new SecureRandom();
generator.initialize(KEY_SIZE, random);
keyPair = generator.generateKeyPair();
//将公钥和私钥存放,登录时会不断请求获取公钥
//建议放到redis的缓存中,避免在分布式场景中,出现拿着server1的公钥去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(PUBLIC_KEY, publicKeyStr);
PrivateKey privateKey = keyPair.getPrivate();
String privateKeyStr = new String(Base64.encodeBase64(privateKey.getEncoded()));
rsaMap.put(PRIVATE_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());
//可能导致内存泄漏问题
/* Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
Security.addProvider(provider);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", provider);*/
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", getInstance());
PrivateKey privateKey = keyPair.getPrivate();
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] res = cipher.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("username");
System.out.println(RSAUtil.decrypt(usernames));
}
}
2.2 获取公钥的RSAController类
@RestController
public class RSAController {
@RequestMapping("/getPublicKey")
public String getPublicKey(){
return RSAUtil.getPublicKey();
}
}
2.3 登录请求的处理接口:
@Controller
public class LoginController {
//将Service注入Web层
@Autowired
UserService userService;
/*读取application.yml中的配置参数,在RSAUtil中我并没有按照此方法去存储PRIVATE_KEY和PUBLIC_KEY,建议将之写至配置文件中。*/
@Value("${RSA.privateKey}")
private String privateKey;
@Value("${RSA.publicKey}")
private String publicKey;
@RequestMapping("/login")
public String login() {
return "login";
}
/**
* 请求参数存放于request Payload中
*
* @param req
* @return
*/
@RequestMapping(value = "/loginRequest", method = RequestMethod.POST)
@ResponseBody
public String loginRequest(HttpServletRequest req) {
//将request Payload中的参数转化为JSONObject
StringBuilder stringBuilder = new StringBuilder();
try (BufferedReader reader = req.getReader();) {
char[] buff = new char[1024];
int jsonLength;
while ((jsonLength = reader.read(buff)) != -1) {
stringBuilder.append(buff, 0, jsonLength);
}
} catch (IOException e) {
e.printStackTrace();
}
JSONObject jsonObject = new JSONObject(stringBuilder.toString());
//获取JSONObject中的参数
String username = jsonObject.getString("username");
String password = jsonObject.getString("password");
String passwordRSA = "";
String usernameRSA = "";
try {
// 这里就是解密后的密码了
usernameRSA = RSAUtil.decryptWithPrivate(username);
passwordRSA = RSAUtil.decryptWithPrivate(password);
} catch (Exception e) {
e.printStackTrace();
}
//查下mysql中的用户信息
return userService.loginRequest(usernameRSA, passwordRSA);
}
}
2.4 通过Mapper访问数据库
public interface LoginService {
String loginRequest(String userName, String password);
}
@Service
public class LoginServiceImpl implements LoginService {
//将DAO注入Service层
@Autowired
private LoginMapper loginMapper;
public String loginRequest(String userName, String password) {
UserBean userBean = loginMapper.getInfo(userName, password);
String loginMsg = "";
if(userBean != null){
loginMsg = "RSA加密和解密,登录成功!";
}else{
loginMsg = "登录失败!";
}
return loginMsg;
}
}
2.5 UserBean 为实体类:
注:其中的@Getter,@Setter和@Data都为lombok的注解,
@Getter
@Setter
@Data
public class UserBean {
private String id;
private String userName;
private String password;
}
2.6 LoginMapper类
public interface LoginMapper {
UserBean getInfo(String userName,String password);
}
2.7 LoginMapper.xml
注:其中的namespace根据真实项目中的文件位置而定。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.test.mapper.LoginMapper">
<select id="getInfo" parameterType="String" resultType="com.example.test.bean.LoginBean">
SELECT * FROM t_user WHERE userName = #{userName} AND password = #{password}
</select>
</mapper>
2.8 附上Application.yml文件
server:
port: 8080
spring:
datasource:
name: mysql
url: jdbc:mysql://localhost:3306/mysql?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.test.bean
RSA:
privateKey: privateKey
publicKey: publicKey
三、测试,浏览器输入:http://localhost:8080/login
3.1 测试获取公钥接口:
3.2 测试加密后的用户名和密码
用户名和密码在mysql中存在且正确,返回“RSA加密和解密,登录成功!”