views:

253

answers:

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  A: 

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.

Jon Skeet
+1  A: 

I have come out with a solution in my blog.

Ngu Soon Hui