你好,我在使用数组作为密钥和ruby-mcrypt gem值加密时遇到问题。gem允许我使用数组进行键fine,cipher = Mcrypt.new("rijndael-256", :ecb, secret)
工作但当我尝试加密时它会给我一个错误。我试过很多东西,但没有运气。有人知道mcrypt不喜欢用数组加密吗?
require 'mcrypt'
def encrypt(plain, secret)
cipher = Mcrypt.new("rijndael-256", :ecb, secret)
cipher.padding = :zeros
encrypted = cipher.encrypt(plain)
p encrypted
encrypted.unpack("H*").first.to_s.upcase
end
array_to_encrypt = [16, 0, 0, 0, 50, 48, 49, 55, 47, 48, 50, 47, 48, 55, 32, 50, 50, 58, 52, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
key_array = [65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56]
result = encrypt(array_to_encrypt, key_array)
p "RESULT IS #{result}"
输出如下:
Mcrypt::RuntimeError: Could not initialize mcrypt: Key length is not legal.
我在
ruby-mcrypt
gem中跟踪到了here这个错误,但还不太理解它,无法理解为什么会收到错误消息。任何帮助或洞察都将是惊人的谢谢! 最佳答案
库不支持数组。您需要使用字符串代替:
def binary(byte_array)
byte_array.pack('C*')
end
array_to_encrypt = [16, 0, 0, 0, 50, 48, 49, 55, 47, 48, 50, 47, 48, 55, 32, 50, 50, 58, 52, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
key_array = [65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56]
result = encrypt(binary(array_to_encrypt), binary(key_array))
p "RESULT IS #{result}"
关于ruby - ruby-mcrypt为什么不接受数组作为键?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54011153/