【问题标题】:RSA library with angular带有角度的 RSA 库
【发布时间】:2017-12-06 19:46:47
【问题描述】:

您好,我正在尝试在我的应用程序中实现加密。我在前端使用 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

非常感谢您的帮助。

【问题讨论】:

标签: node.js angular encryption


【解决方案1】:

请在此处查找解决方案 Plunker:

带角度的JSEncrypt

https://plnkr.co/edit/sEPK1DcynMphJnGziUVX

我也使用过 JSEncrypt v2.3.0 Lib。

实施

在 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 包含结果字符串

【讨论】:

  • 虽然理论上这可以回答这个问题,it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
  • 你好@yatin-patel。当我在 Angular 中使用 JSEncrypt 时,它会使用共享的公钥完美地加密数据,但是当我在 java 中使用私钥对其进行解密时,它会给我带来不可读的字符。你能帮帮我吗?
【解决方案2】:

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

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

ng eject

那么你需要把这个数据添加到弹出进程创建的webpack.config.js文件中:

"node": {
    ...
    "crypto": true,
    ...
}

这将告诉 webpack 填充 crypto 模块。

我将很快深入研究如何保持 ng-cli 并拥有正确的 polyfill 行为...

【讨论】:

    【解决方案3】:

    我使用 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);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 2016-08-14
      • 2019-03-11
      • 1970-01-01
      • 2018-03-11
      • 1970-01-01
      相关资源
      最近更新 更多