Bash - OpenSSL 解密返回“错误的幻数”文件从十六进制转换后出错

发布于 2025-01-11 12:05:43 字数 1358 浏览 0 评论 0原文

我有一个十六进制格式的文件,我正在尝试使用 OpenSSL 对其进行解密。但是,将文件从十六进制转换回二进制并输入正确的密码后,我收到一个 bad magic number 错误。我在 Debian Linux 上使用 bash。

我尝试使用一个简单的文件重现该错误,以下是我采取的步骤:

  1. 将文本写入文件:nano test.txt 并写入hello world

  2. 使用 OpenSSL 加密文件:openssl enc -aes-256-cbc -md sha512 - pbkdf2 -iter 100000 -salt -in test.txt -out test.data 密码 1234

  3. 删除原文件:rm test.txt

  4. 使用OpenSSL解密test.dataopenssl enc -d -aes-256-cbc -md sha512 -pbkdf2 -iter 100000 -salt -in test.data -out test.txt 密码 1234

  5. 确认以上内容按预期工作(此处没有问题)

  6. test.data 转换为十六进制:cat test.data |十六进制转储-C | tee test.hex

    (这就是我的原始文件的转换方式,所以这就是我的数据现在的格式)

  7. test.hex 转换回二进制:xxd -r -p test.hex test.data.copy

  8. 尝试解密 test.data.copy< /代码>:<代码>openssl enc -d -aes-256-cbc -md sha512 -pbkdf2 -iter 100000 -salt -in test.data.copy -out test.txt,密码为1234。在这里,我收到 bad magic number 错误,并看到解密的文件是空白的

编辑:感谢@Barmar,我确实看到这些文件彼此不同。所以看起来错误是在使用 cat 输出二进制文件时出现的。知道了这一点,还有什么办法还能恢复原来的文件吗?

我这里哪里出错了?是否在我不知道的情况下在某个地方插入了一些额外的字节?由于我只有十六进制文件而不是原始文件,我应该怎么做才能确保它被正确解密?谢谢

I have a file in hex format that I am trying to decrypt using OpenSSL. However, after converting the file from hex back to binary, and after typing in the correct password, I receive a bad magic number error. I'm using bash on Debian Linux.

I've tried to reproduce the error with a simple file and here's the steps I've taken:

  1. Write text to file: nano test.txt and write hello world

  2. Encrypt file with OpenSSL: openssl enc -aes-256-cbc -md sha512 -pbkdf2 -iter 100000 -salt -in test.txt -out test.data with password 1234

  3. Delete original file: rm test.txt

  4. Decrypt test.data with OpenSSL: openssl enc -d -aes-256-cbc -md sha512 -pbkdf2 -iter 100000 -salt -in test.data -out test.txt with password 1234

  5. Confirm that the above works as expected (no problems here)

  6. Convert test.data to hex: cat test.data | hexdump -C | tee test.hex

    (This is how my original file was converted, so this is the format my data is in now)

  7. Convert test.hex back to binary: xxd -r -p test.hex test.data.copy

  8. Try to decrypt test.data.copy: openssl enc -d -aes-256-cbc -md sha512 -pbkdf2 -iter 100000 -salt -in test.data.copy -out test.txt with password 1234. Here I receive the bad magic number error and see that the decrypted file is blank

Edit: Thanks to @Barmar, I do see that the files are different from each other. So it looks like the error was in using cat to output the binary. Knowing this, is there any way to still recover the original file?

Where did I go wrong here? Were there some extra bytes inserted somewhere along the way that I'm not aware of? Since I only have the hex file and not the original, what should I do to make sure it gets properly decrypted? Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世晨晓 2025-01-18 12:05:43

感谢 @GordonDavisson 向我展示问题出在使用 hexdump -C 上。

正如他在评论中提到的,使用 hexdump -C 会向文件添加一堆额外数据,xxd -r -p 将包含在生成的输出文件中。

我只是删除了每行开头的行号和末尾的 ASCII 表示形式。然后,我删除了新行和双空格以获得纯十六进制文件。获得纯十六进制后,我能够使用相同的 xxd -r -p 命令转换为二进制,并使用 openssl 成功解密。

感谢您的所有帮助,否则我永远不会弄清楚

Thanks to @GordonDavisson for showing me that the problem was with using hexdump -C.

As he mentioned in his comment, using hexdump -C adds a bunch of extra data to the file which xxd -r -p will include in the resulting output file.

I simply removed the line numbers at the beginning of each line and the ASCII representation at the end. I then removed new lines and double whitespaces to get a plain hex file. After getting plain hex, I was able to convert to binary using the same xxd -r -p command and successfully decrypt using openssl.

Thanks for all the help, I would never have figured it out otherwise

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文