Ruby openssl 中的 AES 等效项?

发布于 2024-08-16 08:35:53 字数 954 浏览 3 评论 0原文

Gibberish 库提供了一个很好的 CBC 算法...

// In Jascascript
GibberishAES.enc("Made with Gibberish\n", "password");
// Outputs: "U2FsdGVkX1+21O5RB08bavFTq7Yq/gChmXrO3f00tvJaT55A5pPvqw0zFVnHSW1o"

# On the command line
echo "U2FsdGVkX1+21O5RB08bavFTq7Yq/gChmXrO3f00tvJaT55A5pPvqw0zFVnHSW1o" | 
  openssl enc -d -aes-256-cbc -a -k password

我怎样才能在 ruby​​ 中进行解密?直接的方法行不通...

require 'openssl'

def aes(m,k,t)
  (aes = OpenSSL::Cipher::Cipher.new('aes-256-cbc').send(m)).key = k
  aes.update(t) << aes.final
end

def encrypt(key, text)
  aes(:encrypt, key, text)
end

def decrypt(key, text)
  aes(:decrypt, key, text)
end

def p k
  Digest::SHA256.digest(k) ## what goes here???
end

require 'base64'
def t x
  ## also tried.. simply returning x...
  Base64.decode64(x)      
end


text = "U2FsdGVkX1+21O5RB08bavFTq7Yq/gChmXrO3f00tvJaT55A5pPvqw0zFVnHSW1o"
pass = "password"

decrypt(p(pass), t(text))

Gibberish library provides a nice CBC algo...

// In Jascascript
GibberishAES.enc("Made with Gibberish\n", "password");
// Outputs: "U2FsdGVkX1+21O5RB08bavFTq7Yq/gChmXrO3f00tvJaT55A5pPvqw0zFVnHSW1o"

# On the command line
echo "U2FsdGVkX1+21O5RB08bavFTq7Yq/gChmXrO3f00tvJaT55A5pPvqw0zFVnHSW1o" | 
  openssl enc -d -aes-256-cbc -a -k password

How can I do this decryption in ruby? The straightforward way doesn't work...

require 'openssl'

def aes(m,k,t)
  (aes = OpenSSL::Cipher::Cipher.new('aes-256-cbc').send(m)).key = k
  aes.update(t) << aes.final
end

def encrypt(key, text)
  aes(:encrypt, key, text)
end

def decrypt(key, text)
  aes(:decrypt, key, text)
end

def p k
  Digest::SHA256.digest(k) ## what goes here???
end

require 'base64'
def t x
  ## also tried.. simply returning x...
  Base64.decode64(x)      
end


text = "U2FsdGVkX1+21O5RB08bavFTq7Yq/gChmXrO3f00tvJaT55A5pPvqw0zFVnHSW1o"
pass = "password"

decrypt(p(pass), t(text))

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

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

发布评论

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

评论(2

神妖 2024-08-23 08:35:53

深入研究乱码...提供了答案的线索。以及为什么传统机制不起作用。

dec = function(string, pass) {
    // string, password in plaintext
    var cryptArr = Base64.decode(string),
    salt = cryptArr.slice(8, 16),
    pbe = openSSLKey(s2a(pass), salt),
    key = pbe.key,
    iv = pbe.iv;
    cryptArr = cryptArr.slice(16, cryptArr.length);
    // Take off the Salted__ffeeddcc
    string = rawDecrypt(cryptArr, key, iv);
    return string;
},

转换为红宝石现在相当微不足道..记下它以供我个人将来参考。

require 'base64'
require 'openssl'

def decode(k,t)
  cryptArr = Base64.decode64(t)
  salt     = cryptArr[8..15]
  data     = cryptArr[16..-1] 

  aes = OpenSSL::Cipher::Cipher.new('AES-256-CBC').decrypt
  aes.pkcs5_keyivgen(k, salt, 1)
  s = aes.update(data) + aes.final
end

orig = "Made with Gibberish\n"
cipr = "U2FsdGVkX1+21O5RB08bavFTq7Yq/gChmXrO3f00tvJaT55A5pPvqw0zFVnHSW1o"
pass = "password"

puts decode(pass, cipr)

Digging into Gibberish code... provides the clues to the answers. and why the traditional mechanism does not work.

dec = function(string, pass) {
    // string, password in plaintext
    var cryptArr = Base64.decode(string),
    salt = cryptArr.slice(8, 16),
    pbe = openSSLKey(s2a(pass), salt),
    key = pbe.key,
    iv = pbe.iv;
    cryptArr = cryptArr.slice(16, cryptArr.length);
    // Take off the Salted__ffeeddcc
    string = rawDecrypt(cryptArr, key, iv);
    return string;
},

Converting to ruby is now fairly trivial.. noting it down for my personal future reference.

require 'base64'
require 'openssl'

def decode(k,t)
  cryptArr = Base64.decode64(t)
  salt     = cryptArr[8..15]
  data     = cryptArr[16..-1] 

  aes = OpenSSL::Cipher::Cipher.new('AES-256-CBC').decrypt
  aes.pkcs5_keyivgen(k, salt, 1)
  s = aes.update(data) + aes.final
end

orig = "Made with Gibberish\n"
cipr = "U2FsdGVkX1+21O5RB08bavFTq7Yq/gChmXrO3f00tvJaT55A5pPvqw0zFVnHSW1o"
pass = "password"

puts decode(pass, cipr)
一身骄傲 2024-08-23 08:35:53

我编写了最初的 Gibberish JS 库,最后我终于抽出时间来纠正 Ruby 中的情况。 Ryan Oberoi 的上述代码绝对正确,但我已经继续创建了一个 gem 来执行相同的操作。请访问 https://github.com/mdp/gibberish

I wrote the original Gibberish JS library, and I finally got around to rectifying the situation in Ruby. The above code from Ryan Oberoi is absolutely correct, but I've gone ahead and created a gem to do the same thing. Check it out at https://github.com/mdp/gibberish

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文