Click here to Skip to main content
15,889,176 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
how can I encrypt or decrypt a text string using aes in .net that is equivalent to mysql_encrypt() and mysql_decrypt() ? .

I had simulated mysql_encrypt and mysql_decrypt using following code:

PHP
<?php
 // Key Function
 define('MY_KEY','keyfor$am@naT@480IsHere');
function mysql_aes_key($key)
{
    $new_key = str_repeat(chr(0), 16);
    for($i=0,$len=strlen($key);$i<$len;$i++)
    {
        $new_key[$i%16] = $new_key[$i%16] ^ $key[$i];
    }

    return $new_key;
}

//  AES Encryption
function aes_encrypt($val)
{
    $key = mysql_aes_key(MY_KEY);
    //echo $key gives     [, . Aam@naT@48
    $pad_value = 16-(strlen($val) % 16);
    $val = str_pad($val, (16*(floor(strlen($val) / 16)+1)), chr($pad_value));

    $encodedVal=mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $val, MCRYPT_MODE_ECB, mcrypt_create_iv( mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_DEV_URANDOM));
    //this is main incoding block  which take string and gives encoded byte
    //$encodedVal  encoded byte
    return base64_encode($encodedVal); //return $encodedVal as string
}


//  AES Decryption
function aes_decrypt($v)
{
    $key = mysql_aes_key(MY_KEY);
    $val=base64_decode((string)($v));  // $v is string , this convert string to byte

    $decodedVal = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $val, MCRYPT_MODE_ECB, mcrypt_create_iv( mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_DEV_URANDOM));

    //this is main incoding block  which take byte and gives decoded string
    return rtrim($decodedVal, "\0..\16");
}



?>



PHP
<?php
 $aesData=aes_encrypt("RajanAdhikari");
 $aesDataBack= aes_decrypt($aesData);


 $aesData2=aes_encrypt("Zoltan zorgo");
 $aesDataBack2= aes_decrypt($aesData2);

  echo "Static Key is ".MY_KEY ."<br>";
      //gives Static Key is keyfor$am@naT@480IsHere
  echo  "modified Key by mysql_aes_key is ".mysql_aes_key(MY_KEY) ."<br><br>";
      //gives modified Key by mysql_aes_key is [, . Aam@naT@48

  echo "Aes is   ".$aesData ."<br>";
      // gives  Aes is Vk5UxUzn5xAye5R/xqcvxA==
  echo "Data is   ".$aesDataBack ."<br> <br>";
      // gives Data is RajanAdhikari
  echo "Aes2 is   ".$aesData2 ."<br>";
      // gives  Aes is Vk5UxUzn5xAye5R/xqcvxA==
  echo "Data2 is   ".$aesDataBack2 ."<br>";
      // gives Data is RajanAdhikari
  ?>



Problem is i have encrypt some data by web (using php) and stored in mysql database.
now i have to fetch , decrypt and show that data in vb.net desktop application .

Now I have to get this in vb.net (or in c#.net).
how to achieve this?
is it possible? if then how?
thanks in advanced.
Posted
Updated 3-Jun-13 5:43am
v4

1 solution

I assume you didn't visit google in this topic... pity :(
http://www.dreamincode.net/forums/topic/156922-aes-encryption-and-c%23/[^]

[update]
This is working for me:
C#
string decrypt_function(byte[] Cipher_Text, byte[] Key)
        {
            RijndaelManaged Crypto = null;
            MemoryStream MemStream = null;
            ICryptoTransform Decryptor = null;
            CryptoStream Crypto_Stream = null;
            StreamReader Stream_Read = null;
             string Plain_Text;

             try
             {
                Crypto = new RijndaelManaged();
                Crypto.Key = Key;
		Crypto.Mode = CipherMode.ECB;

                MemStream = new MemoryStream(Cipher_Text);

                 //Create Decryptor make sure if you are decrypting that this is here and you did not copy paste encryptor.
                 Decryptor = Crypto.CreateDecryptor(Crypto.Key, Crypto.IV);

                 //This is different from the encryption look at the mode make sure you are reading from the stream.
                 Crypto_Stream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read);

                //I used the stream reader here because the ReadToEnd method is easy and because it return a string, also easy.
                 Stream_Read = new StreamReader(Crypto_Stream);
                 Plain_Text = Stream_Read.ReadToEnd();
             }
            finally
            {
                if (Crypto != null)
                    Crypto.Clear();

                MemStream.Flush();
                MemStream.Close();

            }
            return Plain_Text;
        }

byte[] mkey(string skey)
{
	Encoding winLatinCodePage = Encoding.GetEncoding(1252);
	byte[] key = Encoding.UTF8.GetBytes(skey);
	byte[] k = new byte[16] {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	for(int i=0;i<key.Length;i++)
	{
	k[i%16] = (byte)(k[i%16] ^ key[i]);
	}
		
	return k;
}


void Main()
{
string sk="keyfor$am@naT@480IsHere";
string enc="Vk5UxUzn5xAye5R/xqcvxA==";
string dec = decrypt_function(System.Convert.FromBase64String(enc), mkey(sk));

dec.Dump();
}

This is a linqpad test, but the code is there. I had done a little modification to the code from the link to: I have changed the mode.
 
Share this answer
 
v3
Comments
Surendra Adhikari SA 2-Jun-13 6:29am    
I have already Googled about it . and tried verious methods but i get different output on vb.net then of php code stated above .Thanks for the link , but this did not solved my scenario .can you give some detail as of mine scenario.
Zoltán Zörgő 2-Jun-13 6:32am    
Be aware, that .net is using unicode string by default, which is not valid for php! The easiest is to have utf-b all araoudn your application.
Surendra Adhikari SA 2-Jun-13 6:39am    
i agree with you as ,i am getting different output then of my php function on .net with different solution on this problem (even using same key and text).
Data is already encrypted by php function above and i cant change that. So i must replicate the function on vb.net to fetch and display that data.
What to Do then??
Waiting for any Solution ....Thanks In Advance.
Zoltán Zörgő 2-Jun-13 8:39am    
Well, first of all, tell me what is the encoding you have used in the php file? Can you give me a pair of plain and encrypted data in your system - and of course the key you have used for that calculation?
The problem is that you have made some auxiliary processing too...
Zoltán Zörgő 3-Jun-13 5:35am    
You have solved it? I hope you didn't misunderstood me: I don't want your production key, just a proper set of key, unencrypted and encrypted data from your environment to be able to verify your results.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900