如何在服务器和GWT客户端之间使用RSA?

我想在Java Server后端和GWT客户端之间加密数据。 在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").click(function () { do_encrypt(); }); 

我使用加密明文的hex表示在服务器上解密。 这是我在服务器上解密的方式。

我使用以下库:

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

以下是我使用(d,n)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" } 

我得到以下exception:

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

我想行println(“encrypted:”+ new String(hexEncodedCipher2)); 是问题。

  1. 我如何在客户端解密?

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

RSA只能加密短于密钥长度的数据块。

因此,您必须使用混合方案,即在RSA中加密随机密钥,该密钥将与AES等对称密码一起使用。

对于这个例子我有很多主题: 如何使用RSA加密C#中的文件(大数据)

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