最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Encrypting string in javascript and decryption in java - Stack Overflow

programmeradmin1浏览0评论

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. /
  2. .txt
  3. /

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.

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).

my java implementation is:

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);
}

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

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./rsa/

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.

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).

my java implementation is:

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);
}

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

Share Improve this question asked Jul 31, 2012 at 9:49 Kishor SharmaKishor Sharma 5998 silver badges15 bronze badges 9
  • so, you want to encrypt in the JS side and decrypt in at the java side? How are you passing the encrypted data from one to the other? – maasg Commented Jul 31, 2012 at 9:55
  • cipher text will be passed and decryption will be using private key. – Kishor Sharma Commented Jul 31, 2012 at 9:56
  • how are you transfering the data between javascript and java? Could you show the Javascript side as well? – maasg Commented Jul 31, 2012 at 9:59
  • its simply a form submission. i will encrypting string entered by user and than submitting it. But my problem is that javascript and java encryption are different for same key, same string value. I am first checking if ecryption in both side are same. Using those library in javascript are simple and straightforward. Right now i am just displaying values in html and checking in my java class. – Kishor Sharma Commented Jul 31, 2012 at 10:06
  • Don't implement cryptography yourself. Use well-tested libraries. – OrangeDog Commented Jul 31, 2012 at 11:11
 |  Show 4 more ments

3 Answers 3

Reset to default 1

Try Gibberish AES (JS Lib) https://github./mdp/gibberish-aes/

RSA is a key transport mechanism. You use it to encrypt keys for symmetric algorithms. From what I’ve seen, people who are using it for anything else don't know what they are doing, and if they persist, end up building a worthless crypto system.

I've successfully inter-operated between Java and the Stanford Javascript Crypto Library. There are many other good JavaScript libraries for symmetric encryption algorithms.

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.

发布评论

评论列表(0)

  1. 暂无评论