The requirements for input to the block encipher functions (AES, DES, Triple DES and Blowfish) are quite strict.
Input Data: Input data to the block encipher functions in ECB or CBC mode
must be an exact multiple of the block length (16 bytes for AES, 8 bytes for the others)
or the functions will return an error. It is the user's responsibility to provide padding where necessary and
to remove the padding after decryption. Failure to do this will result in a BAD_LENGTH_ERROR
:-
Data not a valid length Input not multiple of 8 bytes long Input not multiple of block size
See Padding for more guidance on preparing your plaintext for encryption.
This rule on input lengths does not apply to the file encryption functions which automatically provide their own padding.
Keys and IV: All the keys and initialization vectors provided in hex format must be of the exact required length
or the function will return a BAD_KEY_LEN_ERROR
or BAD_IV_ERROR
:-
Invalid key length Invalid initialization vector Invalid IV length
There are no checks made on the lengths of keys and IVs provided in byte format for most of the block cipher functions; it is assumed you have done it correctly. So please make sure you provide byte arrays of the correct lengths. The result is "undefined behaviour" if you don't, where "undefined behaviour" has the lovely meaning used in the ANSI standard for C; namely, it might be ignored, do something unpredicatable, or your program may crash. "Nuffink to do wiv us, mate." Either way, you will not get the output you expected.
Note also that the CNV_BytesFromHexStr
function will - by design - filter invalid hex characters and return
the resulting bytes from whatever is left without error. The hex versions of the encryption functions are stricter and will
fail if any invalid hex characters are found in the input parameters.
See Hexadecimal versus Bytes for more details of the hex conversion functions.