$ npm install crypto-js
安装cryptojs 安装方法
实际使用情况:
import CryptoJS from "crypto-js";
//let decrypted = CryptoJS.lib.SerializableCipher.decrypt(
? ? ? ? ? // ? ?CryptoJS.algo.AES,
? ? ? ? ? // ? ?data.tuituiMobileCrypt,
? ? ? ? ? // ? ?CryptoJS.enc.Utf8.parse(data.privateKey),
? ? ? ? ? // ? ?{
? ? ? ? ? // ? ? ?mode: CryptoJS.mode.ECB,
? ? ? ? ? // ? ? ?padding: CryptoJS.pad.Pkcs7
? ? ? ? ? // ? ?}
? ? ? ? ? // ?)
?
? ? ? ? ? //data.basicInfo.userMobile= JSON.parse(CryptoJS.enc.Utf8.stringify(decrypted))[data.tuituiMobile];?
? ? ? ? ? //}
案例情况:
// 编码 function utf8_to_b64(str) { return window.btoa(unescape(encodeURIComponent(str))); } // 解码 function b64_to_utf8(str) { return decodeURIComponent(escape(window.atob(str))); } // Usage: utf8_to_b64('? à la mode'); // 4pyTIMOgIGxhIG1vZGU= b64_to_utf8('4pyTIMOgIGxhIG1vZGU='); // "? à la mode" utf8_to_b64('I \u2661 Unicode!'); // SSDimaEgVW5pY29kZSE= b64_to_utf8('SSDimaEgVW5pY29kZSE='); // "I ? Unicode!" utf8_to_b64('我爱上海'); // 5oiR54ix5Lit5Zu9 b64_to_utf8('SSDimaEgVW5pY29kZSE='); // "我爱上海" utf8_to_b64(123456789); // MTIzNDU2 b64_to_utf8("MTIzNDU2"); // 123456789
const key = CryptoJS.enc.Utf8.parse("1111111111111111"); //16位 const iv = CryptoJS.enc.Utf8.parse("1111111111111111"); export default { //aes加密 encrypt(word) { let encrypted = ""; if (typeof word == "string") { const srcs = CryptoJS.enc.Utf8.parse(word); encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); } else if (typeof word == "object") { //对象格式的转成json字符串 const data = JSON.stringify(word); const srcs = CryptoJS.enc.Utf8.parse(data); encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); } return encrypted.ciphertext.toString(); }, // aes解密 decrypt(word) { const encryptedHexStr = CryptoJS.enc.Hex.parse(word); const srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr); const decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8); return decryptedStr.toString(); } };