在尝试设置一个可互操作的加密系统时,我在一个轻量级的“概念验证”中遇到了一个奇怪的情况。
我用 Ruby 编写了以下代码:
- 从我的文件系统上的虚拟文本文件创建加密文件
- 解密加密文件
- 与原始文件比较并检查它们是否相同
这是代码:
require 'openssl'
require 'base64'
# Read the dummy file
data = File.read("test.txt")
# Create an encrypter
cipher = OpenSSL::Cipher::AES.new(256, :CBC)
cipher.encrypt
key = "somethingreallyreallycomplicated"
cipher.key = key
# Encrypt and save to a file
encrypted = cipher.update(data) + cipher.final
open "encrypted.txt", "w" do |io| io.write Base64.encode64(encrypted) end
# Create a decrypter
decipher = OpenSSL::Cipher::AES.new(256, :CBC)
decipher.decrypt
decipher.key = key
# Decrypt and save to a file
encrypted_data = Base64.decode64(File.read("encrypted.txt"))
plain = decipher.update(encrypted_data) + decipher.final
open "decrypted.txt", "w" do |io| io.write plain end
# Compare original message and decrypted message
puts data == plain #=> true
一切正常,此脚本输出“true”
然后我尝试使用 openssl 命令行通过以下命令解密我的文件:
openssl aes-256-cbc -d -a -in encrypted.txt -k somethingreallyreallycomplicated
但我得到了:bad magic number
怎么来的?