我正在尝试将此加密加密和解密 js 代码转换为 Angular 项目的打字稿

时间:2021-06-08 07:47:54

标签: node.js angular encryption

我在 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();
 } 

当我添加加密时,发生了这种类型的错误:

0 个答案:

没有答案