将C#加密算法转换为Ruby

将C#加密算法转换为Ruby,c#,ruby,encryption,aes,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

嗨,我在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,并进行加密,然后作为base64字符串返回

我没有找到Rfc2898DeriveBytes的确切替代方案,我使用了。下面是我的ruby方法:

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
我的代码中有两个问题。我无法在iv上获得相同的值,如果我将其用作静态值,则返回值不匹配


我没有太多的经验。我遗漏了什么?

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

我刚刚在修改后的机器上运行了C代码,以输出
IV
的值。它给了我:

takKsX7IBXq3R0Q5GWgJo/XhhEHDNfRFxSVru12vtU4=
y/lm9eKzBJTMdU+uA6GlXA==
分别作为
IV
的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)
clear_text='HELLO WORLD'
cipher=OpenSSL::cipher::AES256.new(:CBC)
加密
cipher.key=Base64.decode64('takKsX7IBXq3R0Q5GWgJo/xhehdnfrfxsvru12vtu4=')
cipher.iv=Base64.decode64('y/lm9eKzBJTMdU+uA6GlXA==')
clearBytes=clearText.encode('UTF-16LE')
加密=密码。更新(clearBytes)

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

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:)


另外,不要忘记,因为你不希望它(或你的salt)是静态的,加密然后MAC,所有这些好东西:)

MSDN说getBytes函数会更改每次调用的密钥大小。我的意思是,如果你调用getBytes(10),两次,就等同于调用getBytes(10+10)或直接调用getBytes(20)。对于这个问题有一些解决方法,你可以在第一次调用时生成一个较长的键,然后将其分为派生键和IV。如果你没有太多C#经验,我建议你不要“翻译”代码,而是从头开始用Ruby编写。如果你更熟悉Ruby,这可能比试图破译一种未知的语言要容易得多。比你要容易得多。当我将键设置为基64字符串时,问题就解决了。