在javascript中加密字符串和在java中解密 [英] Encrypting string in javascript and decryption in java

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

问题描述

我想知道,如果有人知道任何库加密在javascript和java解密。我已经尝试过许多API,但得到不会得到相同的值在java。
我想公钥私钥加密,因此尝试使用RSA。
我很少使用的是:

I would like to know if someone know any library to do encryption in javascript and decryption in java. I have already tried many API, But getting not not getting same values in java.
I want public-private key encryption and hence try to use RSA. Few i have used are:


  1. http://www-cs-students.stanford.edu/~tjw/jsbn/

  2. http://ats.oka.nu/titaniumcore/js/crypto/readme.txt

  3. http://www.ohdave.com/rsa/

  1. http://www-cs-students.stanford.edu/~tjw/jsbn/
  2. http://ats.oka.nu/titaniumcore/js/crypto/readme.txt
  3. http://www.ohdave.com/rsa/

我检查了很少的东西,javascript把字符串分成小块,然后加密它们,使得密文与java和javascript不同。我编辑javascript代码以使用字符串作为一个整体,但没有工作。

Few thing i checked, javascript breaks string into small chunks and then encrypt them which make cipher text different in java and javascript. I edit javascript code to use string as a whole but didn't worked.

我也试图将html页面的字符集设置为utf-8,但它也没有工作。
我成功地加密单字符串像'K'被加密和解密正确,这使我认为在加密字符串在JavaScript中有一个问题,它分成小块(我检查,但它失败加密它作为一个整体)。

I also tried to set charset of html page to utf-8 but it also not worked. I got success in encrypting single character string like 'K' to be encrypted and decrypted correctly which makes me think that there is problem in encrypting string in javascript by dividing it in small chunks (which i checked, but it fails with encrypting it as a whole).

我的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本身),但无法找到解决方案。

Kindly let me know if it is possible as i have searched many question (in stackoverflow itself) but unable to find a solution.

推荐答案

在javascript中,第一个字符被忽略从加密导致问题。固定循环。感谢您的所有回复。

Hey guys i figured out the solution. In javascript, first character was skiped from being encrypted causing the problem. fixed the loop. Thanks for all your replies.

这篇关于在javascript中加密字符串和在java中解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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