原标题:【今日学习】VUE使用RSA加密技术
1
Vue端搭建
首先我们还是先安装必要的模块哈:
npm install jsencrypt --save
接下来我们需要在main.js的配置文件中配置上全局变量哈:
import JSEncrypt from 'jsencrypt';
接下来,我们在main.js创建加密函数:(注:这里的公钥仅仅是作为测试使用哈,正式使用的公钥应该是服务器发过来的哦)
Vue.prototype.$getRsaCode = function(str){
letpubKey=`MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCpwvmYZzKNem5bF1/p8DGNBwAEJ7xicMM0VG0
TAK/vtJ/wFfIQ0s+n66GbtF9I8yRJBfv9+JE5VHNumsEipTRSkgadhtvMsh3G5oW5mNZZLNJAw8Z/E3CkBcPblc
HTwY0Qg50UKvJOg1izxdPxLKwV6+RLBSOYNn1BoZmT/i+ikQIDAQAB`;
// ES6 模板字符串 引用 rsa 公钥
let encryptStr = new JSEncrypt();
encryptStr.setPublicKey(pubKey); // 设置 加密公钥
let data = encryptStr.encrypt(str.toString()); // 进行加密
return data;
};
接下来,我们在main.js创建解密函数咯:(注意:这里的私钥也绝对不会发给客户端的哈,这里仅仅只是测试是否成功哦)
Vue.prototype.$decodeRsaCode = function(str){
letpriKey=`MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKnC+ZhnMo16bl
sXX+nwMY0HAAQnvGJwwzRUbRMAr++0n/AV8hDSz6froZu0X0jzJEkF+/34kTlUc26awSKlNFKSB
p2G28yyHcbmhbmY1lks0kDDxn8TcKQFw9uVwdPBjRCDnRQq8k6DWLPF0/EsrBXr5EsFI5g2fUGh
mZP+L6KRAgMBAAECgYB0tZbjMA+RNJqqBWfrbTajvixErwzUoLGTSHe0mMRcB9HtPSkuZ8BbsaN
paJgZjw9jJLoZ4Mn2oBi78ajWKL3O5QxGxBhFkWAqp1aLcH4I3mV5Nsz/xaQNJ5cUpNoiInHF6o
qe3q8Mb1OSg0DD8WUMWARpqUYrCOeDDFVA2/vAAQJBAPBDrg5rSpvRicTsnW5t3tbUo6fWf8Xfd
KvBKL/NvYW36vClCPbrrMvLZTQh+tbZ1lND8GmFqAwiU9/NHAhMSjcCQQC04TtgoHQIQ+XQ3S9l
/ZtD6MCVuGobLohgWaEKajqd5rxSguY9w9dc0x1ZONSYf1ooh/sTWF5C+iIwkdkionV3AkEAqPo
wlBuJYYVBK6puHphPPTBX2VbWjkfWOmWjTyJXxI/RQGYRo/ekk7/88WlkjW6veegyUUSbJ82Mx7
3tlThyiQJAQQv+XnFVSOOh8cqZEQRyoalCGJWJEt1ZDc0trKEnE0G217VbNzsGZ8tD8qINUgn6U
jkUgZB11YA+a1kHzPy/YQJBAJ6i9CSSBDWi8uAYlKecuZWQEza7l0aiuaPIi3j5UE5Y2U5zP369
JxwtaYpKs+6pVeD1W2cw5YGe1o2ZyZixDhU=`;let encryptStr = new JSEncrypt();encryptStr.setPrivateKey(priKey);return encryptStr.decrypt(str);}
接下来,我们创建测试咯:
initRSA:function () {
// 1、通过调用加密函数得到密文字符串 str
let str = this.$getRsaCode('123456');
// 2、调用解密函数开始解密
alert(this.$decodeRsaCode(str));
}
2
Java端搭建(SSM框架)
注意:这里需要导入一个jar文件哈,这个jar主要用来作为解析使用,这里提供Maven的依赖:
commons-codec
commons-codec
1.14
接下来就是我们的Java代码咯:
public class RsaUtil {
private static Map keyMap = new HashMap(); //用于封装随机产生的公钥与私钥
public static void main(String[] args) throws Exception {
//生成公钥和私钥
genKeyPair();
//加密字符串
String message = "123456";
System.out.println("随机生成的公钥为:" + keyMap.get(0));
System.out.println("随机生成的私钥为:" + keyMap.get(1));
String messageEn = encrypt(message,keyMap.get(0));
System.out.println(message + "\t加密后的字符串为:" + messageEn);
String messageDe = decrypt(messageEn,keyMap.get(1));
System.out.println("还原后的字符串为:" + messageDe);
}
/**
* 随机生成密钥对
* @throws NoSuchAlgorithmException
*/
public static void genKeyPair() throws NoSuchAlgorithmException {
// KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
// 初始化密钥对生成器,密钥大小为96-1024位
keyPairGen.initialize(1024,new SecureRandom());
// 生成一个密钥对,保存在keyPair中
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 得到私钥
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 得到公钥
String publicKeyString = new String(.encode(publicKey.getEncoded()));
// 得到私钥字符串
String privateKeyString = new String(.encode((privateKey.getEncoded())));
// 将公钥和私钥保存到Map
keyMap.put(0,publicKeyString); //0表示公钥
keyMap.put(1,privateKeyString); //1表示私钥
}
/**
* RSA公钥加密
* @param str 加密字符串
* @param publicKey 公钥
* @return 密文
* @throws Exception 加密过程中的异常信息
*/
public static String encrypt( String str, String publicKey ) throws Exception{
//编码的公钥
byte[] decoded = .decode(publicKey);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(
new X509EncodedKeySpec(decoded));
//RSA加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
String outStr = .encodeString(cipher.doFinal(str.getBytes("UTF-8")));
return outStr;
}
/**
* RSA私钥解密
* @param str 加密字符串
* @param privateKey 私钥
* @return 明文
* @throws Exception 解密过程中的异常信息
*/
public static String decrypt(String str, String privateKey) throws Exception{
//64位解码加密后的字符串
byte[] inputByte = .decode(str.getBytes("UTF-8"));
//编码的私钥
byte[] decoded = .decode(privateKey);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(
new PKCS8EncodedKeySpec(decoded));
//RSA解密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
String outStr = new String(cipher.doFinal(inputByte));
return outStr;}}返回搜狐,查看更多
责任编辑: