PHP and C# common encryption and decryption function class, Use of 3DES encryption and decryption

PHP and C# common encryption and decryption function class, Use of 3DES encryption and decryption:

PHP encryption and decryption functions:


function encrypt($string) {
        //Key
        $key = "xxxxxxxx";

        //Encryption
        $cipher_alg = MCRYPT_TRIPLEDES;

        $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,MCRYPT_MODE_ECB), MCRYPT_RAND); 

        $encrypted_string = mcrypt_encrypt($cipher_alg, $key, $string, MCRYPT_MODE_ECB, $iv);
        return base64_encode($encrypted_string);
        return $encrypted_string;
    }

    function decrypt($string) {
            $string = base64_decode($string);

            //key
            $key = "xxxxxxxx";

            $cipher_alg = MCRYPT_TRIPLEDES;

            $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,MCRYPT_MODE_ECB), MCRYPT_RAND); 

            $decrypted_string = mcrypt_decrypt($cipher_alg, $key, $string, MCRYPT_MODE_ECB, $iv);
            return trim($decrypted_string);
    }

C# encryption types:


using System;
using System.Security.Cryptography;
using System.Text;

public class Crypto3DES
{
    public Crypto3DES()
    {

    }

    private System.Text.Encoding encoding;

    public string Key
    {
        get
        {
            return "xxxxxxxx";
        }
    }

    public System.Text.Encoding Encoding
    {
        get
        {
            if( encoding == null )
            {
                encoding = System.Text.Encoding.UTF8;
            }
            return encoding;
        }

        set
        {
            encoding = value;
        }
    }

    public string Encrypt3DES( string strString )
    {
        DESCryptoServiceProvider DES = new DESCryptoServiceProvider();

        DES.Key = Encoding.GetBytes( this.Key );
        DES.Mode = CipherMode.ECB;
        DES.Padding = PaddingMode.Zeros;

        ICryptoTransform DESEncrypt = DES.CreateEncryptor();

        byte[] Buffer = encoding.GetBytes(strString);

        return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
    }

    public string Decrypt3DES( string strString )
    {
        DESCryptoServiceProvider  DES = new DESCryptoServiceProvider ();

        DES.Key = Encoding.UTF8.GetBytes( this.Key );
        DES.Mode = CipherMode.ECB;
        DES.Padding = PaddingMode.Zeros;
        ICryptoTransform DESDecrypt = DES.CreateDecryptor();

        byte[] Buffer = Convert.FromBase64String(strString);
        return UTF8Encoding.UTF8.GetString( DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length) );
    }
}
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

4 Comments on "PHP and C# common encryption and decryption function class, Use of 3DES encryption and decryption"

commenter

Thanks a lot guy. Excellent code. Tried the code and works great. I want to mention that the encryption key needs to be 8 chars.

=)

commenter

Interesting;
i tried to build in the php code in my web app and my collegue build in the c# code. We are storing a password in a database.
Problem is that the encrypted password seem different. If c# app insert a password and i try with php to login or decrypt , I have no success.
any idea?

commenter

Thank you for the code. However, it doesn’t seem to work for me.
I copied the code exactly as is, replacing the key with a different 8-byte string. On the PHP side, the unencrypted string is garbled. I did verify that both sides (C# and PHP) are using the exact same key.

I’m using Visual Studio 2008 Professional Edition and PHP 5.1.6.

Thank you.

-Ray.

commenter

Hi buddies:

10x for da code, is so useful :D

I modified kinda the code to c#

Just u need send the message to encrypt and the key, remember that is only a 8bytes key

Is the only one line for each method:

public string Encrypt3DES(string mensaje, string key)
{
return Convert.ToBase64String((new DESCryptoServiceProvider { Key = Encoding.UTF8.GetBytes(key), Mode = CipherMode.ECB, Padding = PaddingMode.Zeros }.CreateEncryptor() as ICryptoTransform).TransformFinalBlock(Encoding.UTF8.GetBytes(mensaje), 0, Encoding.UTF8.GetBytes(mensaje).Length));
}

public string Decrypt3DES(string mensaje, string key)
{
return UTF8Encoding.UTF8.GetString((new DESCryptoServiceProvider { Key = Encoding.UTF8.GetBytes(key), Mode = CipherMode.ECB, Padding = PaddingMode.Zeros }.CreateDecryptor() as ICryptoTransform).TransformFinalBlock(Convert.FromBase64String(mensaje), 0, Convert.FromBase64String(mensaje).Length));
}

Ok, that’s all. Test it!

if you think that i’m wrong or something, send me a mail please :D

iekzaer@gmail.com

Best Regards

Alfred

Leave a Reply

Name:
Email:
Website:
Comment:
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>