将 C# .Net encrypt/decrypt 算法迁移到 Ruby

Migrating C# .Net encrypt/decrypt algorithm to Ruby

我必须 encrypt/decrypt 来自使用以下代码(基于 Rijndael 算法)的基于 .Net 的 Web 服务的数据:

/**
 * Cifra una cadena texto con el algoritmo de Rijndael
 *
 * @param   plainMessage    mensaje plano (sin cifrar)
 * @param   p_strSpecialKey key especial
 * @return  string          texto cifrado en hexadecimal
 */
public static string AES_encryptString(String plainMessage, string p_strSpecialKey) {
  string strTxtEncrypt = "";

  // Crear una instancia del algoritmo de Rijndael
  try {
    Rijndael RijndaelAlg = Rijndael.Create();
    RijndaelAlg.KeySize = 128;
    RijndaelAlg.Mode = CipherMode.ECB;
    RijndaelAlg.Padding = PaddingMode.Zeros;

    byte[] Key = Encoding.UTF8.GetBytes(p_strSpecialKey);
    byte[] IV = RijndaelAlg.IV;

    int keySize = 32;
    Array.Resize(ref Key, keySize);

    // Establecer un flujo en memoria para el cifrado
    MemoryStream memoryStream = new MemoryStream();

    // Crear un flujo de cifrado basado en el flujo de los datos
    CryptoStream cryptoStream = new CryptoStream(memoryStream,
    RijndaelAlg.CreateEncryptor(Key, IV),
    CryptoStreamMode.Write);

    // Obtener la representación en bytes de la información a cifrar
    byte[] plainMessageBytes = Encoding.UTF8.GetBytes(plainMessage);

    // Cifrar los datos enviándolos al flujo de cifrado
    cryptoStream.Write(plainMessageBytes, 0, plainMessageBytes.Length);
    cryptoStream.FlushFinalBlock();

    // Obtener los datos datos cifrados como un arreglo de bytes
    byte[] cipherMessageBytes = memoryStream.ToArray();

    // Cerrar los flujos utilizados
    memoryStream.Close();
    cryptoStream.Close();

    strTxtEncrypt = ByteToHex(cipherMessageBytes);
  } catch (Exception ex) {
    AddToFile("Error al encriptar el valor: " + plainMessage + " con la clave especial: " + p_strSpecialKey + " " + ex.ToString());
  }

  return strTxtEncrypt;
}

/**
 * Descifra una cadena texto con el algoritmo de Rijndael
 *
 * @param   encryptedMessage    mensaje cifrado en hexadecimal
 * @param   p_strSpecialKey key especial
 * @return  string              texto descifrado (plano)
 */
public static string AES_decryptString(String encryptedMessage, string p_strSpecialKey) {
  string strDecrypt = "";

  // Crear una instancia del algoritmo de Rijndael
  try {
    Rijndael RijndaelAlg = Rijndael.Create();
    RijndaelAlg.KeySize = 128;
    RijndaelAlg.Mode = CipherMode.ECB;
    RijndaelAlg.Padding = PaddingMode.Zeros;

    byte[] Key = Encoding.UTF8.GetBytes(p_strSpecialKey);
    byte[] IV = RijndaelAlg.IV;

    int keySize = 32;
    Array.Resize(ref Key, keySize);

    // Obtener la representación en bytes del texto cifrado
    byte[] cipherTextBytes = HexToByteArray(encryptedMessage);

    // Crear un arreglo de bytes para almacenar los datos descifrados
    byte[] plainTextBytes = new byte[cipherTextBytes.Length];

    // Crear un flujo en memoria con la representación de bytes de la información cifrada
    MemoryStream memoryStream = new MemoryStream(cipherTextBytes);

    // Crear un flujo de descifrado basado en el flujo de los datos
    CryptoStream cryptoStream = new CryptoStream(memoryStream,
    RijndaelAlg.CreateDecryptor(Key, IV),
    CryptoStreamMode.Read);

    // Obtener los datos descifrados obteniéndolos del flujo de descifrado
    int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);

    // Cerrar los flujos utilizados
    memoryStream.Close();
    cryptoStream.Close();

    // Retornar la representación de texto de los datos descifrados
    strDecrypt = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).Replace("[=12=]", "");
  } catch (Exception ex) {
    AddToFile("Error al desencriptar el valor: " + encryptedMessage + " con la clave especial: " + p_strSpecialKey + " " + ex.ToString());
  }

  return strDecrypt;
}

我研究了在 Ruby 上执行相同操作的方法,我找到了这个答案 (How to decode Rijndael in ruby (encoded in VB.net)),但它对我不起作用。另外,我不确定我必须为 iv 使用什么值。 Web 服务给我带来了一个令牌,我必须将其用作密钥(对此 100% 确定),但实际上我不知道我错过了什么。

我用于加密的代码是:

# /lib/crypt.rb
module Crypt
  ...
  def Crypt.encrypt(data, key, iv, cipher_type)
    aes = OpenSSL::Cipher::Cipher.new(cipher_type)
    aes.encrypt
    aes.key = key
    aes.iv = iv
    aes.update(data) + aes.final
  end
end

# ../ws.rb
...
token = "8c5d0e6b93cf5d38d7a076b2db35ee6d" #this is a one of the tokens i received
iv = token
Crypt.encrypt(a_serialized_json,token,iv,"AES-128-CBC")

我必须发送的加密序列化json数据必须像这样的十六进制字符串"f0997ddbb17b08913e00b6fb2541312c1cfdda85e555451a1832df076a5d4a5f7d81d8db92715eade144e9696dfbe9eea573baa8ea90cdbe5baadf32fdeb6db8c9ab6743f8eeeb508921999d7513fad3"。但是 link 上描述的方法生成了一个像这样的加密字符串 ""^w\x9A\x90B\xDC\f\x16\xB8\xBDt\xFBo\xD7r\x97"".

知道我做错了什么或遗漏了什么吗?

UPDATE:我发现我得到的字符串输出是一个字节数组,我必须将这个字节数组转换为十六进制字符串。我正在使用以下方法 (https://github.com/atomicobject/hex_string/blob/master/lib/hex_string.rb)。但是,仍然不确定 Cypher 是否正确配置为充当 C# 代码。

终于找到了一个完全符合C#代码的库,就是ruby-mcrypt(https://github.com/kingpong/ruby-mcrypt)。我使用的 encryption/decryption 代码是这样的:

require 'mcrypt'

module Crypt
  def Crypt.m_encrypt(data, key)
    crypto = Mcrypt.new(:rijndael_128, :ecb, key, nil, :zeros)
    encrypted_data = crypto.encrypt(data)
    encrypted_data
  end

  def Crypt.m_decrypt(data, key)
    crypto = Mcrypt.new(:rijndael_128, :ecb, key, nil, :zeros)
    crypto.decrypt(data)
  end
end

重要提示:此 C# Rjandael 库不使用 IV,因此我将 nil 参数作为 IV 提供给 Mcrypt。

这只是 encryption/decryption 部分,我还将字符串转换为十六进制格式。希望它可以帮助别人!