OpenSSL :: Cipher :: CipherError:错误的最终块长度

时间:2016-08-19 07:39:17

标签: ruby-on-rails ruby ruby-on-rails-4 openssl caesar-cipher

source = ... # encrypted string - Base64.encode64(string)
cipher = OpenSSL::Cipher::Cipher.new('AES-128-ECB')
cipher.decrypt
cipher.key = ['YOUR_CHANNEL_SECRET'].pack('H*')
decoded = Base64.decode64(source)
decrypted = cipher.update(decoded) + cipher.final

对于YOUR_CHANNEL_SECRET 我将我的密钥转换为十六进制十进制字符串

参考 - https://developers.line.me/in_app_web/api-reference#get_token_access_token

我在 cipher.final 上收到错误,我不知道为什么会出现此错误以及解决方案是什么?

我遵循在参考链接中定义但仍然出错的相同内容。

1 个答案:

答案 0 :(得分:2)

相反:

source = ... # encrypted string - Base64.encode64(string)
cipher = OpenSSL::Cipher::Cipher.new('AES-128-ECB')
cipher.decrypt
cipher.key = ['YOUR_CHANNEL_SECRET'].pack('H*')
decoded = Base64.decode64(source)
decrypted = cipher.update(decoded) + cipher.final

我这样做:

def decrypt(encrypted_data, key, iv=nil, cipher_type="AES-256-CBC")
  cipher = OpenSSL::Cipher::Cipher.new(cipher_type)
  cipher.decrypt
  cipher.key = key
  cipher.iv = iv if iv != nil
  cipher.update([encrypted_data].pack("H*")) + cipher.final
end