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

46 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

commenter

Thanks, the code works just great. Even it was my first code in C#, the code was simple and precise.

Best Regards

commenter

I have been surfing online more than 2 hours today, yet I never found any interesting article like yours.
It is pretty worth enough for me. In my opinion, if all website owners and bloggers made
good content as you did, the web will be much more useful
than ever before.

commenter

I have been used in your common encryption and decryption function class. its working perfectly. but when i am going to view and edit using decryption password shows junk character in CRT monitor but not is LCD monitor, how to resolve this.

if i give 8 character in my password its not come like this but i will give less then 8 character and more then 8 character it will come like

password: senthil
decryption password: senthil/0/

and password: senthilkumar
decryption password: senthilkumar/0/0/0/0/

how to resolve this.

commenter

excellent publish, very informative. I wonder why the opposite
experts of this sector don’t notice this. You should continue your writing. I am confident, you have a great readers’ base already!

commenter

The things everyone must know concerning background checks
and how to better prepare yourself for them.

commenter

I’ve been looking around for different coffee makers due to the fact that I want a new one, I stumbled upon your story and wanted to find out if you know precisely where I possibly could get a good price on the one you described?

commenter

Hello, after reading this remarkable article i am too glad to share my experience here with
colleagues.

commenter

Hello, Neat post. There is an issue together with your site in internet explorer, might check this?
IE still is the market chief and a huge section of other folks will miss
your magnificent writing due to this problem.

commenter

Please let me know if you’re looking for a article writer for your site. You have some really good articles and I think I would be a good asset. If you ever want to take some of the load off, I’d
love to write some content for your blog in exchange for a link back to mine.

Please shoot me an e-mail if interested. Thank you!

commenter

Its like you read my mind! You seem to know a lot about this,
like you wrote the book in it or something.

I think that you can do with a few pics to drive the message home a bit, but instead of that, this
is magnificent blog. A fantastic read. I will certainly be back.

commenter

It’s going to be ending of mine day, except before ending I am reading this wonderful piece of writing to increase my knowledge.

commenter

Greetings, There’s no doubt that your website could be having internet browser compatibility issues. When I look at your web site in Safari, it looks fine however, if opening in Internet Explorer, it’s got
some overlapping issues. I merely wanted to give you a quick heads up!

Apart from that, excellent website!

commenter
Emrah Sifoğlu | May 11th, 2013 at 9:17 am

I have looking for encryption and decryption functions.
Thank you very much!

commenter

As a cash loan lender, they understand that you appear
short from time for it to time. Likewise, lenders do not ask
one to fax any documents for validation and so the complete process
is hassle free and paperless.

commenter

It’s impressive that you are getting thoughts from this piece of writing as well as from our dialogue made at this place.

commenter

I am in fact glad to glance at this webpage posts which
carries lots of useful facts, thanks for providing these kinds of statistics.

My blog post: IT Support Letchworth

commenter

We are a bunch of volunteers and opening a new scheme in our community.
Your site provided us with helpful info to work on.
You have done a formidable task and our entire neighborhood will probably be
thankful to you.

commenter

Greetings I am so thrilled I found your website, I really found you by error, while
I was looking on Yahoo for something else, Regardless I am here now and would just
like to say thank you for a remarkable post
and a all round entertaining blog (I also love the theme/design),
I don’t have time to read through it all at the minute but I have book-marked it
and also added in your RSS feeds, so when I have time I will be
back to read a great deal more, Please do keep up the awesome b.

commenter

I’m not that much of a internet reader to be
honest but your sites really nice, keep it up! I’ll go ahead and bookmark your website to come back down the road. Many thanks

commenter

This website really has all the information I wanted concerning this subject and didn’t know who to ask.

My site; Obagi NUderm

commenter

Thanks for your personal marvelous posting! I quite enjoyed reading
it, you will be a great author. I will make certain to bookmark your
blog and definitely will come back in the foreseeable future.
I want to encourage one to continue your great job,
have a nice evening!

