以 Vue 作为例子
1.CryptoJS加密解密
首先引用CryptoJS的库
https://cdn.staticfile.org/crypto-js/3.1.9/crypto-js.min.js

加密:

const key = CryptoJS.enc.Utf8.parse("1234123412ABCDEF");  //十六位十六进制数作为密钥
const iv = CryptoJS.enc.Utf8.parse('ABCDEF1234123412'); //十六位十六进制数作为密钥偏移量
let srcs = CryptoJS.enc.Utf8.parse('this is test');//需要加密的数据
let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
let a1 =  encrypted.ciphertext.toString().toUpperCase();

解密:

let encryptedHexStr = CryptoJS.enc.Hex.parse(a1);
let srcs1 = CryptoJS.enc.Base64.stringify(encryptedHexStr);
let decrypt1 = CryptoJS.AES.decrypt(srcs1, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
let decryptedStr = decrypt1.toString(CryptoJS.enc.Utf8);
let b1 = decryptedStr.toString()

2.jsrsasign
先引用jsrsasign的库
https://cdn.staticfile.org/jsrsasign/8.0.12/jsrsasign-all-min.js

需要一对公钥和私钥

let priK='你的公钥'
let pk=‘你的私钥’

加密

                let src = "测试原文";// 加密的数据
                let pub = KEYUTIL.getKey(pk);
                let enc = KJUR.crypto.Cipher.encrypt(src,pub);
                console.log(hextob64(enc));

解密

                let prv = KEYUTIL.getKey(priK);
                let dec = KJUR.crypto.Cipher.decrypt(hextob64(enc),prv);
                console.log(dec);

创建签名

				let signature=new KJUR.crypto.Signature({alg:"SHA1withRSA",prvkeypem:priK});
				signature.updateString(src);
				let sign = hextob64(signature.sign());

解签

				let signatureVf = new KJUR.crypto.Signature({alg: "SHA1withRSA", prvkeypem: pk});
                signatureVf.updateString(src);
                let b = signatureVf.verify(b64tohex(sign));

3.jsencrypt
npm install jsencrypt
import JSEncrypt from ‘jsencrypt/bin/jsencrypt’
首先同样需要一对公钥和私钥

// 加密
                let encryptor = new JSEncrypt()
                let publicKey = `MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC0keMaeU1uF/RKwk8MN/4BYCD9
                                NT2UGqJfgQ+Uau0JpxzlJ7jrCwuZPWHR/Jjkt46J8IfbbqL47WLNxIPgt/JZaqan
                                pqTkJvXv519SbyOfWNvDENfygI8m117C7AjOejXVxIoUA+OTvnetliCEkGKnmS6m
                                1noS6r6uBr8tfPf5fwIDAQAB`
                encryptor.setPublicKey(publicKey) // 设置公钥
                let rsaPassWord = encryptor.encrypt('我是测试的数据'); // 对需要加密的数据进行加密
                console.log('我的加密数据是',rsaPassWord);

                // 解密
                let secretWord = rsaPassWord  //假如后台已经拿到了加密数据
                let decrypt = new JSEncrypt() // 新建JSEncrypt对象
                let privateKey = `MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALSR4xp5TW4X9ErC
                                Tww3/gFgIP01PZQaol+BD5Rq7QmnHOUnuOsLC5k9YdH8mOS3jonwh9tuovjtYs3E
                                g+C38llqpqempOQm9e/nX1JvI59Y28MQ1/KAjybXXsLsCM56NdXEihQD45O+d62W
                                IISQYqeZLqbWehLqvq4Gvy189/l/AgMBAAECgYB5ffr+sgUuAQIGVuyWvQmzZTlt
                                BKh1AOKlPZH8+2CXcRAZAvwBcUIRjeJGRh2dLf/Cwcq3Ep6mvBoRdpqA1WNCMdtu
                                rkkJQlYE0eEJ6cKSQQZpbSnLBDfd4wEwUIHVAZVBwNgJB1r14sk/cPs6WiH9YQ+A
                                cBD1VgXOrburIEOFQQJBAOuzFt1LI03LZEnh3TsUPZ2ybuvShUjXP0LYB9Msn0EZ
                                UOwYmhpOWzNNhzBNE2bUjXDz8eMcTcKfiAG0+i7P1KECQQDEHz/8NDMAqQ+l1uOj
                                uRwZu0ewK29mh60ukqjbh0ShxK1zGGaiqr8vECam4VDLMyFp9igbsKS3AmmMPtvD
                                KPofAkAbK8sP+bB5MGBAUCRTwjKUMlStJNium/mz9/l286bsN0Hn5bImJDBJSr+x
                                5sC80vX3NngFCWdBb2ibYcSRju3hAkAe38CF5n3E89hXxh1t770hu97MN/pAjMlR
                                lJrqfD/sq3aWsNevcevI7+T1znCWnIT9Flg4C7XSbZnKDzcvYOsHAkEAhX3rHD4l
                                2rO49MXV7HGONhdHaPCSbQ2BQKgV7GHYKAqqlaBcZFwc5c8bG3XjeDopK4SmQyNE
                                Q25FAm6vAF0OcA==`
                decrypt.setPrivateKey(privateKey)
                let getWord = decrypt.decrypt(secretWord) // 解密
                console.log('我是解密后的原本内容',getWord)
Logo

前往低代码交流专区

更多推荐