Skip to content

Instantly share code, notes, and snippets.

@oniryx
Created March 14, 2012 14:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save oniryx/2036829 to your computer and use it in GitHub Desktop.
Save oniryx/2036829 to your computer and use it in GitHub Desktop.
Encrypt and Decrypt data in PHP compatible with C# mechanism
<?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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment