I have a method that was implemented on JAVA, but I need it in Apex

public static String decryptAccountInformation(String websitekey, String document,String encryptedAccount) {
    String key = document+websitekey;
    try {
        byte[] keyBytes = MessageDigest.getInstance("MD5").digest(key.getBytes(UTF_8));
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, "AES"),new IvParameterSpec(keyBytes));
        byte[] val = cipher.doFinal(DatatypeConverter.parseHexBinary(encryptedAccount));
        return new String(val,UTF_8);
    } catch (Exception e) {
        throw new RuntimeException("Exception decrypting : " + encryptedAccount, e);

I tried the following

String key = '1537883301DFA543EFEB4CE1D461C251D74CA1D4BB01EB47A3C7C577D831BCD18FB8FC0';
String encryptedAccount = '7641B3781B8166E0A5DCA3A4A0AA9D9EF7CD3969AE18FB84221F6BC830DBB03C';
Blob blobKey = Blob.valueOf(key);
Blob blobKey16 = Crypto.generateDigest('MD5', blobKey);
Blob encryptedBlob = EncodingUtil.base64Decode(encryptedAccount);
Blob decryptedBlob = Crypto.decryptWithManagedIV('AES128', blobKey16, encryptedBlob);
String decryptedString = decryptedBlob.toString();

But received error: pad block corrupted

Can anyone help?

  • Can you share a sample websitekey , document and encryptedAccount? – Nagendra Singh Nov 27 '20 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.