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

    • This is probably the encrypted string that you are storing is of more than 14 characters long?

      • Alsong Dun Dunstan Philander

        So isnt there a way to insert unlimited length of the string to be enrypted

      • Alsong Dun Dunstan Philander

        Because when i encrypt a string of any length it can be decrypted perfectly. But in a case of storing the encrypted string in a MSSQL database and then retrieving it from the database and then decrypt it,it doesnt work

      • Alsong Dun Dunstan Philander

        I solved it, had to only change the datatype to varchar(max). Thanks

  • Dennis Araya Bustamante

    I replace return for echo and works fine, thanks a lot!

  • Hermenegildo Alejandro Gonzale

    Works like a charm! Thanks a lot, this code is simple and functional and save me a lot of time.

  • Raj Bhushan Yadav

    this is real common class function for enc & dec type

  • can’t decrypt. can you help me?