我可以为 OpenSSL 3des 设置密钥长度吗?

Can I set key length for OpenSSL 3des?

我想做这些

  require "openssl"
  decipher = OpenSSL::Cipher::Cipher.new('des3')
  decipher.decrypt
  decipher.key = "11111111"

但它抛出 OpenSSL::Cipher::CipherError: key length too short

我试图通过 des.key_len = 8 设置密钥长度,它抛出 OpenSSL::Cipher::CipherError: invalid key length 错误。

来自 OpenSSL ruby 文档:

key_length = integer → integer

Sets the key length of the cipher. If the cipher is a fixed length cipher then attempting to set the key length to any value other than the fixed value is an error.

Under normal circumstances you do not need to call this method (and probably shouldn’t).

See EVP_CIPHER_CTX_set_key_length for further information.

并且由于Triple Des 具有固定密钥长度(168位~24字节)的密码,你会得到一个错误.