Tuesday, May 22, 2012

Encrypt by PHP & Decrypt by ASP.NET (C#) and vice versa

Ref: http://stackoverflow.com/questions/1278974/php-encryption-vb-net-decryption

php.ini, enable:
extension=php_mcrypt.dll

Here is my modified code:

PHP file:
<?php

function decryptRJ256($key,$iv,$string_to_decrypt)
{
    $string_to_decrypt = base64_decode($string_to_decrypt);
    $rtn = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $string_to_decrypt, MCRYPT_MODE_CBC, $iv);
    $rtn = rtrim($rtn, "\0\4");
    return($rtn);
}

function encryptRJ256($key,$iv,$string_to_encrypt)
{
    $rtn = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $string_to_encrypt, MCRYPT_MODE_CBC, $iv);
    $rtn = base64_encode($rtn);
    return($rtn);
}

$ky = 'lkirwf897+22#bbtrm8814z5qq=498j5'; // 32 * 8 = 256 bit key
$iv = '741952hheeyy66#cs!9hjv887mxx7@8y'; // 32 * 8 = 256 bit iv

$text = "Here is my data to encrypt !!!";

$etext = encryptRJ256($ky, $iv, $text);
$dtext = decryptRJ256($ky, $iv, $etext);

header('Location: http://localhost/test.aspx?t='.$etext);
?>

ASP.NET (C#):

public static string DecryptRJ256(string prm_key, string prm_iv, string prm_text_to_decrypt) {

var sEncryptedString = prm_text_to_decrypt;

var myRijndael = new RijndaelManaged() {
 Padding = PaddingMode.Zeros,
 Mode = CipherMode.CBC,
 KeySize = 256,
 BlockSize = 256
};

var key = Encoding.ASCII.GetBytes(prm_key);
var IV = Encoding.ASCII.GetBytes(prm_iv);

var decryptor = myRijndael.CreateDecryptor(key, IV);

var sEncrypted = Convert.FromBase64String(sEncryptedString);

var fromEncrypt = new byte[sEncrypted.Length];

var msDecrypt = new MemoryStream(sEncrypted);
var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);

csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

return (Encoding.ASCII.GetString(fromEncrypt));
}

public static string EncryptRJ256(string prm_key, string prm_iv, string prm_text_to_encrypt) {

var sToEncrypt = prm_text_to_encrypt;

var myRijndael = new RijndaelManaged() {
 Padding = PaddingMode.Zeros,
 Mode = CipherMode.CBC,
 KeySize = 256,
 BlockSize = 256
};

var key = Encoding.ASCII.GetBytes(prm_key);
var IV = Encoding.ASCII.GetBytes(prm_iv);

var encryptor = myRijndael.CreateEncryptor(key, IV);

var msEncrypt = new MemoryStream();
var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

var toEncrypt = Encoding.ASCII.GetBytes(sToEncrypt);

csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
csEncrypt.FlushFinalBlock();

var encrypted = msEncrypt.ToArray();

return (Convert.ToBase64String(encrypted));
}


protected void Page_Load(object sender, EventArgs e)
    {
//Shared 256 bit Key and IV here
string sKy = " lkirwf897+22#bbtrm8814z5qq=498j5 "; //32 chr shared ascii string (32 * 8 = 256 bit)
string sIV = " 741952hheeyy66#cs!9hjv887mxx7@8y "; //32 chr shared ascii string (32 * 8 = 256 bit)

if(Request.QueryString["t"]!=null)
{
string t = Request.QueryString["t"].ToString();
t = t.Trim().Replace(" ", "+");
Button1.Text = DecryptRJ256(sKy, sIV, t);
}
    }



No comments:

Post a Comment

Subscribe to RSS Feed Follow me on Twitter!