Migrating C # .Net encryption / decryption algorithm to Ruby

I need to encrypt / decrypt data from a .NET based web service that uses the following code (based on Rijndael's algorithm ):

/**
 * 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("\0", "");
  } catch (Exception ex) {
    AddToFile("Error al desencriptar el valor: " + encryptedMessage + " con la clave especial: " + p_strSpecialKey + " " + ex.ToString());
  }

  return strDecrypt;
}

      

I researched a way to do the same in Ruby and I found this answer ( How to decode Rijndael in ruby ​​(coded in VB.net) ) but it doesn't work for me. Also, I'm not sure what value I should be using for iv. The webservice brings me a token that I have to use as a key (100% sure of that), but I really don't know what I'm missing.

The code I am using to encrypt:

# /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")

      

The encrypted serialized json data I have to send should look like a hex string like "f0997ddbb17b08913e00b6fb2541312c1cfdda85e555451a1832df076a5d4a5f7d81d8db92715eadebed8ea9695cf But the method described in the link generates an encrypted string like this "^ w \ x9A \ x90B \ xDC \ f \ x16 \ xB8 \ xBDt \ xFBo \ xD7r \ x97" ".

Any idea on what I am doing wrong or what I am missing?

UPDATE . I figured out that the string output I am getting is a byte array and I need to convert that byte array to a hex string. I am using the following aproach ( https://github.com/atomicobject/hex_string/blob/master/lib/hex_string.rb ). But, still not sure if Cypher is properly configured to act like C # code.

+3


source to share


1 answer


Finally, I found a library that matches the C # code exactly and ruby-mcrypt

( https://github.com/kingpong/ruby-mcrypt ). And the encryption / decryption code I used is this:

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

      



IMPORTANT: This Rjandael C # library does not use IV, for this reason I am giving nil parameter as IV for Mcrypt.

This is only the encryption / decryption section, I am also converting the string to hexa format. Hope this can help someone else!

0


source