NodeJS 在angular 9中使用节点加密

2skhul33  于 2023-05-28  发布在  Node.js
关注(0)|答案(4)|浏览(159)

我的项目是在Angular 6中,它有以下代码行

const crypto = require('crypto-js');
const Buffer = require('buffer').Buffer;
const decrypt = new Buffer(data.result.encr, 'base64');
const privatekey = Buffer.from(data.result.pk, 'base64');
this.decrypted = crypto.privateDecrypt(privatekey, decrypt).toString('utf-8');
return this.decrypted;

它运行良好。
现在我把代码迁移到了Angular 9。我发现crypto不再支持NPM
https://www.npmjs.com/package/crypto
它说我必须使用加密的内置库。但我不知道怎么用。
我想crypto-js会帮助我。但事实并非如此。
如果有人知道如何在Angular 9中使用crypto,或者如何将上面的行转换为crypto-js,那就太好了。
注意:加密只发生在服务器端,因为它们有nodejs。
先谢谢你了。

cnjp1d6j

cnjp1d6j1#

3-4天后,我终于解决了这个问题。
1.我安装了crypto-browserify
1.删除node_modules文件夹,然后使用npm-install再次安装所有依赖项
crypto-browserify提供与crypto相同的功能

p5cysglq

p5cysglq2#

我最近在我的MEAN Stack应用程序中实现了这一点。使用以下命令安装crypto-js后:

npm i crypto-js --save
  • *Angular-9**中的以下服务可以在整个项目中用于加密和解密。
import { Injectable } from '@angular/core';
import * as CryptoJS from 'crypto-js';
import { environment } from 'src/environments/environment';

@Injectable({
  providedIn: 'root'
})
export class CryptoJsService {

  constructor() { }

  get jsonFormatter() {
    return {
      stringify: (cipherParams: any) => {
        const jsonObj = { ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64), iv: null, s: null };
        if (cipherParams.iv) {
          jsonObj.iv = cipherParams.iv.toString();
        }
        if (cipherParams.salt) {
          jsonObj.s = cipherParams.salt.toString();
        }
        return JSON.stringify(jsonObj);
      },
      parse: (jsonStr) => {
        const jsonObj = JSON.parse(jsonStr);
        // extract ciphertext from json object, and create cipher params object
        const cipherParams = CryptoJS.lib.CipherParams.create({
          ciphertext: CryptoJS.enc.Base64.parse(jsonObj.ct)
        });
        if (jsonObj.iv) {
          cipherParams.iv = CryptoJS.enc.Hex.parse(jsonObj.iv);
        }
        if (jsonObj.s) {
          cipherParams.salt = CryptoJS.enc.Hex.parse(jsonObj.s);
        }
        return cipherParams;
      }
    };
  }
  /* Method for Encryption */
  encrypt(value: any) {
    const key =  environment.crypto_js_key; // SECRET KEY FOR ENCRYPTION 
    value = value instanceof String ? value : JSON.stringify(value);
    const encrypted = CryptoJS.AES.encrypt(value,key, 
      { format: this.jsonFormatter, mode: CryptoJS.mode.CBC }).toString();
    return encrypted;
  }

  /* Method for Decryption */
  decrypt(value: any): any {
    const key = environment.crypto_js_key; //SECRET KEY FOR ENCRYPTION
    const decrypted = CryptoJS.AES.decrypt(value, key, {format: this.jsonFormatter }).toString(CryptoJS.enc.Utf8);
    return JSON.parse(decrypted);
  }
}

Nodejs中,以下实用程序可以在整个应用中使用:

var CryptoJS = require('crypto-js');
var config = require('../config/environment');

module.exports.encrypt = function(value){
  var JsonFormatter = {
    stringify: function(cipherParams){
      var jsonObj = { ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64) };
      if (cipherParams.iv) {
        jsonObj.iv = cipherParams.iv.toString();
      }
      if (cipherParams.salt) {
        jsonObj.s = cipherParams.salt.toString();
      }
      return JSON.stringify(jsonObj);
    },
    parse: function(jsonStr) {
      var jsonObj = JSON.parse(jsonStr);
      // extract ciphertext from json object, and create cipher params object
      var cipherParams = CryptoJS.lib.CipherParams.create({
        ciphertext: CryptoJS.enc.Base64.parse(jsonObj.ct)
      });
      if (jsonObj.iv) {
        cipherParams.iv = CryptoJS.enc.Hex.parse(jsonObj.iv);
      }
      if (jsonObj.s) {
        cipherParams.salt = CryptoJS.enc.Hex.parse(jsonObj.s);
      }
      return cipherParams;
    }
  }
  value = value instanceof String ? value: JSON.stringify(value);
  var encrypted = CryptoJS.AES.encrypt(value, config.crypto_js_key, {
    format: JsonFormatter, mode: CryptoJS.mode.CBC
  }).toString();
  return encrypted;
}

module.exports.decrypt = function(value) {
  return CryptoJS.AES.decrypt(value, config.crypto_js_key, {format: JsonFormatter }).toString(CryptoJS.enc.Utf8);
}
q9yhzks0

q9yhzks03#

根据所需的哈希值,对我来说最好的选择是ts-md5 lib。

import {Md5} from 'ts-md5/dist/md5';
...
Md5.hashStr('blah blah blah'); // hex:string
Md5.hashStr('blah blah blah', true); // raw:Int32Array(4)
Md5.hashAsciiStr('blah blah blah'); // hex:string
Md5.hashAsciiStr('blah blah blah', true); // raw:Int32Array(4)
egdjgwm8

egdjgwm84#

我使用的是Angular 16,并在一个方法中使用了crypto,它工作了。我没有使用任何包裹。
我的要求是得到一些随机数。

private getRandomNumbers(){
const array = new Uint32Array(10);
crypto.getRandomValues(array);

for (const num of array) {
  console.log(num);
}
}

相关问题