bash AES128-CBC“坏幻数”和“读取输入文件时出错”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40188082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:20:15  来源:igfitidea点击:

AES128-CBC "bad magic number" and "error reading input file"

bashencryptionopensslcryptographyaes

提问by Alex Park

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

我正在尝试part444.txt使用消息解密文件 ( ):

y2EdLtmNQsZkvwwf8jf3fM6c1thfzF0sQfblayGIBik=

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

这是在 CBC 模式下的 128 位 AES 下 base64 编码的加密文本。它没有填充。IV 是加密文本的前 16 个字节,密钥是h4ckth1sk3yp4d16.

I know that people received the bad magic number error from problems with Base64 but now I get the "error reading input file" and not sure where to go from here.

我知道人们从 Base64 的问题中收到了错误的幻数错误,但现在我收到了“读取输入文件时出错”的消息,并且不确定从哪里开始。

I have tried:

我试过了:

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

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

为什么我会遇到错误“坏幻数”“读取输入文件时出错”

回答by dr jimbob

This is sort of a pain to do with openssl, because openssl's encryption makes assumptions about padding and deriving a salted key from the entered password that you have to deliberately turn off.

这对 openssl 来说有点痛苦,因为 openssl 的加密假设了关于填充和从输入的密码中派生出加盐密钥的假设,您必须故意关闭该密码。

It's much easier to do in python with say PyCrypto, where these assumptions aren't made.

在 python 中使用PyCrypto更容易做,其中没有做出这些假设。

>>> 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.

It is possible to do this with openssl, but you have to read the base64 encoded data -- take out the first 16 bytes and remember it as your $IV(after encoding it back to hex that openssl expects), start reading all the bytes after the first 16 and remember it as the $CIPHERTEXT(and say re-encode in base64). Similar for the $KEY, you have to convert it from ASCII to bytes in hex. Assuming you stored these in variables, then the following would work:

可以使用 openssl 来做到这一点,但是您必须读取 base64 编码的数据——取出前 16 个字节并将其记住为您的$IV(在将其编码回 openssl 期望的十六进制之后),然后开始读取所有字节第一个 16 并记住它$CIPHERTEXT(并说在 base64 中重新编码)。与 类似$KEY,您必须将其从 ASCII 转换为十六进制字节。假设您将这些存储在变量中,那么以下将起作用:

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 -ddecodes base64 to binary (using base64 from GNU coreutils; on BSD replace with base64 -D), base64b64 encodes binary data, cut -b 17-reads from the 17th byte of data to the end of the file, and xxd -pconverts binary to hex.

Note 将base64 -dbase64 解码为二进制(使用来自 GNU coreutils 的 base64;在 BSD 上替换为base64 -D),base64b64 编码二进制数据,cut -b 17-从数据的第 17 个字节读取到文件末尾,xxd -p并将二进制转换为十六进制。