【发布时间】:2019-06-24 19:38:23
【问题描述】:
我正在使用一个相当简单的加密/解密 Ruby 脚本,它似乎可以工作 - 但是解密位会破坏消息的前几个字节。我错过了什么?
代码如下:
key = OpenSSL::Random.random_bytes(16)
plain_text = "Some important txt we want to encrypt"
cipher = OpenSSL::Cipher::AES128.new(:CBC)
cipher.encrypt
cipher.key = key
cipher.random_iv
cipher_text = cipher.update(plain_text) + cipher.final
cipher = OpenSSL::Cipher::AES128.new(:CBC)
cipher.decrypt
cipher.key = key
cipher.random_iv
decrypted_plain_text = cipher.update(cipher_text) + cipher.final
puts "AES128 in CBC mode"
puts "Plain text: " + plain_text
puts "Cipher text: " + urlsafe_encode64(cipher_text)
puts "Decrypted plain text: " + decrypted_plain_text
结果:
AES128 in CBC mode Plain text: Some important txt we want to encrypt
Cipher text:
P2fdC7cApQvxHnfxSEfB2iJaueK3xRoj-NN3bDR8JheL_VPFYTDF_RxpLfBwoRfp
Decrypted plain text: �܇�Σ }w�D�A:xt we want to encrypt
【问题讨论】:
-
这里的每一行末尾都有大量的空格。有几个是可以理解的,但超过 190 个!
-
@tadman 已修复 :)
标签: ruby encryption openssl aes cbc-mode