本文介绍了带有角度的 RSA 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hello I am trying to implement encryption in my application. I am using angular (angular-4) for the frontend and node js for the backend. Communication is done through socket.io through custom commands. But basically what I am stuck at is finding an appropriate library for RSA encryption in client side. client will first request server for an RSA public key. Server responds with key but now I cannot find any library suitable to encrypt data with RSA using this public key. I have tried node-rsa. Following is a code 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

非常感谢您的帮助.

推荐答案

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

JSEncrypt with angular

Please Find Solution Plunker here:

JSEncrypt with angular

https://plnkr.co/edit/sEPK1DcynMphJnGziUVX

我已经使用了 JSEncrypt v2.3.0 Lib.

I have used JSEncrypt v2.3.0 Lib for same.

实施

在 Angular 项目的 Asset 文件夹中添加 JSEncrypt Lib javascript 文件.在 index.html

Add JSEncrypt Lib javascript file in Asset Folder of Angular Project.Add script in index.html

因此,它将在所有组件中可用.

So, It will available at all Component.

在您要使用它的组件文件中声明 JSEncrypt.

declare JSEncrypt at your component file where you want to use it.

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

这篇关于带有角度的 RSA 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 22:43