前端使用vue
RSA
准备工作
官网
jsencrypt :http://travistidwell.com/jsencrypt/
encryptlong:https://www.npmjs.com/package/encryptlong
1,安装
1.1 安装jsencrypt,执行以下命令
npm install jsencrypt
1.2 安装encryptlong,执行以下命令:
npm i encryptlong -S
2、开始--在导入包后在前端的工具库中直接封装RSA方法
代码
importJSEncryptfrom'jsencrypt'; /** * RSA公共加密方法-----单体版本 * @param value * @returns {string | false} * @constructor */exportfunctionRSA(value) { constpublicKey="密钥"; constencrypt=newJSEncrypt(); encrypt.setPublicKey(publicKey); consttest=encrypt.encrypt(value); returntest} /** * RSA公共加密方法-------数组+单体版本 * @param value * @returns {string | false} * @constructor */exportfunctionRSA(value) { // RSA公钥constpublicKey="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqAc0oxV0IW8wTjKVd/bI"+"IvI2/o9c12ACq9FceDI8t5Z0GYOL7rAoL9/v6ev6MwzFzLyLaQhAGR6qV8AFQu1s"+"mwIDAQAB"; constencryptor=newJSEncrypt(); encryptor.setPublicKey(publicKey); // 如果传入的值是数组,则对数组中的每个元素进行加密if (Array.isArray(value)) { constencryptedArray= []; value.forEach((item) => { constencryptedValue=encryptor.encrypt(item); encryptedArray.push(encryptedValue); }); returnencryptedArray; } else { // 如果传入的值是单个值,则直接对其进行加密constencryptedValue=encryptor.encrypt(value); returnencryptedValue; } }
在封装完成方法之后在前端的main方法中直接注入!注入后直接可在前端任意方法中调用这个共有的方法。
注入!!!!!
//自定义RSA加密 import {RSA} from "./utils/index"; Vue.prototype.RSA = RSA
3、使用实例
submit() { constencryptedPhone=this.RSA(ts.ruleForm.phone); constencryptedPersonNo=this.RSA(ts.ruleForm.personNo); constencryptedPassword=this.RSA(ts.ruleForm.collectionPassword); letapiUrl=""; letparam= { authorizedPerson: ts.ruleForm.authorizedPerson, personNo: encryptedPersonNo, phone: encryptedPhone, departmentId: ts.ruleForm.departmentId, educationDegree: ts.ruleForm.educationDegree, qualificationNo: ts.ruleForm.qualificationNo, certificateValidity: ts.ruleForm.certificateValidity, authorizationValidity: ts.ruleForm.authorizationValidity, collectionPassword: encryptedPassword, annexFormId: ts.ruleForm.annexFormId, authTypes: ts.ruleForm.authTypes, rpAuthType: ts.ruleForm.rpAuthType, trainScore: ts.ruleForm.trainScore, }; if (this.addFlg==0) { apiUrl="application/ca/add"; } else { apiUrl="application/ca/edit"; param.id=this.formId; } this.appHttp({ url: this.appHttp.adornAppRequestUrl(apiUrl), method: "post", data: this.appHttp.adornData(param), headers: { "Content-Type": "application/json", // 添加请求头,明确指定请求体为 JSON 格式 }, }).then(({data}) => { if (data.code==40000) { ts.$message({ message: data.msg, type: "success", }); this.processShell.reloadPage({ flowCode: "collection.authorization", formId: data.id, }); } else { ts.$message.error(data.msg); } }); },
4、Java后端代码
在Java中封装解密工具类RsaUtil后直接调用就可
importjava.nio.charset.StandardCharsets; importjava.security.KeyFactory; importjava.security.PrivateKey; importjava.security.spec.PKCS8EncodedKeySpec; importjava.util.Base64; /*** @Author 薛千凝* @Date 2023/8/16 17:24*/publicclassRsaUtil { // 解密密码字段publicstaticStringdecryptPassword(StringcollectionPassword) { try { // 获取私钥字符串,这里假设私钥存在 privateKeyStr 变量中StringprivateKeyStr="密钥"; // 解码私钥字符串为字节数组byte[] privateKeyBytes=Base64.getDecoder().decode(privateKeyStr); // 构造 PKCS8EncodedKeySpec 对象PKCS8EncodedKeySpeckeySpec=newPKCS8EncodedKeySpec(privateKeyBytes); // 获取 RSA 密钥工厂实例KeyFactorykeyFactory=KeyFactory.getInstance("RSA"); // 生成私钥对象PrivateKeyprivateKey=keyFactory.generatePrivate(keySpec); // 创建解密器Ciphercipher=Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.DECRYPT_MODE, privateKey); // 解密byte[] decryptedPasswordBytes=cipher.doFinal(Base64.getDecoder().decode(collectionPassword)); // 转换为字符串StringdecryptedPassword=newString(decryptedPasswordBytes, StandardCharsets.UTF_8); returndecryptedPassword; } catch (Exceptione) { e.printStackTrace(); returnnull; } } }