首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带angular的RSA库

带angular的RSA库
EN

Stack Overflow用户
提问于 2017-07-03 21:50:45
回答 3查看 18K关注 0票数 4

你好,我正在尝试在我的应用程序中实现加密。我使用angular (angular-4)作为前端,使用node js作为后端。通信通过自定义命令通过socket.io完成。但基本上,我被困在客户端为RSA加密寻找合适的库。客户端将首先向服务器请求RSA公钥。服务器使用密钥进行响应,但现在我找不到任何适合使用此公钥使用RSA加密数据的库。我已经尝试了node-rsa。以下是代码sn

代码语言:javascript
复制
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'));
    }

但是我收到了这个错误。

代码语言:javascript
复制
Error during encryption. Original error: TypeError: crypt.createHash is not a function
at NodeRSA.webpackJsonp.../../../../node-rsa/src/NodeRSA.js.module.exports.NodeRSA.$$encrypt

如果您能帮忙,我们将不胜感激。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 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;

在类中声明变量

代码语言:javascript
复制
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包含结果字符串

票数 7
EN

Stack Overflow用户

发布于 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

代码语言:javascript
复制
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);
    }

}
票数 4
EN

Stack Overflow用户

发布于 2018-08-31 17:54:42

如果你想继续使用非常好的node-rsa库,你可以做些什么呢?将ng-cli从你的项目中弹出,并将webpack配置为使用crypto-browserify库自动多填充nodejs加密模块。

首先,从您的项目中弹出ng-cli (您将需要运行yarn buildnpm run build来构建您的项目,并且您将无法再使用ng build):

代码语言:javascript
复制
ng eject

然后,您需要将此数据添加到弹出过程创建的webpack.config.js文件中:

代码语言:javascript
复制
"node": {
    ...
    "crypto": true,
    ...
}

这将告诉webpack多填充crypto模块。

我将很快深入研究如何保持ng-cli并拥有正确的多填充行为……

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44887294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档