如何在express.js中使用crypto.js插入数据时以角度5解密

时间:2018-11-29 10:23:17

标签: javascript node.js angular express cryptojs

我已经在express.js中使用以下代码对一些文本进行了加密:-

var crypto = require('crypto'),
algorithm = 'aes-256-ctr',
password = '6A80FD8D38D579D1090F6CDB62CA34CA',
iv = '79b67e539e7fcadf';

var cipher = crypto.createCipheriv(algorithm,password,iv)
var crypted = cipher.update('hello world','utf8','hex')
crypted += iv.toString('hex') + cipher.final('hex');
console.log(crypted)

现在,我想以角度5解密此cryptonedText。但是我找不到任何方法。我尝试以下代码:-

import * as CryptoJS from 'crypto-js';
var key = CryptoJS.enc.Utf8.parse('6A80FD8D38D579D1090F6CDB62CA34CA');
var iv = CryptoJS.enc.Utf8.parse('79b67e539e7fcadf');

var decrypted = CryptoJS.AES.decrypt(encryptedTextHere, key, {
    iv: iv,
    mode: CryptoJS.mode.CTR,
});

我们非常感谢您提供正确代码的帮助。谢谢

1 个答案:

答案 0 :(得分:0)

尝试这样

import { AES, enc, mode } from 'crypto-js';

const key = enc.Utf8.parse('6A80FD8D38D579D1090F6CDB62CA34CA');
const iv = enc.Utf8.parse('79b67e539e7fcadf');

const decrypted = AES.decrypt('encryptedTextHere', key, {
  iv: iv,
  mode: mode.CTR
});
console.log(decrypted.toString(enc.Utf8));

还请确保安装键入内容

npm i @types/crypto-js --save-dev