将C#加密算法转换为Ruby

时间:2015-11-24 07:55:32

标签: c# ruby encryption aes

我在c#中有加密算法,我需要将它移植到ruby。

private string Encrypt(string clearText)
{
    string EncryptionKey = "ENC_KEY";
    byte[] clearBytes = Encoding.Unicode.GetBytes(clearText); 
    using (Aes encryptor = Aes.Create())
    {
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x1, 0x2, 0x3, 0x4, 0x5, 0x5, 0x5, 0x6, 0x7, 0x8, 0x9, 0x10, 0x11 });
        encryptor.Key = pdb.GetBytes(32);
        encryptor.IV = pdb.GetBytes(16);
        using (MemoryStream ms = new MemoryStream()) {
        using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
        {
            cs.Write(clearBytes, 0, clearBytes.Length); cs.Close();
        }
        clearText = Convert.ToBase64String(ms.ToArray()); }
    }
    return clearText;
}

据我所知,alghorithm生成AES密钥,并且iv加密并返回为64字符串。

我没有找到Rfc2898DeriveBytes的确切替代品,我使用了PBKDF2 Gem。这是我的红宝石方法:

def self.encrypt clear_text
  iterations = 1000
  encryption_key = 'EncryptionKey'
  clearBytes = clear_text.encode( 'UTF-16LE' ).bytes.to_a
  enc_bytes = [0x1, 0x2, 0x3, 0x4, 0x5, 0x5, 0x5, 0x6, 0x7, 0x8, 0x9, 0x10, 0x11]
  salt = enc_bytes.pack('C*')
  derived_a = PBKDF2.new do |p|
    p.password = encryption_key
    p.salt = salt
    p.iterations = iterations
    p.key_length = 32
  end

  derived_b = PBKDF2.new do |p|
    p.password = encryption_key
    p.salt = salt
    p.iterations = iterations
    p.key_length = 16
  end

  key = derived_a.bin_string
  # iV = derived_b.bin_string
  iV_a = iV_a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] # Static iV
  iV = iV_a.pack('C*')


  cipher = OpenSSL::Cipher::AES256.new(:CBC)
  cipher.encrypt
  cipher.key = key
  cipher.iv = iV
  encrypted = cipher.update(clear_text) + cipher.final
  Base64.encode64(encrypted)
end

我的代码中有2个问题。我无法在iv上获得相同的值,如果我将其用作静态值,则返回值不匹配。

我没有太多的c#经验。我错过了什么?

2 个答案:

答案 0 :(得分:2)

看起来您的PBKDF2生成算法具有固定输入,因此生成的KeyIV应该始终相同。

我刚刚在我的机器上运行了C#代码,以输出KeyIV的值。它给了我:

takKsX7IBXq3R0Q5GWgJo/XhhEHDNfRFxSVru12vtU4=
y/lm9eKzBJTMdU+uA6GlXA==

分别为KeyIV的Base64编码值。您可以在Ruby代码中使用这些值,这样就无需使用PBKDF2 gem继续生成这些值。

所以这个Ruby代码

clear_text = 'HELLO WORLD'
cipher = OpenSSL::Cipher::AES256.new(:CBC)
cipher.encrypt
cipher.key = Base64.decode64('takKsX7IBXq3R0Q5GWgJo/XhhEHDNfRFxSVru12vtU4=')
cipher.iv = Base64.decode64('y/lm9eKzBJTMdU+uA6GlXA==')
clearBytes = clearText.encode('UTF-16LE')
encrypted = cipher.update(clearBytes)
encrypted << cipher.final
puts Base64.encode64(encrypted)

将输出与Encrypt("HELLO WORLD")

相同的内容

答案 1 :(得分:1)

对于初学者,您有不同的加密密钥

string EncryptionKey = "ENC_KEY";
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x1, 0x2, 0x3, 0x4, 0x5, 0x5, 0x5, 0x6, 0x7, 0x8, 0x9, 0x10, 0x11 });

不同于:

encryption_key = 'EncryptionKey'
...
derived_a = PBKDF2.new do |p|
    p.password = encryption_key
    p.salt = salt
    p.iterations = iterations
    p.key_length = 32
  end

如果你真的使用相同的“一切”,你可能需要确保你的密码在KDF之前是相同的:)

此外,不要忘记pack your IV in with your ciphertext,因为你不希望那个(或你的盐)是静态的,然后加密MAC,那么好的东西:)