0

I am trying to use a SSO solution in C#, where the documentation is only available in PHP.

I have this PHP Code:

function encrypt ($message)
{
    $initialVector = "1234567890123456";

    $key = md5($this->apiPassword);
    $crypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $message, MCRYPT_MODE_CFB, $initialVector);

    return base64_encode($initialVector) .":" . base64_encode($crypt);
}

The C# Code I tried is the following:

private string encrypt(string message)
{
  RijndaelManaged aes128 = new RijndaelManaged();
  aes128.BlockSize = 128;
  aes128.KeySize = 128;
  aes128.Mode = CipherMode.CFB;
  aes128.Padding = PaddingMode.None;
  aes128.IV = Encoding.ASCII.GetBytes("1234567890123456");
  aes128.Key = Encoding.ASCII.GetBytes(getMd5(key));

  byte[] plainTextBytes = Encoding.ASCII.GetBytes(json);

  ICryptoTransform encryptor = aes128.CreateEncryptor();
  MemoryStream ms = new MemoryStream();

  CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
  cs.Write(plainTextBytes, 0, plainTextBytes.Length);

  // convert our encrypted data from a memory stream into a byte array.
  byte[] cypherTextBytes = ms.ToArray();

  // close memory stream
  ms.Close();

  return Convert.ToBase64String(aes128.IV) + ":" + Convert.ToBase64String(cypherTextBytes);
}

key and message are identical. The IV part is returned correctly, only the encrypted parts are not equal. The md5 method is also working correctly.

Edit: Changing the Padding also doesn't change anything.

Dominik G
  • 1,459
  • 3
  • 17
  • 37
  • 2
    Have you tried to change the feedback size (segment size (shift register size))? CFB is a set of modes which is parametrized by the feedback size. I think, .Net uses the block size, but mcrypt uses a byte. – Artjom B. Mar 21 '17 at 19:13
  • Thanks @Artjom, setting `aes128.FeedbackSize` to 8 helped. – Dominik G Mar 22 '17 at 07:21

0 Answers0