RAS加密算法其实原理很简单:前台发送请求到后台获得加密公钥,然后前台js用得到的公钥进行加密,将密文传到后台,后台再对密文进行解密。具体代码如下:
加密公钥:
PublicKeyMap publicKeyMap = RSAUtils.getPublicKeyMap();// 生成公钥
前台js代码:
$.post("../../vuser/produceRas", function(data) {
if (data) {
var modulus = data.modulus;// 后台生成密钥对
var exponent = data.exponent;
var epwd = $('#password').val().trim();
if (epwd.length != 256) {
var publicKey = RSAUtils.getKeyPair(exponent, '', modulus);
var getMapKey = RSAUtils.encryptedString(publicKey, epwd);// 根据公钥生成密文
$.post("../../vuser/getKeyMap", {
getMapKey : getMapKey,
userName : userName,
phoneId : phoneId,
messages : messages
}, function(data) {
alert(data);
}, "json");
$("#save").submit();
}
}
}, "json");
后台java解密:
String encrypttext = request.getParameter("getMapKey");// 前台密文 String
String vpwd = RSAUtils.decryptStringByJs(encrypttext);//进行解密
maven依赖:
<!-- RSA包 -->
<dependency>
<groupId>org.lucee</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.52.0</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.7</version>
</dependency>