在javascript中使用RSA加密的问题 [英] Issue using RSA encryption in javascript

查看:82
本文介绍了在javascript中使用RSA加密的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个项目,该项目目前正在 salesforce apex 类中进行加密(使用 Crypto 库),并且需要将该逻辑移到 javascript 文件中.我试图用来进行加密的 node.js 包是 node-rsa.

这是目前存在于 apex 中的代码:

 String algName = 'RSA';斑点签名;String signGen = '';String pKey = 'MIIEvgIBADANBgkqhkiG<其余密钥被剪断>';字符串有效载荷 = '一些有效载荷';blob privateKey = EncodingUtil.base64Decode(pKey);blob 输入 = Blob.valueOf(payload);签名 = Crypto.sign(algName, input, privateKey);signGen = EncodingUtil.base64Encode(signature);

这是最初的 javascript 实现:

<块引用>

 var tmp = forge.util.decode64(pKey);var privateKey2 = new NodeRSA(tmp);有效载荷 = '一些有效载荷var encrypted = key.encrypt(payload, 'base64');

我遇到的问题是该行:var privateKey2 = new NodeRSA(tmp);

导致以下错误:PEM 格式无效

node-rsa 在他们的例子中使用的私钥在以下密钥的开头和结尾都有市场:---- 开始 RSA 私钥 --------- 结束 RSA 私钥 -----

所以我不确定是否必须以某种方式向 node-rsa 库表明该密钥的格式不同.或者我可以尝试使用另一个 RSA javascript 库?

解决方案

我在此处给您留下了关于如何使用 forge 执行此操作的回复:https://github.com/digitalbazaar/forge/issues/150

var pkey = '一些 base64 编码的私钥';var pkeyDer = forge.util.decode64(pkey);var pkeyAsn1 = forge.asn1.fromDer(pkeyDer);var privateKey = forge.pki.privateKeyFromAsn1(pkeyAsn1);//如果 pkey 以标准 PEM 格式存储,则上面可以简化,然后执行以下操作://var pkey = 'pem 格式的一些私钥';//var privateKey = forge.pki.privateKeyFromPem(pkey);var payload = '一些字符串有效载荷';var md = forge.md.sha1.create();md.update(payload, 'utf8');var 签名 = privateKey.sign(md);var signature64 = forge.util.encode64(signature);//signature64 现在是 SHA-1 摘要上的 base64 编码的 RSA 签名//使用 PKCS#1v1.5 填充...如有必要,请参阅其他填充选项的示例

I'm working with a project that currently is doing encryption in a salesforce apex class (using the Crypto library) and that logic needs to be moved into a javascript file. The node.js package I'm trying to use to do the encryption is node-rsa.

Here's the code that currently exists in apex:

    String algName = 'RSA';
    blob signature;
    String signGen = '';
    String pKey =  'MIIEvgIBADANBgkqhkiG<rest of key snipped>';
    String payload = 'some payload';

    blob privateKey = EncodingUtil.base64Decode(pKey);
    blob input = Blob.valueOf(payload);

    signature = Crypto.sign(algName, input, privateKey);

    signGen = EncodingUtil.base64Encode(signature);

And here's the initial javascript implementation:

    var tmp = forge.util.decode64(pKey);
    var privateKey2 = new NodeRSA(tmp);

    payload = 'some payload
    var encrypted = key.encrypt(payload, 'base64');

The problem I'm having is that the line: var privateKey2 = new NodeRSA(tmp);

is causing the following error: Invalid PEM format

The private key that the node-rsa uses in their example has markets at the beginning and end of the key of: ---- BEGIN RSA PRIVATE KEY ----- ---- END RSA PRIVATE KEY -----

So I'm not sure if I have to somehow indicate to the node-rsa library that this key is in a different format. Or maybe there's another RSA javascript library I could try using?

解决方案

I left you a response for how to do this using forge here: https://github.com/digitalbazaar/forge/issues/150

var pkey = 'some base64-encoded private key';
var pkeyDer = forge.util.decode64(pkey);
var pkeyAsn1 = forge.asn1.fromDer(pkeyDer);
var privateKey = forge.pki.privateKeyFromAsn1(pkeyAsn1);

// above could be simplified if pkey is stored in standard PEM format, then just do this:
// var pkey = 'some private key in pem format';
// var privateKey = forge.pki.privateKeyFromPem(pkey);

var payload = 'some string payload';
var md = forge.md.sha1.create();
md.update(payload, 'utf8');

var signature = privateKey.sign(md);
var signature64 = forge.util.encode64(signature);

// signature64 is now a base64-encoded RSA signature on a SHA-1 digest
// using PKCS#1v1.5 padding... see the examples for other padding options if necessary

这篇关于在javascript中使用RSA加密的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