Skip to content

@oniryx /encrypt_decrypt.php
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.