A simple two-way function to encrypt or decrypt a string

This function will provide you a two-way system to encrypt a string or decrypt an encrypted string.

So, how to encrypt a string?
Just call this function and pass your string. Additionally set your second parameter to 'e' (optional). To encrypt Hello World!, write-

It’ll generate a encrypted string RTlOMytOZStXdjdHbDZtamNDWFpGdz09

And, how to decrypt?
Pass your encrypted string to the function and set second parameter to 'd'.


Need help with your WordPress tasks?

  • Wladimir Jr

    Thank you!!!

  • Forrest Rave

    in the first line
    function my_simple_crypt( $string, $action = ‘e’ ) {
    you set the parameter $action to = ‘e’, how come?

    • This is the default action, where e stands for encrypt. That means, if you pass a string as first parameter and do not pass second parameter, it’ll assume that you are going to encrypt the string.
      Bu if you want to decrypt a string, you must assign second parameter as d.
      Hope it helps.

  • hi nazmul ahsan, thanks for your script, what if i want the length of encryption to be only 16 characters long not 32. is it possible?

  • Webber Depor

    does not work for me. returns false everytime

    • Sorry to hear that. but what do you mean when you say “returns false”?

  • Alsong Dun Dunstan Philander

    I have used the code to encrypt strings and store them in the database and then retrieve it. But when you insert a string of more than 14 characters, it is encrypted but after returning it from the database (MSSQL database), then placing it to be decrypted. It doesnt decrypt it. But when i am not storing it into a database, it works well