如何存储尝试在 ruby 中加密时创建的随机密钥和 iv?
How to store the random key and iv created when trying to encrypt in ruby?
我目前正在进行一个项目,我将在该项目中将用户数据存储在文本文件中。我想使用 Ruby 中的 OpenSSL 库加密这些文件。我能够使用随机创建的密钥和 iv 值来加密文件。由于程序不会 运行 在服务器上,而是在本地,我想用密码导出这些值,以便不同的方法可以解密信息。
require "openssl"
cipher = OpenSSL::Cipher::AES256.new(:CBC)
cipher.encrypt
key = cipher.random_key
iv = cipher.random_iv
encrypted = cipher.update("test.txt") + cipher.final
File.open("temp.txt", "w+") do |file|
file.write(encrypted)
end
如果我不使用随机密钥,它不会加密信息或 returns 错误。
'update': key not set (OpenSSL::Cipther::CipherError)
当我尝试使用导出命令(使用随机密钥)时,我也收到一条错误消息,指出它不是一种方法。
'export' for #<string:0x00000000065659b8> (NoMethodError)
看来您没有开发加密方案的经验,所以我建议您不要自己实现。内置的 OpenSSL 库不能防止您犯安全错误。正如您所注意到的,它没有提供有关如何安全处理密钥和 iv 的指导(如果操作不当将是不安全的)。
我推荐使用 rbnacl
gem,它有:
The RbNaCl::SimpleBox
class provides a simple, easy-to-use cryptographic API where all of the hard decisions have been made for you in advance. If you're looking to encrypt something, and you don't know much about cryptography, this is probably what you want to be using.
这是一个完整的端到端示例,输出到磁盘并从磁盘加载:
require 'rbnacl'
generated_key = RbNaCl::Random.random_bytes(RbNaCl::SecretBox.key_bytes)
box = RbNaCl::SimpleBox.from_secret_key(generated_key)
ciphertext = box.encrypt('plaintext')
# save both artifacts to disk
File.open('cipher.bin', 'w') { |f| f.write(ciphertext) }
File.open('key.bin', 'w') { |f| f.write(generated_key) }
# decrypt by reading artifacts from disk
# this could be done in separate program
ciphertext_from_file = File.read('cipher.bin', mode: 'rb') # r -> "read", b -> "binary"
key_from_file = File.read('key.bin', mode: 'rb')
regenerated_box = RbNaCl::SimpleBox.from_secret_key(key_from_file)
regenerated_box.decrypt(ciphertext_from_file)
# => 'plaintext'
# cleanup
File.delete('cipher.bin') if File.exist?('cipher.bin')
File.delete('key.bin') if File.exist?('key.bin')
请注意,generated_key
和 ciphertext
都是二进制字符串。如果您不想保存原始二进制文件,请使用 Base64.encode64
或 Digest.hexencode
之类的方式将它们转换为可打印字符。这样,它们将是可打印的,copy/paste-able,并且您不需要使用 'rb'
.
执行“读取二进制文件”
我目前正在进行一个项目,我将在该项目中将用户数据存储在文本文件中。我想使用 Ruby 中的 OpenSSL 库加密这些文件。我能够使用随机创建的密钥和 iv 值来加密文件。由于程序不会 运行 在服务器上,而是在本地,我想用密码导出这些值,以便不同的方法可以解密信息。
require "openssl"
cipher = OpenSSL::Cipher::AES256.new(:CBC)
cipher.encrypt
key = cipher.random_key
iv = cipher.random_iv
encrypted = cipher.update("test.txt") + cipher.final
File.open("temp.txt", "w+") do |file|
file.write(encrypted)
end
如果我不使用随机密钥,它不会加密信息或 returns 错误。
'update': key not set (OpenSSL::Cipther::CipherError)
当我尝试使用导出命令(使用随机密钥)时,我也收到一条错误消息,指出它不是一种方法。
'export' for #<string:0x00000000065659b8> (NoMethodError)
看来您没有开发加密方案的经验,所以我建议您不要自己实现。内置的 OpenSSL 库不能防止您犯安全错误。正如您所注意到的,它没有提供有关如何安全处理密钥和 iv 的指导(如果操作不当将是不安全的)。
我推荐使用 rbnacl
gem,它有:
The
RbNaCl::SimpleBox
class provides a simple, easy-to-use cryptographic API where all of the hard decisions have been made for you in advance. If you're looking to encrypt something, and you don't know much about cryptography, this is probably what you want to be using.
这是一个完整的端到端示例,输出到磁盘并从磁盘加载:
require 'rbnacl'
generated_key = RbNaCl::Random.random_bytes(RbNaCl::SecretBox.key_bytes)
box = RbNaCl::SimpleBox.from_secret_key(generated_key)
ciphertext = box.encrypt('plaintext')
# save both artifacts to disk
File.open('cipher.bin', 'w') { |f| f.write(ciphertext) }
File.open('key.bin', 'w') { |f| f.write(generated_key) }
# decrypt by reading artifacts from disk
# this could be done in separate program
ciphertext_from_file = File.read('cipher.bin', mode: 'rb') # r -> "read", b -> "binary"
key_from_file = File.read('key.bin', mode: 'rb')
regenerated_box = RbNaCl::SimpleBox.from_secret_key(key_from_file)
regenerated_box.decrypt(ciphertext_from_file)
# => 'plaintext'
# cleanup
File.delete('cipher.bin') if File.exist?('cipher.bin')
File.delete('key.bin') if File.exist?('key.bin')
请注意,generated_key
和 ciphertext
都是二进制字符串。如果您不想保存原始二进制文件,请使用 Base64.encode64
或 Digest.hexencode
之类的方式将它们转换为可打印字符。这样,它们将是可打印的,copy/paste-able,并且您不需要使用 'rb'
.