public
Created

Encrypt and Decrypt data in PHP compatible with C# mechanism

  • Download Gist
encrypt_decrypt.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php
// Resource: http://stackoverflow.com/questions/4329260/cross-platform-php-to-c-sharp-net-encryption-decryption-with-rijndael
 
$iv = "45287112549354892144548565456541";
$key = "anjueolkdiwpoida";
 
$clear = "2310296|340105";
$encrypted = "B/DrahtonRfOMOgkCTcZRcuOdlpc68uKrNp9oCBpchY=";
 
$block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
$padding = $block - (strlen($clear) % $block);
$text = $clear.str_repeat(chr($padding), $padding);
 
$enc = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv);
$enc64=base64_encode($enc);
var_dump($enc64);
var_dump($enc64===$encrypted);
 
$_enc = base64_decode($enc64);
$dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $_enc, MCRYPT_MODE_CBC, $iv);
$dec = rtrim($dec, chr($padding));
var_dump($dec);
var_dump($dec===$clear);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.