我在 node.js 中使用加密模块加密了数据,如何使用 crypto-js 在 react.js 中解密
I have encrypted data in node.js using crypto module, how can I decrypt in react.js using crypto-js
在下面的 node.js 中给出的示例工作正常,在 node.js 加密中使用加密模块效果很好,但我不知道如何使用 crypto-js 库解密该数据.
const crypto = require('crypto');
const ENC_KEY = "6fa979f20126cb08aa645a8f495f6d85"; // set random encryption key
const IV = "7777777a72ddc2f1"; // set random initialisation vector
const phrase = "who let the dogs out";
var encrypt = ((val) => {
let cipher = crypto.createCipheriv('aes-256-cbc', ENC_KEY, IV);
let encrypted = cipher.update(val, 'utf8', 'base64');
encrypted += cipher.final('base64');
return encrypted;
});
var decrypt = ((encrypted) => {
let decipher = crypto.createDecipheriv('aes-256-cbc', ENC_KEY, IV);
let decrypted = decipher.update(encrypted, 'base64', 'utf8');
return (decrypted + decipher.final('utf8'));
});
var encrypted_key = encrypt(phrase);
var original_phrase = decrypt(encrypted_key);
console.log(encrypted_key) // hUU10kfhDhOKA0jb4efuYq3BbtyiBl+EqhfYdTkSkiI=
console.log(original_phrase) // who let the dogs out
在 React 中使用 'crypto-js',如何解密由 node.js 中的 'crypto' 模块完成的加密数据?
在反应中我可以加密数据,
import CryptoJS from 'crypto-js'
aesEncrypt(data) {
let key = '6fa979f20126cb08aa645a8f495f6d85';
let iv = '7777777a72ddc2f1';
let cipher = CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse(key), {
iv: CryptoJS.enc.Utf8.parse(iv),
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
});
console.log(cipher.toString());
}
当你从后端获得加密数据时,只需在前端使用具有相同ENC_KEY[=的解密函数(与上面的代码相同) 14=]。
它将return解密数据。
在下面的 node.js 中给出的示例工作正常,在 node.js 加密中使用加密模块效果很好,但我不知道如何使用 crypto-js 库解密该数据.
const crypto = require('crypto');
const ENC_KEY = "6fa979f20126cb08aa645a8f495f6d85"; // set random encryption key
const IV = "7777777a72ddc2f1"; // set random initialisation vector
const phrase = "who let the dogs out";
var encrypt = ((val) => {
let cipher = crypto.createCipheriv('aes-256-cbc', ENC_KEY, IV);
let encrypted = cipher.update(val, 'utf8', 'base64');
encrypted += cipher.final('base64');
return encrypted;
});
var decrypt = ((encrypted) => {
let decipher = crypto.createDecipheriv('aes-256-cbc', ENC_KEY, IV);
let decrypted = decipher.update(encrypted, 'base64', 'utf8');
return (decrypted + decipher.final('utf8'));
});
var encrypted_key = encrypt(phrase);
var original_phrase = decrypt(encrypted_key);
console.log(encrypted_key) // hUU10kfhDhOKA0jb4efuYq3BbtyiBl+EqhfYdTkSkiI=
console.log(original_phrase) // who let the dogs out
在 React 中使用 'crypto-js',如何解密由 node.js 中的 'crypto' 模块完成的加密数据? 在反应中我可以加密数据,
import CryptoJS from 'crypto-js'
aesEncrypt(data) {
let key = '6fa979f20126cb08aa645a8f495f6d85';
let iv = '7777777a72ddc2f1';
let cipher = CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse(key), {
iv: CryptoJS.enc.Utf8.parse(iv),
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
});
console.log(cipher.toString());
}
当你从后端获得加密数据时,只需在前端使用具有相同ENC_KEY[=的解密函数(与上面的代码相同) 14=]。 它将return解密数据。