I am trying to decrypt the encrypted string from Rijndael VBA code. Java 8 code
public static void Decrypt() throws Exception{
String mydata = "3m/WeZ1cAUEqexeH64gPehkMdQSRvx7K9TKhtpUfEg==";
byte[] encryptedBytes = Base64.getDecoder().decode(mydata);
byte[] key = Base64.getDecoder().decode("VGhpcnR5VHdvQnl0ZXMzJFRoaXJ0eVR3b0J5dGVzMyQ=");
byte[] iv = Base64.getDecoder().decode("MyRUaHJlZVR3b0J5dGVzMzMkVGhyZWVUd29CeXRlczM=");
PaddedBufferedBlockCipher bufferedBlock = new PaddedBufferedBlockCipher(new CBCBlockCipher(new RijndaelEngine(256)), new PKCS7Padding());
CipherParameters keyAndIV = new ParametersWithIV(new KeyParameter(key), iv);
bufferedBlock.init(false, keyAndIV);
byte[] decryptedBytes = new byte[bufferedBlock.getOutputSize(encryptedBytes.length)];
int processed = bufferedBlock.processBytes(encryptedBytes, 0, encryptedBytes.length, decryptedBytes, 0);
processed += bufferedBlock.doFinal(decryptedBytes, processed);
System.out.println(new String(decryptedBytes, 0, processed, StandardCharsets.UTF_8));
}
Above code is giving me an error "last block incomplete in decryption" in line
processed += bufferedBlock.doFinal(decryptedBytes, processed);
This is the VBA Code encryption:
Function Encrypt(plaintext, aesKey)
Dim cipherBytes, aesKeyBytes, ivKeyBytes, plainBytes() As Byte
Dim utf8, AES, aesEnc, cipherMode As Object
Dim aesIV() As Byte
Set AES = CreateObject("System.Security.Cryptography.RijndaelManaged")
Set utf8 = CreateObject("System.Text.UTF8Encoding")
AES.KeySize = 256
AES.BlockSize = 256
'CipherMode.CBC
AES.Mode = 1
'PaddingMode.PKCS7
AES.Padding = 2
AES.Key = utf8.GetBytes_4("ThirtyTwoBytes3$ThirtyTwoBytes3$")
AES.IV = utf8.GetBytes_4("3$ThreeTwoBytes33$ThreeTwoBytes3")
plainBytes = utf8.GetBytes_4(plaintext)
'plainBytes = B64Decode(plaintext)
'Set aesEnc = AES.CreateEncryptor_2((aesKeyBytes), (ivKeyBytes))
cipherBytes = AES.CreateEncryptor().TransformFinalBlock((plainBytes), 0, UBound(plainBytes))
Encrypt = B64Encode(cipherBytes)
End Function
I am trying to encrypt the data sent by VBA and decrypt and use it. Help me in correcting the Java code to match the VBA code VBA AES CBC encryption