I am trying to encrypt data using javascript and decrypt on server side
Java code using TripleDES or DESede. I am able to encrypt, but I am not able to decrypt it at server side. Can anyone help me resolving this issue. Following is the code for encryption in JS:
var loginId = "xyzabc";
var key = 'ThisIsSecretEncryptionKey';
var encloginId = CryptoJS.TripleDES.encrypt(loginId, key);
Encryption works fine in JS file.
Now I have to decrypt on server side using Java code. Below is the code for decryption:
public static
String KEY_STRING =''ThisIsSecretEncryptionKey";
public DESedeEncryption() throws Exception
{
myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
keyAsBytes = KEY_STRING.getBytes(UNICODE_FORMAT);
myKeySpec = new DESedeKeySpec(keyAsBytes);
mySecretKeyFactory = SecretKeyFactory.getInstance(myEncryptionScheme);
cipher = Cipher.getInstance(myEncryptionScheme);
key = mySecretKeyFactory.generateSecret(myKeySpec);
}
public String encrypt(String unencryptedString) {
String encryptedString = null;
System.out.println("Encrypt method call="+unencryptedString);
try {
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
byte[] encryptedText = cipher.doFinal(plainText);
BASE64Encoder base64encoder = new BASE64Encoder();
encryptedString = base64encoder.encode(encryptedText);
} catch (Exception e) {
e.printStackTrace();
}
return encryptedString;
}
I tried generating key using salt and IvParameterSpec and tried with HexBinary too. But nothing work.
I am getting exception as: javax.crypto.BadPaddingException: Given final block not properly padded.
Please help in resolving this issue or any other way to do it.
Thanks in advance.