cryptojs支持rsa加密_如何使用JSBN加密Crypto-JS密钥?

本文介绍了如何使用JSBN库来加密Crypto-JS的WordArray类型密钥,解决将二进制数据(特别是WordArray)转换为可加密格式的问题。通过修改pkcs1pad2函数以接受十六进制字符串或直接处理WordArray,可以实现对二进制数据的RSA加密。
摘要由CSDN通过智能技术生成

I'm using JSBN to encrypt/decrypt data using public/private keypairs. It works great for text data, including hex strings.

My problem is now I have binary data, specifically Crypto-JS Word Arrays, that I need to encrypt with a public key and send along to another platform.

So consider this:

var key = CryptoJS.lib.WordArray.random(256/8);

var rsa = new RSAKey();

rsa.setPublic(modulus, exponent);

var encrypted_key = rsa.encrypt(key.toString());

This works but it means 'encrypted_key' is infact a hex string that's been encrypted, not the actual key. I need to encrypt the actual key.

So I see two challenges here:

1) I'm not 100% sure how to get the actual bytes out of a CryptoJS.lib.WordArray -- though that doesn't seem totally insurmountable.

2) I have no idea if it's even possible to encrypt binary data using JSBN. I'd love pointers to figure out how to do it.

Any thoughts?

解决方案

The JSBN library contains a function, namely pkcs1pad2(), wherein it converts the text to numeric values using JavaScript's charCodeAt() function. You'll see that conversion code in the first while() loop:

function pkcs1pad2(s,n) {

if(n < s.length + 11) { // TODO: fix for utf-8

alert("Message too long for RSA");

return null;

}

var ba = new Array();

var i = s.length - 1;

while(i >= 0 && n > 0) {

var c = s.charCodeAt(i--);

if(c < 128) { // encode using utf-8

ba[--n] = c;

}

else if((c > 127) && (c < 2048)) {

ba[--n] = (c & 63) | 128;

ba[--n] = (c >> 6) | 192;

}

else {

ba[--n] = (c & 63) | 128;

ba[--n] = ((c >> 6) & 63) | 128;

ba[--n] = (c >> 12) | 224;

}

}

ba[--n] = 0;

var rng = new SecureRandom();

var x = new Array();

while(n > 2) { // random non-zero pad

x[0] = 0;

while(x[0] == 0) rng.nextBytes(x);

ba[--n] = x[0];

}

ba[--n] = 2;

ba[--n] = 0;

return new BigInteger(ba);

}

If you wish to encrypt binary data then you'll likely have to modify this function so it converts the input in the way you want it.

Below is an example of pkcs1pad2() modified to accept binary data in the form of a hex string. If you use this version of pkcs1pad2() then you can convert your CryptoJS.lib.WordArray into hex and pass that hex string to rsa.encrypt().

function pkcs1pad2(hexPlaintext,n) {

if(n < hexPlaintext.length/2 + 11) {

alert("Message too long for RSA");

return null;

}

var ba = new Array();

var i = hexPlaintext.length;

while(i >= 2 && n > 0) {

ba[--n] = parseInt(hexPlaintext.slice(i-2,i),16);

i-=2;

}

ba[--n] = 0;

var rng = new SecureRandom();

var x = new Array();

while(n > 2) { // random non-zero pad

x[0] = 0;

while(x[0] == 0) rng.nextBytes(x);

ba[--n] = x[0];

}

ba[--n] = 2;

ba[--n] = 0;

return new BigInteger(ba);

}

Alternatively, you could modify it to take the WordArray directly and convert that to the array format that is used by JSBN, but I'll leave that as an exercise for the reader.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值