【发布时间】:2012-07-29 01:24:57
【问题描述】:
我想知道是否有人知道任何库可以在 javascript 中进行加密并在 java 中进行解密。我已经尝试了很多 API,但在 java 中没有得到相同的值。
我想要公私钥加密,因此尝试使用 RSA。
我用过的几个是:
- http://www-cs-students.stanford.edu/~tjw/jsbn/
- http://ats.oka.nu/titaniumcore/js/crypto/readme.txt
- http://www.ohdave.com/rsa/
我检查了几件事,javascript 将字符串分成小块,然后对它们进行加密,这使得 java 和 javascript 中的密文不同。我编辑 javascript 代码以将字符串作为一个整体使用,但没有奏效。
我也尝试将 html 页面的字符集设置为 utf-8,但它也不起作用。 我成功地加密了像'K'这样的单个字符串以正确加密和解密,这让我认为通过将其分成小块来加密javascript中的字符串存在问题(我检查过,但它无法将其加密为所有的)。
我的java实现是:
BigInteger d = new BigInteger("1f3fac65c4ae222e3a3074dd4c38fbb72c0705c4bbac0385b867c12c25a44e01", 16);
BigInteger e = new BigInteger("65537");
BigInteger N = new BigInteger("b42e91fbca364cf2a125aec67ffbdab624fd401100c40ea05189ba34d1028b0d", 16);
String messageToEncrypt = "kishor";
byte [] messageByte = messageToEncrypt.getBytes();
BigInteger message = new BigInteger(messageByte);
//Encrypting and Decrypting messages
//Encrypt a message using N and e:
BigInteger ciphertext = message.modPow(e, N);
//Decrypt the message using N and d:
BigInteger plaintext = ciphertext.modPow(d, N);
byte[] plainTextByte = plaintext.toByteArray();
String decryptMessage = new String(plainTextByte);
/*System.out.println("p : " + p);
System.out.println("q : " + q);*/
System.out.println("N : " + N.toString(16));
System.out.println("e : " + e.toString(16));
System.out.println("d : " + d.toString(16));
/*System.out.println("PhiN : " + PhiN);*/
System.out.println("ciphertext : " + ciphertext.toString(16));
System.out.println("decryptMessage : " + decryptMessage);
}
请让我知道是否有可能,因为我已经搜索了很多问题(在 stackoverflow 本身中)但无法找到解决方案。
【问题讨论】:
-
那么,你想在JS端加密,在java端解密?您如何将加密数据从一个传递到另一个?
-
密文将被传递,解密将使用私钥。
-
如何在 javascript 和 java 之间传输数据?能否也展示一下 Javascript 方面?
-
它只是一个表单提交。我将加密用户输入的字符串然后提交。但我的问题是 javascript 和 java 加密对于相同的密钥、相同的字符串值是不同的。我首先检查双方的加密是否相同。在 javascript 中使用这些库简单明了。现在我只是在 html 中显示值并检查我的 java 类。
-
不要自己实现密码学。使用经过良好测试的库。
标签: java javascript cryptography public-key-encryption