我在 node.js 中使用加密来加密和解密用户凭据,效果很好。另一方面,我必须使用相同的方法来获得 Angular 中加密和解密的确切结果。经证实,加密在 Angular 中不起作用。我想知道达到所需结果的任何方法。
我添加了 npm i --save-dev @types/node
但这对我不起作用。还添加了加密,但未找到该模块。我也用 npm crypto-js 加密了,但是和我的 js 方法不一样。
这是我的js文件代码,我需要在我的Angular项目中实现同样的方法:
'use strict';
const crypto = require('crypto');
const IV_LENGTH = 16;
encrypt(text: any, key: any) {
console.log('Before Encrption Data is-->', text, key)
let iv = crypto.randomBytes(this.IV_LENGTH);
let cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
let returnable = iv.toString('hex') + ':' + encrypted.toString('hex');
return returnable;
}
decrypt(text: any, key: any) {
let textParts = text.split(':');
let iv = Buffer.from(textParts.shift(), 'hex');
let encryptedText = Buffer.from(textParts.join(':'), 'hex');
let decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(key), iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}