Ruby on Rails 中的 DES3 解密

发布于 2024-08-18 00:25:44 字数 527 浏览 2 评论 0原文

我的 RoR 服务器接收一个字符串,该字符串在 C++ 应用程序中使用 des3 和 base64 编码进行加密。

密码对象的创建方式如下:

cipher = OpenSSL::Cipher::Cipher::new("des3")
cipher.key = key_str
cipher.iv =  iv_str

key_str 和 iv_str:是加密算法的密钥和初始化向量的字符串表示形式。它们对于 RoR 和 C++ 应用程序是相同的。

RoR 端的代码如下:

result = ""
result << cipher.update( Base64.decode64(message) )
result << cipher.final

执行最后一行代码后,出现异常

OpenSSL::CipherError (bad decrypt)

这里出了什么问题?有什么想法吗?

My RoR server receives a string, that was encrypted in C++ application using des3 with base64 encoding

The cipher object is created so:

cipher = OpenSSL::Cipher::Cipher::new("des3")
cipher.key = key_str
cipher.iv =  iv_str

key_str and iv_str: are string representations of key and initialization vector for encryption algorithm. They are the same for RoR and C++ application.

The code on the RoR side is following:

result = ""
result << cipher.update( Base64.decode64(message) )
result << cipher.final

After executing the last line of code, i get an exception

OpenSSL::CipherError (bad decrypt)

What is wrong here ? Any ideas ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睡美人的小仙女 2024-08-25 00:25:44

OpenSSL::Cipher 指出:

在使用以下任何一项之前,请确保调用 .encrypt.decrypt
方法:

  • [key=iv=random_keyrandom_ivpkcs5_keyivgen ]

在您的具体情况下,省略对 cipher.decrypt 的调用会导致出现错误解密错误,如您所见。

以下示例纠正了该问题并显示了预期的行为:

require 'openssl'
require 'Base64'

# For testing purposes only!
message = 'MyTestString'
key = 'PasswordPasswordPassword'
iv = '12345678'

# Encrypt plaintext using Triple DES
cipher = OpenSSL::Cipher::Cipher.new("des3")
cipher.encrypt # Call this before setting key or iv
cipher.key = key
cipher.iv = iv
ciphertext = cipher.update(message)
ciphertext << cipher.final

puts "Encrypted \"#{message}\" with \"#{key}\" to:\n\"#{ciphertext}\"\n"

# Base64-encode the ciphertext
encodedCipherText = Base64.encode64(ciphertext)

# Base64-decode the ciphertext and decrypt it
cipher.decrypt
plaintext = cipher.update(Base64.decode64(encodedCipherText))
plaintext << cipher.final

# Print decrypted plaintext; should match original message
puts "Decrypted \"#{ciphertext}\" with \"#{key}\" to:\n\"#{plaintext}\"\n\n"

The documentation for OpenSSL::Cipher states:

Make sure to call .encrypt or .decrypt before using any of the following
methods:

  • [key=, iv=, random_key, random_iv, pkcs5_keyivgen]

In your specific case, omitting the call to cipher.decrypt causes a bad decrypt error, as you've seen.

The following example corrects that problem and exhibits the expected behavior:

require 'openssl'
require 'Base64'

# For testing purposes only!
message = 'MyTestString'
key = 'PasswordPasswordPassword'
iv = '12345678'

# Encrypt plaintext using Triple DES
cipher = OpenSSL::Cipher::Cipher.new("des3")
cipher.encrypt # Call this before setting key or iv
cipher.key = key
cipher.iv = iv
ciphertext = cipher.update(message)
ciphertext << cipher.final

puts "Encrypted \"#{message}\" with \"#{key}\" to:\n\"#{ciphertext}\"\n"

# Base64-encode the ciphertext
encodedCipherText = Base64.encode64(ciphertext)

# Base64-decode the ciphertext and decrypt it
cipher.decrypt
plaintext = cipher.update(Base64.decode64(encodedCipherText))
plaintext << cipher.final

# Print decrypted plaintext; should match original message
puts "Decrypted \"#{ciphertext}\" with \"#{key}\" to:\n\"#{plaintext}\"\n\n"
音栖息无 2024-08-25 00:25:44
gem install encryptor

它包装了标准 Ruby OpenSSL 库并允许您使用它的任何算法。

require 'encryptor'
Base64.decode64(message).decrypt(:algorithm => 'des', :key => key, :iv => iv)
gem install encryptor

It wraps the standard Ruby OpenSSL library and allows you to use any of its algorithms.

require 'encryptor'
Base64.decode64(message).decrypt(:algorithm => 'des', :key => key, :iv => iv)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文