1

I get a problems with encryption from JavaScript and Decryption to Java

Here is my JAVA CODE

public class AES {

private static SecretKeySpec secretKey;
private static byte[] key;

public static void setKey(String myKey)
{
    MessageDigest sha = null;
    try {
        key = myKey.getBytes("UTF-8");
        sha = MessageDigest.getInstance("SHA-1");
        key = sha.digest(key);
        key = Arrays.copyOf(key, 16);
        secretKey = new SecretKeySpec(key, "AES");
    } catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}

public static String decrypt(String strToDecrypt, String secret)
{
    try
    {
        setKey(secret);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
    }
    catch (Exception e)
    {
        System.out.println("Error while decrypting: " + e.toString());
    }
    return null;
}

Here is my JavaScript Code

 set(keys, value) {
varkey = CryptoJS.enc.Utf8.parse(keys);
var iv = CryptoJS.enc.Utf8.parse(keys);
var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(value.toString()), key,
{
    keySize: 16,
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
});

return encrypted.toString();

}

I got message error form JAVA

Error while decrypting: javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.

Please help me for this case. Thank before hand

Khong Minh
  • 23
  • 9
  • probably the encoding ... base64 or a hex string .. make sure the encoding is what you expect – Jaromanda X Dec 24 '19 at 10:05
  • sir so base on my code above could you give any example for that? really appreciate your respond. – Khong Minh Dec 24 '19 at 10:12
  • I don't do java, so I can't help with that ... what encoding does java use? base64? hex string? some obscure java only encoding? just check the encoding, make sure it's the same – Jaromanda X Dec 24 '19 at 10:14
  • JAVA AES use with encode base64. anyways thank you sir I will recheck with encoding type again. – Khong Minh Dec 24 '19 at 10:20
  • The two codes are functionally different: encryption uses the CBC mode (requires an IV) and decryption uses the ECB mode (doesn't require an IV). Switch to CBC for decryption and use the IV that was also used for encryption. In addition, the key is determined in the Java code using the SHA1 digest, which isn't used in the JavaScript code. – Topaco Dec 24 '19 at 10:26
  • Thank you sir so I need to remove sha1 and change JAVA code to CBC mode right ? – Khong Minh Dec 24 '19 at 10:38
  • If the JavaScript-code is the reference, then yes. Both codes must use the _same_ key, _same_ mode, and _same_ IV (if the mode requires an IV, such as CBC). Otherwise the decryption fails. – Topaco Dec 24 '19 at 11:44
  • Thank you so much finally I found this useful link to implement and it worked fine. https://stackoverflow.com/questions/41432896/cryptojs-aes-encryption-and-java-aes-decryption – Khong Minh Dec 25 '19 at 04:40

0 Answers0