commenter

It’s hard to find experienced people for this topic, however, you seem like you know what you’re
talking about! Thanks

commenter

I needed to thank you for this very good read!! I absolutely enjoyed every little
bit of it. I’ve got you book-marked to check out new stuff you post…

commenter

I like the valuable info you provide for your articles.
I’ll bookmark your weblog and take a look at again here regularly. I am relatively sure I’ll
be told lots of new stuff right right here! Best of luck for the following!

commenter

Wow that was unusual. I just wrote an incredibly long comment but after I clicked submit my comment
didn’t appear. Grrrr… well I’m not writing
all that over again. Regardless, just wanted to say excellent blog!

commenter

you are truly a good webmaster. The web site loading speed
is amazing. It sort of feels that you’re doing any unique trick. Also, The contents are masterwork. you’ve performed a great process on this matter!

commenter

My brother suggested I might like this blog. He was totally right.
This post actually made my day. You can not imagine simply how much time I had spent for this information!
Thanks!

commenter

I think everything wrote was actually very logical. However,
think about this, what if you composed a catchier title?
I mean, I don’t want to tell you how to run your blog, but suppose you added a post title that grabbed folk’s attention?
I mean PHP and C# common encryption and decryption function
class, Use of 3DES encryption and decryption | Internet Resources | Blog.

950buy.com is a little vanilla. You should peek at Yahoo’s home page and watch how they create article headlines to grab people interested. You might add a video or a pic or two to grab people excited about what you’ve got to say.
Just my opinion, it would make your blog a little livelier.

commenter

Hello, all is going sound here and ofcourse every one is sharing information, that’s truly excellent, keep up writing.

commenter

I’m really impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you modify it yourself? Either way keep up the excellent quality writing, it is rare to see a great blog like this one these days.

commenter

Right now it looks like BlogEngine is the preferred blogging platform out there right now.
(from what I’ve read) Is that what you are using on your blog?

commenter

Anԁ if you ԁon’t want to leave the United States should repudiate the credit card consolidation loan in our society. Rights and Tools You May Need: For example, you have no idea what that means later.

my web page :: Seeing as chessthegame.com refers to

commenter

Pretty! This was an extremely wonderful article. Thank you for supplying this info.

commenter

The New Gucci Jackie Shoulder Large Handbag has a
rugged classic leather-based structure with an attractive type.
Their Pink Buckle Tote handbag will match all of
your summertime colors. There is magnetic snap closure that closes this designer handbag.

commenter

A motivating discussion is worth comment.
I do think that you should publish more about this subject matter, it may not be a taboo subject but typically folks don’t talk about these subjects. To the next! Many thanks!!

commenter

I like the valuable info you supply to your articles.
I’ll bookmark your blog and take a look at once more here regularly. I am reasonably sure I will learn lots of new stuff proper right here! Best of luck for the next!

commenter

Good web site you’ve got here.. It’s difficult to find excellent writing like yours nowadays.
I honestly appreciate people like you! Take care!!

commenter

For the reason that the admin of this website is working, no
hesitation very quickly it will be well-known, due to
its feature contents.

commenter

The role of the accident lawyer is to be your advocate and your legal representation. He also is aware that some motorcyclists don’t always drive safely, but that when automobiles drive unsafely the motorcyclist will suffer more severe injuries. Virtually anything can be stored in a portable garage that you want to keep safe from the ravages of winter.

commenter

My spouse and I stumbled over here from a different page and thought I should check things out. I like what I see so i am just following you. Look forward to looking at your web page yet again.

commenter

Guys on forums say there is no working roulette system, you can’t
beat it in the long run, but i’m using bluehand roulette
system for weeks and it earns me decent money everyday, maybe
couple of weeks is not a long run, maybe i’m lucky

commenter

I see a lot of interesting articles on your page. You have to spend a lot of time writing, i know how to save
you a lot of work, there is a tool that creates unique, SEO friendly posts in couple of minutes,
just type in google – laranita’s free content source

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>