0
votes

I have a problem with OpenSSL on PHP und the equivalent part for c# AES. I wrote a php script to encrypt and decrypt with OpenSSL. Everything works fine. I can encrypt a text and later decrypt it without a problem. Now i need this function to work for c#. After searching the internet for some time I got finally a version of my code, where the result looked at last like my php result. But if I try to decode the text I got from my c# program and insert it in my php script, it cant be decoded. I hope you can help me.

This is my php code(this works)

function encrypt_text($data,$key,$cipher,$options) {
  $encryption_key = base64_decode($key);
  $iv_length = openssl_cipher_iv_length($cipher);
  $iv = openssl_random_pseudo_bytes($iv_length);
  $raw_data = openssl_encrypt($data,$cipher,$encryption_key,$options,$iv);
  return base64_encode($raw_data . '::' . $iv);
}

function decrypt_text($data,$key,$cipher,$options) {
  $encryption_key = base64_decode($key);
  list($encrypted_data, $iv) = array_pad(explode('::', base64_decode($data), 2),2,null);
  return openssl_decrypt($encrypted_data, $cipher, $encryption_key, $options, $iv);
}

This is my C# code (that dont work)

public static string OpenSSLEncrypt(string text)
{
    Aes myAes = Aes.Create();
    RNGCryptoServiceProvider rngCSP = new RNGCryptoServiceProvider();
    Byte[] encrypted_key = Convert.FromBase64String(KEY);
    Byte[] iv_bytes = new byte[16];
    rngCSP.GetNonZeroBytes(iv_bytes);
    myAes.Key = encrypted_key;
    myAes.IV = iv_bytes;
    myAes.Mode = CipherMode.CBC;
    Byte[] encryption = EncryptStringToBytes(text, myAes.Key, myAes.IV, myAes.Mode);
    Byte[] seperator = Encoding.UTF8.GetBytes("::");
    Byte[] result = new byte[0];
    result = AddByteToArray(result, encryption);
    result = AddByteToArray(result, seperator);
    result = AddByteToArray(result, myAes.IV);
    return Convert.ToBase64String(result);
}

private static Byte[] AddByteToArray(Byte[] bArray, Byte[] newByte)
{
    byte[] newArray = new byte[bArray.Length + newByte.Length];
    bArray.CopyTo(newArray, 0);
    int i = 0;
    while (i <= newByte.Length -1)
    {
        newArray[bArray.Length + i] = newByte[i];
        i++;
    }
    return newArray;
}

private static Byte[] EncryptStringToBytes(string text, Byte[] key, Byte[] iv, CipherMode cipherMode)
{
    Byte[] encrypted;

    using (Aes aesAlg = Aes.Create())
    {
        aesAlg.Key = key;
        aesAlg.IV = iv;
        aesAlg.Mode = cipherMode;

        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

        using(MemoryStream msEncrypt = new MemoryStream())
        {
            using(CryptoStream csEncrypt = new CryptoStream(msEncrypt,encryptor,CryptoStreamMode.Write))
            {
                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                {
                    swEncrypt.Write(text);
                }
                encrypted = msEncrypt.ToArray();
            }
        }
    }
    return encrypted;
}