AES128-CBC "bad magic number" and "input file for reading errors"

I am trying to decrypt a file ( part444.txt ) with the message:

 y2EdLtmNQsZkvwwf8jf3fM6c1thfzF0sQfblayGIBik= 

This is base64 encoded text under 128 bit AES in CBC mode. It is not supplemented. IV is the first 16 bytes of the ciphertext, and the key is h4ckth1sk3yp4d16 .

I know that people got a bad number of mages error from problems with Base64, but now I get a "read error input file" and am not sure where to go from here.

I tried:

 openssl enc -base64 -d part444.txt | openssl aes-128-cbc -d -k h4ckth1sk3yp4d16 

Why am I encountering "bad magic number" and "error reading input" errors?

+6
source share
1 answer

This is a kind of pain associated with openssl, because openssl encryption makes assumptions about filling in and derives the salt key from the entered password, which you should intentionally disable.

This is much easier to do in python using PyCrypto , where these assumptions are not fulfilled.

 >>> import base64 >>> data = base64.b64decode('y2EdLtmNQsZkvwwf8jf3fM6c1thfzF0sQfblayGIBik=') >>> from Crypto.Cipher import AES >>> aes_crypter = AES.new('h4ckth1sk3yp4d16', AES.MODE_CBC, data[:16]) >>> aes_crypter.decrypt(data[16:]) # this gives the encrypted secret. 

This can be done using openssl, but you have to read the data encoded in base64 - take out the first 16 bytes and remember it as your $IV (after encoding it back to the hex that openssl expects), start by reading all the bytes after the first 16 and remember it as $CIPHERTEXT (and say transcode to base64). Similarly for $KEY , you must convert it from ASCII to bytes in hexadecimal format. Assuming you saved them in variables, the following will work:

 IV=`base64 -d part444.txt | xxd -p -l 16` CIPHERTEXT=`base64 -d part444.txt | cut -b 17- | base64` KEY=`echo -n h4ckth1sk3yp4d16 |xxd -p` echo $CIPHERTEXT | openssl aes-128-cbc -d -a -nopad -K $KEY -iv $IV && echo "" 

Note base64 -d decodes base64 into a binary file (using base64 from GNU coreutils, when replacing BSD with base64 -d ), base64 b64 encodes binary data, cut -b 17- reads from the 17th byte of data to the end of the file and xxd -p converts binary to hexadecimal.

+6
source

Source: https://habr.com/ru/post/1011575/


All Articles