如何在服务器和GWT客户端之间使用RSA? [英] How Do I use RSA between Server and GWT Client?

查看:292
本文介绍了如何在服务器和GWT客户端之间使用RSA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加密Java Server后端和GWT Client之间的数据。在GWT客户端上,我使用 sbn.js 库。它的工作速度非常快,并且速度比 gwt-crypto 快得多。



这里是如何在给定(e,n)RSA的客户端上进行加密。我创建了一个 JSFiddle

  var n =BC86E3DC782C446EE756B874ACECF2A115E613021EAF1ED5EF295BEC2BED899D26FE2EC896BF9DE84FE381AF67A7B7CBB48D85235E72AB595ABF8FE840D5F8DB; 

var e =3;
var d =7daf4292fac82d9f44e47af87348a1c0b9440cac1474bf394a1b929d729e5bbcf402f29a9300e11b478c091f7e5dacd3f8edae2effe3164d7e0eeada87ee817b;

function do_encrypt(){
var before = new Date();
var rsa = new RSAKey();
rsa.setPublic(n,e);
var res = rsa.encrypt($(#plaintext)。val());

$(#ciphertext)。val(res);
$(#cipherb64)。val(hex2b64(res));
console.log(res);



}

$(#encrypt)点击(function(){
do_encrypt();
});

我使用加密明文的十六进制表示在服务器上解密。
这是我如何在服务器上解密。



我使用以下libs:

  compile'org.bouncycastle :bcprov-jdk15on:1.51'
compile'org.bouncycastle:bcprov-ext-jdk15on:1.51'

以下是使用RSA(d,n)在服务器上解密的方式:

  try {
BigInteger mode = new BigInteger(BC86E3DC782C446EE756B874ACECF2A115E613021EAF1ED5EF295BEC2BED899D26FE2EC896BF9DE84FE381AF67A7B7CBB48D85235E72AB595ABF8FE840D5F8DB,16);
BigInteger exponent = new BigInteger(3);
RSAKeyParameters公钥=新RSAKeyParameters(假的,模量,指数)

的BigInteger exponent2 =新的BigInteger( 7daf4292fac82d9f44e47af87348a1c0b9440cac1474bf394a1b929d729e5bbcf402f29a9300e11b478c091f7e5dacd3f8edae2effe3164d7e0eeada87ee817b,16);
RSAKeyParameters专用密钥=新RSAKeyParameters(真,模量,exponent2)

字符串的EncryptedData = a7f7d5c77c246729141cdfcc77f1f7b38d5f8066b0bc53b2e85119f3f1784f43be2140b5c382ad483bb57cc1b586962cbb1e831e6070a27e4880bbc549e20a372571d09c6b1269ddd7288916f10c96a9138f4165569c4767bfb489de2d44b450ed1495c99da985dc264dabadd9709ccd950ae55095373ccbc3344a26b3efd2dc;

////// decrypt
AsymmetricBlockCipher d = new RSAEngine();
d =新的PKCS1Encoding(d);
d.init(false,privateKey);

byte [] messageBytes2 = new BigInteger(encryptedData,16).toByteArray();
byte [] hexEncodedCipher2 = d.processBlock(messageBytes2,0,messageBytes2.length);


println(encrypted:+ new String(hexEncodedCipher2));

}
catch(异常e){
e.printStackTrace()
println############### #### error
}

我得到以下例外:

 错误| 
org.bouncycastle.crypto.DataLengthException:对于RSA密码,输入太大。

我想这行println(encrypted:+ new String(hexEncodedCipher2));是


  1. 如何在客户端解密?

    / li>
  2. 为什么每次使用相同的(e,n)和相同的明文运行客户端加密时,我会得到不同的加密? >



解决方案

RSA只能加密比密钥长度短的数据块。 p>

所以你必须使用混合方案,这是在RSA中加密一个随机密钥,将使用像AES这样的对称密码。



我有很多主题,例如:如何使用RSA加密C#中的文件(巨大的数据)



下一步去哪里?
Javascript< - > Java AES


I want to encrypt data between a Java Server backend and a GWT Client. On the GWT client I use the sbn.js library. It works very fast and is much faster that gwt-crypto.

Here is How I encrypt on the client side given (e,n) of RSA. I created a JSFiddle:

var n = "BC86E3DC782C446EE756B874ACECF2A115E613021EAF1ED5EF295BEC2BED899D26FE2EC896BF9DE84FE381AF67A7B7CBB48D85235E72AB595ABF8FE840D5F8DB";

var e = "3";
var d = "7daf4292fac82d9f44e47af87348a1c0b9440cac1474bf394a1b929d729e5bbcf402f29a9300e11b478c091f7e5dacd3f8edae2effe3164d7e0eeada87ee817b";

function do_encrypt() {
    var before = new Date();
    var rsa = new RSAKey();
    rsa.setPublic(n, e);
    var res = rsa.encrypt($("#plaintext").val());

    $("#ciphertext").val(res);
    $("#cipherb64").val(hex2b64(res));
    console.log("res");



}

$("#encrypt").click(function () {
    do_encrypt();
});

I use the hex representation of the encrypted plaintext to be decrypted on the server. Here is how I decrypt on the server.

I use the following libs:

compile 'org.bouncycastle:bcprov-jdk15on:1.51'
compile 'org.bouncycastle:bcprov-ext-jdk15on:1.51'

Here is how I decrypt on the server using (d,n) of RSA:

    try {
        BigInteger modulus = new BigInteger("BC86E3DC782C446EE756B874ACECF2A115E613021EAF1ED5EF295BEC2BED899D26FE2EC896BF9DE84FE381AF67A7B7CBB48D85235E72AB595ABF8FE840D5F8DB",16);
        BigInteger exponent = new BigInteger("3");
        RSAKeyParameters publicKey = new RSAKeyParameters(false, modulus, exponent)

        BigInteger exponent2 = new BigInteger("7daf4292fac82d9f44e47af87348a1c0b9440cac1474bf394a1b929d729e5bbcf402f29a9300e11b478c091f7e5dacd3f8edae2effe3164d7e0eeada87ee817b", 16);
        RSAKeyParameters privateKey = new RSAKeyParameters(true, modulus, exponent2)

        String encryptedData = "a7f7d5c77c246729141cdfcc77f1f7b38d5f8066b0bc53b2e85119f3f1784f43be2140b5c382ad483bb57cc1b586962cbb1e831e6070a27e4880bbc549e20a372571d09c6b1269ddd7288916f10c96a9138f4165569c4767bfb489de2d44b450ed1495c99da985dc264dabadd9709ccd950ae55095373ccbc3344a26b3efd2dc";

        ////// decrypt
        AsymmetricBlockCipher d = new RSAEngine();
        d = new PKCS1Encoding(d);
        d.init(false, privateKey);

        byte[] messageBytes2 = new BigInteger(encryptedData,16).toByteArray();
        byte[] hexEncodedCipher2 = d.processBlock(messageBytes2, 0, messageBytes2.length); 


        println("encrypted:"+new String(hexEncodedCipher2));

    }
    catch(Exception e) {
        e.printStackTrace()
        println "#################### error"
    }

I got the following exception:

Error |
org.bouncycastle.crypto.DataLengthException: input too large for RSA cipher.

I suppose that the line println("encrypted:"+new String(hexEncodedCipher2)); is the problem.

  1. How can I decrypt on the client side?

  2. Why do I get different encryptions every time I ran the client side encryption with the same (e,n) and the same plaintext?

解决方案

RSA can only encrypt data blocks that are shorter than the key length.

So you have to use hybrid scheme which is to cipher in RSA a random key that will be use with a symmetric cipher like AES.

There are many topics i SO for this ex : how to use RSA to encrypt files (huge data) in C#

where to go next ? Javascript <-> Java AES

这篇关于如何在服务器和GWT客户端之间使用RSA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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