Archived Forum Post

Index of archived forum posts

Question:

AES Decrypt from Base64 where IV is Prepended

Apr 26 '16 at 15:59

We have the following situation. We need to AES decrypt from a base64 string, where the base64 (if decoded) is composed of the IV followed by the encrypted bytes. How can this be done simply?


Answer

The IV for AES is always 16 bytes in length. (The IV for any block encryption algorithm is always equal to the block size as defined by the algorithm. For AES, the block size is 16 bytes, regardless of key size.)

The simplest way to solve the problem is to use the Crypt2.ReEncode method to re-encode the input string from Base64 to Hex. This way, you can easily split the string into it's two parts: the IV and the encrypted data.

For example, in VBScript:

encryptedHex = crypt.ReEncode(encryptedBase64,"base64","hex")

Once you have the string in hex form, it is easy to split. The IV is the first 32 chars (each byte in a hex string is represented by 2 chars. For example, the hex string 01020304 represents 4 bytes: 1,2,3,4.) So.. write code to split the string, then:

  1. Set the IV by calling Crypt2.SetEncodedIV(ivHexStr,"hex")
  2. Set the Crypt2.EncodingMode = "hex"
  3. Decrypt by calling DecryptStringENC(encHexStr)