uniapp 使用CryptoJS aes-256-ecb加解密写个正确列子
时间: 2024-10-10 19:08:40 浏览: 61
UniApp 中使用 CryptoJS 进行 AES-256-ECB 加解密,首先你需要在项目中引入 CryptoJS 库。这是一个简单的例子:
```javascript
// 引入 CryptoJS
import * as CryptoJS from 'crypto-js';
// 设置加密密钥(假设是128位十六进制字符串)
const encryptionKey = 'your-secret-key';
// 加密函数
function encrypt(text) {
const iv = CryptoJS.lib.WordArray.random(CryptoJS.AES.blockSize);
const encrypted = CryptoJS.AES.encrypt(text, encryptionKey, { mode: CryptoJS.mode.ECB, iv });
return {
ciphertext: encrypted.toString(),
iv: iv.toString()
};
}
// 解密函数
function decrypt(ciphertext, iv) {
const bytes = CryptoJS.enc.Utf8.parse(ciphertext);
const deciphered = CryptoJS.AES.decrypt({ciphertext: bytes}, encryptionKey, {mode: CryptoJS.mode.ECB, iv: CryptoJS.lib.WordArray.parse(iv)});
return deciphered.toString(CryptoJS.enc.Utf8);
}
// 示例
let plainText = 'Hello, World!';
let encryptedData = encrypt(plainText);
console.log('Encrypted:', encryptedData.ciphertext);
let decryptedText = decrypt(encryptedData.ciphertext, encryptedData.iv);
console.log('Decrypted:', decryptedText);
阅读全文
相关推荐















