2

I have a PHP decryption algorithm at one end, and a .Net encryption algorithm at another. I am using mcrypt_decrypt for decrypt purpose in PHP code, and looking for an equivalent of mcrypt_encrypt in .Net. More specifically, the PHP code that I want to convert to .Net is the following:

$cipher_alg = MCRYPT_RIJNDAEL_128;
$iv = mcrypt_create_iv ( mcrypt_get_iv_size ( $cipher_alg, MCRYPT_MODE_ECB ), MCRYPT_RAND );

$encrypted_string = mcrypt_encrypt ( $cipher_alg, $sessionkey, $string, MCRYPT_MODE_CBC, $iv );
$hex_encrypted_string = bin2hex ( $encrypted_string );

Is there an equivalent of mcrypt_encrypt in .Net?

1
  • mcrypt_create_iv() + MCRYPT_MODE_ECB is an expensive NOP. Commented Feb 24, 2016 at 21:04

2 Answers 2

3

I have come out with a solution in my blog.

Here is an excerpt :

    private static string CreateEncryptedString(string myString, string hexiv, string key)
    {
        RijndaelManaged alg = new RijndaelManaged();
        alg.Padding = PaddingMode.Zeros;
        alg.Mode = CipherMode.CBC;
        alg.BlockSize = 16 * 8;
        alg.Key = ASCIIEncoding.UTF8.GetBytes(key);
        alg.IV = StringToByteArray(hexiv);
        ICryptoTransform encryptor = alg.CreateEncryptor(alg.Key, alg.IV);

        MemoryStream msStream = new MemoryStream();
        CryptoStream mCSWriter = new CryptoStream(msStream, encryptor, CryptoStreamMode.Write);
        StreamWriter mSWriter = new StreamWriter(mCSWriter);
        mSWriter.Write(myString);
        mSWriter.Flush();
        mCSWriter.FlushFinalBlock();

        var EncryptedByte = new byte[msStream.Length];
        msStream.Position = 0;
        msStream.Read(EncryptedByte, 0, (int)msStream.Length);

        return ByteArrayToHexString(EncryptedByte);

    }

    public static byte[] StringToByteArray(String hex)
    {
        int NumberChars = hex.Length;
        byte[] bytes = new byte[NumberChars / 2];
        for (int i = 0; i < NumberChars; i += 2)
            bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
        return bytes;
    }

    public static string ByteArrayToHexString(byte[] ba)
    {
        StringBuilder hex = new StringBuilder(ba.Length * 2);
        foreach (byte b in ba)
            hex.AppendFormat("{0:x2}", b);
        return hex.ToString();
    }

There is one thing to note. The key size must be 16. Which means that key parameter must be a string with 16 characters. Or else the encryption won't work and will throw a CryptographicException.

On the PHP end, here's how you do the decoding:

$cipher_alg = MCRYPT_RIJNDAEL_128;
$decrypted_string = mcrypt_decrypt($cipher_alg, $key, 
$encrypted_string , MCRYPT_MODE_CBC, trim(hex2bin(trim($hexiv))));
Sign up to request clarification or add additional context in comments.

Comments

1

There isn't a direct equivalent, but 128-bit Rijndael is supported (and probably most other ciphers that you would want). See the Rijndael class for details and an example.

Basically the encryption part of the framework is based on you specifying an appropriate instance of a base class (CryptoStream, SymmetricAlgorithm etc) rather than specifying a cipher by name - but you should be able to do what you need to.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.