PHP password encryption

For most of us, web developers, the encryption and decryption of strings in the www is a known mandatory fact. But we all straggling to find the best way to approach the situation.

There are many ways to encrypt strings (and when i say “strings” i mean “Passwords” mostly), and they all can achieve the same goal, but each solution comes with the same problems :

  1. Lack of documentation.
  2. No way to make the same call from different scripts with the same encryption.
  3. Hard maintenance and spaghetti code.

This is why i am giving you a suitable solution. To be honest, the attached class and documentation are a Puzzle from several scripts and tutorials a read and learned in order to give you the best solution I can think of.

We all agree that mcrypt_* functions are the best way to create an encrypted strings, and we can wrap the mcrypt_encrypt and mcrypt_decrypt with md5 or base64 extra encryption, but this is a “Later on” subject we will discuss in a later article.

You can find a great explanation for mcrypt in here.

So, not lets start talking about the solution i give here.

This is a simple class with simple logic, because i don’t think this should be heavy on logic, for performance reasons.

The class attached is a small class with 3 methods :

  • encrypt – The encryption function, returns the encrypted String.
  • decrypt – The decryption function, returns the original String.
  • generateKey – Generates a cipher key to give the encryption and decryption functions.

The zipped file attached includes a complete example page to make you familiar with the logic of this class.

There are 2 ways to work with it :

Encrypt and decrypt on the same script – This will not need you to give the class a key to remember, because the script will encrypt and decrypt with the same instance of the class. I don’t see a use for this, but who am i to prevent it from you?

Encrypt once and decrypt later – This is the main use of the class. When you create an encrypted password you will get the encryption key for later use. You can instantiate a new class and retrieve the original string by giving the constructor of the new instance the key you received when created the encrypted string.



Great news ya all (12/03/2014)

This script downloaded tonight for the 1000th time. I got several emails asking for more “muscles” to the original script, and i will work on it soon. Meanwhile, take a look at my latest post about PHP 5.6 migration, and start upgrading.. There is only forward in our field of expertise 🙂


