I want to encrypt a small string in javascript using RSA with public keys and then decrypt that string in java server side code using private keys.
I am using this code in javascript: http://www-cs-students.stanford.edu/~tjw/jsbn/ Example at: http://www-cs-students.stanford.edu/~tjw/jsbn/rsa2.html
and this code on java side: Encrypting string in javascript and decryption in java
Both of codes work good independently but they do not understand each other. Need to solve this today or I am open to accept any other asymmetrical algorithm that works this way.
wanna leave the example for further generations :)
First, we need to generate key pair in java code
Now let`s move to java code of our current page:
Now for javascript side:
And to decrypt it in java code:
Here you go, sorry, I`m not good in handling these catch clauses.
====================================================================
Upd: Here I found out some issues, regarding this code. First of all, you can change algorithm of
to:
But this is not essential, it works with both. Now the real problem is regarding this line
here when you generate byte array from BigInteger, it sometimes adds [0] in front as signum (Sometimes NOT! so algorithm can decipher that array), so byte array size can be 65/129/257, which cannot be deciphered by algorithm, it throws IllegalBlockSizeException. This problem is discussed in Getting 1 byte extra in the modulus RSA Key and sometimes for exponents also question. The simplest solution is just throw away that zero from array: