Help with AES-128-ECB function

So I am trying to make a function that does the same as the following OpenSSL command:

openssl enc -d -in myFile -out decryptedoutput -aes-128-ecb -K 279977f62f6cfd2d91cd75b889ce0c9a -nosalt -md md5 

and this is the function I have written:

//CLIENT CODE (BTW the openFile function just grabs the bytes from the input file) byte[] AESKey = { 0x27, 0x99, 0x77, 0xf6, 0x2f, 0x6c, 0xfd, 0x2d, 0x91, 0xcd, 0x75, 0xb8, 0x89, 0xce, 0x0c, 0x9a }; byte[] AESContents = EncryptAES(openFile(), AESKey, false); //END OF CLIENT CODE static byte[] EncryptAES(byte[] message, byte[] key, bool doEncryption) { Aes aes = new AesManaged(); aes.Key = key; aes.Mode = CipherMode.ECB; aes.Padding = PaddingMode.Zeros; ICryptoTransform cipher; if (doEncryption == true) cipher = aes.CreateEncryptor(); else cipher = aes.CreateDecryptor(); // Problem Lies Here return cipher.TransformFinalBlock(message, 0, message.Length); } 

The only problem is that I keep getting an error saying ‘Length of the data to decrypt is invalid’ and when I try to modify the length by making the length component shorter, the output isn’t even remotely similar. I will mention that when I run the OpenSSL command with the file that I have in mind, I get this output:

bad decrypt 140126889710016:error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length:../crypto/evp/evp_enc.c:525: 

and then proceeds to give me the correct file output. It could be related to the error in the original case, but I don’t know how to work around it.

I’ll admit, I’m not very strong working with cryptographic algorithms so any help is appreciated!

submitted by /u/WormChickenWizard
[link] [comments]

Leave a Reply