代码之家  ›  专栏  ›  技术社区  ›  Olly

如何解决OpenSSL的弃用警告::Cipher::Cipher#encrypt

  •  3
  • Olly  · 技术社区  · 16 年前

    我刚刚将Mac升级到Snow Leopard,并启动并运行了Rails环境。除了OSX之外,与我之前安装的唯一区别是我现在运行的是 ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0] (雪豹默认)而不是1.8.6。

    warning: argumtents for OpenSSL::Cipher::Cipher#encrypt and OpenSSL::Cipher::Cipher#decrypt were deprecated; use OpenSSL::Cipher::Cipher#pkcs5_keyivgen to derive key and IV

    导致第4行出现这些警告的代码示例(它解码了一个加密字符串):

    1. def decrypt(data)
    2.  encryptor = OpenSSL::Cipher::Cipher.new('DES-EDE3-CBC')
    3.  key = "my key"
    4.  encryptor.decrypt(key)
    5.  text = encryptor.update(data)
    6.  text << encryptor.final
    7. end
    

    我很难理解如何解决这个问题,而谷歌并没有真正提供帮助。我是否应该尝试降级到Ruby 1.8.6(如果是这样,最好的方法是什么?),我是否应该试着隐藏警告(把头埋在沙子里?!),或者我可以在代码中应用一个简单的修复程序?

    2 回复  |  直到 16 年前
        1
  •  4
  •   ZZ Coder    16 年前

    你的例子正好说明了问题所在。三重DES需要24字节的密钥材料(包括奇偶校验),但您只提供了6字节。您的密钥材料将被重复以弥补不足,从而导致密钥安全性降低。

    Ruby提供了以下示例代码。 pass 是您的密钥,您可以使用任何硬编码值 salt .

    puts "--Encrypting--"
    des = OpenSSL::Cipher::Cipher.new(alg)
    des.pkcs5_keyivgen(pass, salt)
    des.encrypt
    cipher =  des.update(text)
    cipher << des.final
    puts %(encrypted text: #{cipher.inspect})
    puts
    
    puts "--Decrypting--"
    des = OpenSSL::Cipher::Cipher.new(alg)
    des.pkcs5_keyivgen(pass, salt)
    des.decrypt
    out =  des.update(cipher)
    out << des.final
    puts %(decrypted text: "#{out}")
    puts
    
        2
  •  1
  •   stderr    16 年前

    ZZ Coder很近,但没有雪茄。事实上,你应该 从不

    puts "--Encrypting--"
    des = OpenSSL::Cipher::Cipher.new(alg)
    des.encrypt
    des.pkcs5_keyivgen(pass, salt)
    cipher =  des.update(text)
    cipher << des.final
    puts %(encrypted text: #{cipher.inspect})
    puts
    

    puts "--Decrypting--"
    des = OpenSSL::Cipher::Cipher.new(alg)
    des.decrypt
    des.pkcs5_keyivgen(pass, salt)  
    out =  des.update(cipher)
    out << des.final
    puts %(decrypted text: "#{out}")
    puts
    
    推荐文章