你好,我正在尝试在我的应用程序中实现加密。我使用angular (angular-4)作为前端,使用node js作为后端。通信通过自定义命令通过socket.io完成。但基本上,我被困在客户端为RSA加密寻找合适的库。客户端将首先向服务器请求RSA公钥。服务器使用密钥进行响应,但现在我找不到任何适合使用此公钥使用RSA加密数据的库。我已经尝试了node-rsa。以下是代码sn
import * as NodeRSA from 'node-rsa';
@Injectable()
export class SecurityService {
RSA: any
initializeRSA(key: string) {
this.RSA = new NodeRSA();
this.RSA.importKey(key)
console.log(this.RSA.encrypt('Hello World'));
}但是我收到了这个错误。
Error during encryption. Original error: TypeError: crypt.createHash is not a function
at NodeRSA.webpackJsonp.../../../../node-rsa/src/NodeRSA.js.module.exports.NodeRSA.$$encrypt如果您能帮忙,我们将不胜感激。
发布于 2017-07-03 23:35:47
请在此处找到解决方案柱塞:
带角度的JSEncrypt
https://plnkr.co/edit/sEPK1DcynMphJnGziUVX
我已经使用了JSEncrypt v2.3.0lib来做同样的事情。
Implementation
在Angular项目的Asset文件夹中添加JSEncrypt Lib javascript文件。在index.html中添加脚本
<script src="jsencrypt.js"></script>
因此,它将在所有组件中可用。
在要使用它的组件文件中声明JSEncrypt。
declare var JSEncrypt: any;
在类中声明变量
decrypt = new JSEncrypt();
const privatekey = Private Key goes here;
const publickey = Public key goes here;
const decryptDataRow = Decrypted data string;
this.decrypt.setPrivateKey(privatekey);
this.decryptdata = this.decrypt.decrypt(decryptDataRow);decryptdata包含结果字符串
发布于 2018-09-21 03:18:58
我使用CryptoJS进行AES编码。以及,用于RSA编码的node-forge。
第1部分:npm install --save node-forge npm install --save crypto-js npm install --save @types/crypto-js npm install --save @types/node-forge
import { Injectable } from '@angular/core';
import * as forge from 'node-forge';
import * as CryptoJS from 'crypto-js';
/*
* forge: For RSA encryption
* CryptoJS: For AES encryption
*/
export interface AESKeys {
iv: string;
key: string;
}
@Injectable()
export class MyEncryptService {
constructor() {}
/**
* @param randomWordArray
* AES Key or IV generated in this service as Hex string
* @param pemKey
* the Public Key from "get_rsa_key" endpoint (separate http service)
* @returns
* a RSA encrypted random key/iv
*/
public getBase64Encrypted(randomWordArray, pemKey): string {
const pk = forge.pki.publicKeyFromPem(pemKey);
return forge.util.encode64(pk.encrypt(forge.util.hexToBytes(randomWordArray)));
}
/**
* @param text
* field input text
* @param randomKey
* random key generated from word array
* @param randomIv
* random iv generated from word array
* @returns
* encrypted text
*/
public encrypted(text, randomKey, randomIv): string {
const cipherText: any = CryptoJS.AES.encrypt(text, randomKey, {
iv: randomIv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.NoPadding
});
return cipherText.toString();
}
/**
* @param wordLength
* normally: IV = 16 or Key = 32
* @returns
* randomly generated 128bit (IV) or 256bit (Key) word array
*/
public getWordArray(wordLength): string {
return CryptoJS.lib.WordArray.random(wordLength);
}
}发布于 2018-08-31 17:54:42
如果你想继续使用非常好的node-rsa库,你可以做些什么呢?将ng-cli从你的项目中弹出,并将webpack配置为使用crypto-browserify库自动多填充nodejs加密模块。
首先,从您的项目中弹出ng-cli (您将需要运行yarn build或npm run build来构建您的项目,并且您将无法再使用ng build):
ng eject然后,您需要将此数据添加到弹出过程创建的webpack.config.js文件中:
"node": {
...
"crypto": true,
...
}这将告诉webpack多填充crypto模块。
我将很快深入研究如何保持ng-cli并拥有正确的多填充行为……
https://stackoverflow.com/questions/44887294
复制相似问题