vue RSA加密相关代码
Admin 2023-04-01 群英技术资讯 1540 次浏览
这篇文章主要介绍了“vue RSA加密相关代码”相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇vue RSA加密相关代码文章都会有所收获,下面我们一起来看看吧。
最近安全测试的总是测出安全漏洞来,让开发改。
想了想干脆把请求参数都加密下,前端加密后端解密,这样总差不多了。
看了下AES加密,是对称的,前后端用这个不太行。
于是想到用RSA加密,是非对称的,可以前端加密后端解密。
1.前端是vue项目,使用时,需要先执行:
npm i jsencrypt
把这个依赖下载到node_modules里面。
2.可以增加一个工具类文件:项目名/src/utils/commonUtil.js,内容如下:
import JSEncrypt from "jsencrypt";
export default {
encodeRSA(word, keyStr) {
//这个是公钥,有入参时用入参,没有入参用默认公钥
keyStr = keyStr ? keyStr : 'MIGxxxxxxxxxxxxxxxxxxxxxxxxxx';
//创建对象
const jsRsa = new JSEncrypt();
//设置公钥
jsRsa.setPublicKey(keyStr);
//返回加密后结果
return jsRsa.encrypt(word);
}
}
3.然后,需要使用的地方,就可以这样用:
//引入第2步的工具类
import commonUtil from '@utils/commonUtil'
//引入一个发请求的方法,这个也需要npm i
import axios from 'axios'
//一个发请求用的方法
export function myget(userId) {
return axios.get(`/xxx/user`, {
headers: {
//先把参数rsa加密下,再用urlEncoder转下码,然后放header里传给后台
userId: encodeURIComponent(commonUtil.encodeRSA(userId, null)),
},
}).then(res => {
return res.data
})
}
这样,就把加密参数放入header里的userId里了,后台可以取出后解密。
1.可以先写个工具类,如下:(RSA公钥和私钥可以用这个工具类生成,然后自己记录后使用)
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.Security;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Map;
import java.util.HashMap;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.KeyPair;
public class RSAUtil {
//公钥,可以写前端
public static String public_key="MIGxxxxxx";
//私钥,只能放后端
public static String private_key="MIICxxxxxxxx";
public static void main(String[] args) {
//解密数据
try {
//生成公钥和私钥
genKeyPair();
String publicKey = keyMap.get(0);
//打印出来自己记录下
System.out.println("公钥:" + publicKey);
String privateKey = keyMap.get(1);
//打印出来自己记录下
System.out.println("私钥:" + privateKey);
//获取到后,可以放这里,测试下能不能正确加解密
publicKey = public_key;
privateKey = private_key;
String orgData = "test";
System.out.println("原数据:" + orgData);
//加密
String encryptStr =encrypt(orgData,publicKey);
System.out.println("加密结果:" + encryptStr);
//解密
String decryptStr = decrypt(encryptStr,privateKey);
System.out.println("解密结果:" + decryptStr);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* RSA公钥加密
*
* @param str 加密字符串
* @param publicKey 公钥
* @return 密文
* @throws Exception 加密过程中的异常信息
*/
public static String encrypt(String str,String publicKey) throws Exception {
//base64编码的公钥
byte[] decoded = decryptBASE64(publicKey);
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
//RSA加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
String outStr = encryptBASE64(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 = decryptBASE64(str);
//base64编码的私钥
byte[] decoded = decryptBASE64(privateKey);
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
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;
}
//编码返回字符串
public static String encryptBASE64(byte[] key) throws Exception {
return (new BASE64Encoder()).encodeBuffer(key);
}
//解码返回byte
public static byte[] decryptBASE64(String key) throws Exception {
return (new BASE64Decoder()).decodeBuffer(key);
}
/**
* 密钥长度 于原文长度对应 以及越长速度越慢
*/
private final static int KEY_SIZE = 1024;
/**
* 用于封装随机产生的公钥与私钥
*/
private static Map keyMap = new HashMap();
/**
* 随机生成密钥对
* @throws Exception
*/
public static void genKeyPair() throws Exception {
// KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
// 初始化密钥对生成器
keyPairGen.initialize(KEY_SIZE, new SecureRandom());
// 生成一个密钥对,保存在keyPair中
KeyPair keyPair = keyPairGen.generateKeyPair();
// 得到私钥
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
// 得到公钥
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
String publicKeyString = encryptBASE64(publicKey.getEncoded());
// 得到私钥字符串
String privateKeyString = encryptBASE64(privateKey.getEncoded());
// 将公钥和私钥保存到Map
//0表示公钥
keyMap.put(0, publicKeyString);
//1表示私钥
keyMap.put(1, privateKeyString);
}
}
2.使用时,把公钥内容放入前端js,私钥内容就放后端代码里,就可以和前端联调测试了。样例如下:
//测试接口
@GetMapping("/xxx/user")
public String myget(HttpServletRequest request) {
//先用非对称算法RSA解密一下
try {
//从header里获取到参数
String userId = request.getHeader("userId");
log.debug("收到userId,内容为:"+userId);
//这里解密,注意先用URLDecode处理了下,如果前端没有用的话,这里也不用处理
userId = RSAUtil.decrypt(URLDecoder.decode(userId,"UTF-8"), RSAUtil.private_key);
log.debug("RSA解密成功,userId为"+userId);
} catch (Exception e) {
log.error("RSA解密失败",e);
//如果解密失败,就返回null
return null;
}
return "成功";
}
到此这篇关于“vue RSA加密相关代码”的文章就介绍到这了,更多相关vue RSA加密相关代码内容,欢迎关注群英网络,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
本篇文章给大家带来了关于JavaScript的相关知识,其中主要整理了函数的定义与基本使用的相关问题,包括了用函数语句定义、函数的调用、未定义的实参等等内容,下面一起来看一下,希望对大家有帮助。
这篇文章主要介绍了Vue项目打包部署到apache服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章给大家分享的是微信小程序中列表信息展开收起的效果的实现。小编觉得挺实用的,因此分享给大家做个参考,文中示例代码介绍的非常详细,感兴趣的朋友接下来一起跟随小编看看吧。
轮播图是现在很多网站平台都会应用的一种展现方式,通过定时或者鼠标点击就能够切换看到多张图片,很多商都会将轮播图作为产品展示,这样的效果是用户就能更容易获取商品信息。那么轮播图是如何实现的呢?下面就以基于JavaScript实现的简单轮播图为例,为大家简单介绍下。
这篇文章主要给大家分享的是vue项目实现页面跳转的方法,vue-router是前端开发中用来实现路由页面跳转的一个模块。下面小编将带来如何在已经创建好的vue-router项目基础下实现页面跳转,需要的朋友可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008