Reputation: 117
I am trying to use JCrytion 3.0.1 to encrypt data in javascript and then decrypt it on server by java. My question is how I can get the encrypted string to decrypt it in java using Cipher class which requires Byte[] to process.
This is how I generate my key from java
final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
final KeyPair key = keyGen.generateKeyPair();
And then to decrypt data I used
public String decrypt(byte[] text, PrivateKey key) {
byte[] dectyptedText = null;
try {
final Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, key);
dectyptedText = cipher.doFinal(text); // this is where I get error when trying to use getBytes() to convert encrypted string to byte[]
} catch (Exception ex) {
ex.printStackTrace();
}
return new String(dectyptedText);
}
This is my javascript on client side
var encryptedUser = $.jCryption.encrypt(username, keys);
var encryptedPassword = $.jCryption.encrypt(password, keys);
alert("Start Submiting");
$.ajax({
url: "LoginAuthentication",
data:{username:encryptedUser, password:encryptedPassword},
type:'POST'
}).done(function(){
alert("Successfully Access !!!");
});
I try to use getBytes() but it doesn't work
It works well with other texts I test. My Only problem is how to convert encrypted string from javascript using JCryption to byte[] to decrypt it in Java.
Thank You
Upvotes: 1
Views: 3128