首页 > 解决方案 > 使用 AES 的 Rails 加密,过于复杂

问题描述

我在加密来自我正在使用的第三方供应商的值时遇到问题。

他们的指令如下:

1) Convert the encryption password to a byte array.
2) Convert the value to be encrypted to a byte array.
3) The entire length of the array is inserted as the first four bytes onto the front 
   of the first block of the resultant byte array before encryption.
4) Encrypt the value using AES with:
        1. 256-bit key size,
        2. 256-bit block size, 
        3. Encryption Mode ECB, and
        4. an EMPTY initialization vector.
5) After encryption, you should now have a byte array that holds the encrypted value. 
6) Convert each byte to a HEX format and string all the HEX values together.
7) The final result is a string of HEX values. This is the final encrypted value to be passed. 
   The length of the final value will always be an even number.

EXAMPLE:
Given the following input values:
plainText: 2017/02/07 22:46
secretKey: ABCD1234FGHI5678
The following string will be produced:
D6281D5BE6CD6E79BB41C039F4DD020FBEC9D290AD631B2598A6DFF55C68AD04

到目前为止我尝试过的...

plain_text = "2017/02/07 22:46"
secret_key = "ABCD1234FGHI5678"

plain_text_byte_array = plain_text.bytes
plain_text_byte_array.unshift(0).unshift(0).unshift(0).unshift(16) # I found a Java example in their documentation and this is what they do. They prepend their byte array with 16, 0, 0, 0
secret_byte_array = secret_key.bytes
secret_byte_array = secret_byte_array.concat([0, 0, 0,...]) # also from their java example, they append the secret_byte array with 16 0's in order to get its length to 32

cipher = OpenSSL::Cipher::AES256.new(:ECB)
cipher.key = secret_byte_array.pack("C*")
encrypted = cipher.update(plain_text_byte_array.pack("C*")) + cipher.final

p encrypted.unpack("H*").first.to_s.upcase

# Result is: 
#    "84A0E5DCA7D704C41332F86E707DDAC244A1A87C38A906145DE4060D2BC5C8F4"

如您所见,我的结果与实际结果不符,应该是“D6281D5BE6CD6E79BB41C039F4DD020FBEC9D290AD631B2598A6DFF55C68AD04”

有谁知道我是否遗漏了什么或做一些奇怪的事情。他们的指示对我来说很难解析,所以也许我遗漏了一些东西。感谢您提供的任何帮助!(我在上面看到的内容上尝试了很多不同的变化)。我只需要一些指导,或者至少需要有人告诉我,我不会因为不理解他们的指示而疯狂。

标签: rubyencryptionopensslaesecb

解决方案


我设法重现了他们的结果——他们使用的过程非常复杂,而且远非优雅。我附上了实现其结果所需步骤的更具描述性的解释,以及我曾经这样做的 C# 源代码。

  1. 将密码转换为字节数组。 字节数组的长度必须是 32 字节,如果密码不够长,应该用 0 字节右填充。因此,他们的十六进制编码密码变为4142434431323334464748493536373800000000000000000000000000000000.

  2. 将要加密的值转换为字节数组。 这个很简单,用 UTF-8 编码就行了。

  3. 在加密之前,数组的整个长度作为前四个字节插入到结果字节数组的第一个块的前面。这是愚蠢的,没有任何用处,而是将步骤 2 中的字节数组的长度作为无符号 32 位整数并转换为小端字节数组。将此前缀添加到步骤 2 中的数组。

  4. 使用 AES 加密值。嗯。不,不要那样做。使用Rijndael加密值,使用 256 位块大小、256 位密钥大小、ECB 模式和零填充。

  5. 其余的很简单,只需将加密结果转换为十六进制即可。

我用来实现此结果的代码如下,在 C# 中。我不太了解Ruby,抱歉。

    // 1. Convert the encryption password to a byte array.
    byte[] passwordBytesOriginal = Encoding.UTF8.GetBytes("ABCD1234FGHI5678");
    byte[] passwordBytes = new byte[32];
    Array.Copy(passwordBytesOriginal, 0, passwordBytes, 0, passwordBytesOriginal.Length);


    // 2. Convert the value to be encrypted to a byte array.
    byte[] valueBytes = Encoding.UTF8.GetBytes("2017/02/07 22:46");

    // 3. The entire length of the array is inserted as the first four bytes onto the front 
    // of the first block of the resultant byte array before encryption.
    byte[] valueLengthAsBytes = BitConverter.GetBytes((uint)valueBytes.Length);
    byte[] finalPlaintext = new byte[valueBytes.Length + valueLengthAsBytes.Length];
    Array.Copy(valueLengthAsBytes, 0, finalPlaintext, 0, valueLengthAsBytes.Length);
    Array.Copy(valueBytes, 0, finalPlaintext, valueLengthAsBytes.Length, valueBytes.Length);

    // 4. Encrypt the value using AES...
    byte[] ciphertext;
    using (RijndaelManaged rijn = new RijndaelManaged())
    {
        rijn.BlockSize = 256;
        rijn.KeySize = 256;
        rijn.Key = passwordBytes;
        rijn.Mode = CipherMode.ECB;
        rijn.Padding = PaddingMode.Zeros;

        var encryptor = rijn.CreateEncryptor();
        ciphertext = encryptor.TransformFinalBlock(finalPlaintext, 0, finalPlaintext.Length);
    }

    // 5., 6., 7...
    string result = BitConverter.ToString(ciphertext).Replace("-", "").ToUpper();
    Console.WriteLine(result); // D6281D5BE6CD6E79BB41C039F4DD020FBEC9D290AD631B2598A6DFF55C68AD04

推荐阅读