76 thoughts on “PHP password encryption

  1. free dj software

    Hello! My name is Alex and I stumbled upon your site while doing a quick Google search. Before I get into this, I would like to complement your site on the theme that is currently has, simple yet stylish. Back on topic, I really love the posts and the quality of them, most of the sites that write about the same things that you do are very repetitive and boring. I enjoy reading your posts, keep it up. Guess that’s all I’ll be posting, also, feel free to check out my sons site if you have time (I’m still working on mine, not the smartest in tech haha :P) free dj software or if that doesn’t show up, Keep on blogging! -Alex S.

  2. gta 5 cheats

    It is perfect time to make some plans for the longer term and it’s time to be happy.

    I have read this submit and if I could I want to recommend you some
    interesting issues or tips. Perhaps you can write next articles
    referring to this article. I want to learn even more things approximately it!

  3. Xbox Live Code Generator

    Because here is a list of multiplayer games is that the
    leave was asked for more. At present, you have the opportunity not
    only to communicate but also to have fun in social networks.
    It is a platform used by hundreds of millions of people
    and this amazing potential can be used for business as well.

  4. シャネル 長財布

    俺の人生、女に全く縁がないのだ。 ちょっとくらい縁ってのに触れてみたかった。恋人気分、その行為の一つ一つにドキッとしたりして。 こんなにも心が満たされていたのは久々のことだった。あえて言うなら私たちは戦う宿命にあったのね」 咲良と同じ事を那奈姉も言った! どこのバトル漫画のセリフですか。「はぁ」 私はなまり気味な身体をたまには動かそうと散歩をしていた。 朝食を食べ終えてから適度な運動をする。」 「ひっ!わ、分かった。「綾辻さんですか?すぐに呼んできますね」 「悪いね、頼むよ」 綺羅は雨の打ちつける窓辺を一人で眺めてるだけ。

  5. Darwin Elkayam

    I simply want to mention I am very new to blogging and really savored your web site. Likely I’m likely to bookmark your blog . You absolutely have wonderful posts. Thanks a bunch for sharing your website page.

  6. how to make extra money taking online surveys

    My developer is trying to convince me to move to .net from PHP. I have always disliked the idea because of the expenses. But he’s tryiong none the less. I’ve been using Movable-type on various websites for about a year and am anxious about switching to another platform. I have heard great things about Is there a way I can transfer all my wordpress posts into it? Any kind of help would be really appreciated!

  7. Matthew C. Kriner

    I simply want to say I’m new to weblog and definitely savored your web site. Most likely I’m going to bookmark your site . You really come with great stories. Kudos for revealing your web site.

  8. Pingback: FREE Articles For Websites

  9. minnie driver derm exclusive reviews

    I гeally like youг blog.. very nice colors & theme.

    Did you create this website yourself or did you hire
    someone to do it for you? Plz answer back as I’m looking tо create mÒ¯ own blog ɑnd would
    like to know Ñ¡here u got this from. thank you

  10. Andrew A. Sailer

    I simply want to mention I am beginner to weblog and certainly loved you’re blog site. Almost certainly I’m want to bookmark your website . You definitely have superb well written articles. Thanks for sharing with us your web-site.

  11. Sheree Killoy

    This web site is really a walk-by way of for all the data you wished about this and didn’t know who to ask. Glimpse here, and you’ll positively uncover it.

  12. Pingback: URL

  13. sports club la

    This is very interesting, You are a very skilled blogger. I’ve joined your feed and look forward to seeking more of your wonderful post. Also, I have shared your web site in my social networks!

  14. floor plans

    Great write-up, I¡¦m normal visitor of one¡¦s web site, maintain up the excellent operate, and It’s going to be a regular visitor for a long time.

  15. best home insurance

    Hey, you used to write excellent, but the last several posts have been kinda boring¡K I miss your great writings. Past several posts are just a little bit out of track! come on!

  16. living room set

    Good website! I really love how it is simple on my eyes and the data are well written. I’m wondering how I could be notified when a new post has been made. I have subscribed to your RSS feed which must do the trick! Have a nice day!

  17. new casino

    Wow, marvelous weblog structure! How lengthy have you been running a blog for? you make blogging glance easy. The whole look of your site is wonderful, as neatly as the content material!

  18. Daniell Carton

    Excellent read, I just passed this onto a colleague who was doing a little research on that. And he just bought me lunch since I found it for him smile So let me rephrase that: Thanks for lunch!

  19. Frederic Kayastha

    Generally I do not learn article on blogs, but I would like to say that this write-up very compelled me to take a look at and do so! Your writing taste has been amazed me. Thank you, quite nice article.|

  20. Shelby Kingery

    Today, while I was at work, my sister stole my iPad and tested to see if it can survive a 30 foot drop, just so she can be a youtube sensation. My apple ipad is now broken and she has 83 views. I know this is entirely off topic but I had to share it with someone!|

  21. homeowners insurance rate

    you’re in point of fact a excellent webmaster. The website loading pace is incredible. It kind of feels that you are doing any distinctive trick. Moreover, The contents are masterwork. you have performed a excellent task on this matter!

  22. Grisel Ramey

    Do check my site as well. It’s the finest information that can be found on the unannounced Marq by Assetz group in Bangalore. It’s possible for you to see my web site to additionally learn more about some of the newest jobs in Bangalore.

  23. model home interiors

    You actually make it seem so easy with your presentation but I find this topic to be actually something which I think I would never understand. It seems too complex and extremely broad for me. I’m looking forward for your next post, I will try to get the hang of it!

  24. web domain

    Hello my loved one! I wish to say that this post is amazing, nice written and come with approximately all significant infos. I would like to look extra posts like this .

  25. latest fashion trends

    I do not even know how I ended up here, but I thought this post was great. I do not know who you are but definitely you’re going to a famous blogger if you are not already 😉 Cheers!

  26. garden roses

    Hey, you used to write great, but the last few posts have been kinda boring¡K I miss your tremendous writings. Past several posts are just a little bit out of track! come on!

  27. cheap car parts

    I’m not sure where you are getting your info, but good topic. I needs to spend some time learning more or understanding more. Thanks for great info I was looking for this info for my mission.

  28. broyhill furniture

    Thanks , I have recently been looking for info about this topic for a while and yours is the best I have came upon till now. But, what about the bottom line? Are you positive in regards to the source?

  29. landscapers

    you’re really a good webmaster. The website loading speed is amazing. It kind of feels that you are doing any unique trick. Moreover, The contents are masterpiece. you’ve done a great process on this subject!

  30. Northpark Residences

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

  31. best dating sites

    Thanks for another wonderful post. Where else may anyone
    get that type of info in such a perfect means of writing?

    I’ve a presentation subsequent week, and I am on the search for
    such info.

  32. Pingback: biglist

Leave a Reply

Your email address will not be published. Required fields are marked *