How to Encrypt Decrypt String in PHP


Encrypt Decrypt String in PHP

Demo Download

In this tutorial, I will show you how to encrypt decrypt string in PHP. Encryption is very common in programming languages, it helps to secure our data even if it is accessed by unauthorize person.

I will create a PHP encryption decryption function which I will use to encrypt and decrypt my string.

You will need to make sure that you update the secret key and secret_iv before using this function.

I have added sample secret key and secret_iv in my function so that you can easily view the demo of this encryption and decryption function.

This function is using the openssl_encrypt and openssl_decrypt along with base64_encode and base64_decode respectively to encrypt and decrypt string in PHP.

1. Create an index.php File

Create an index.php file and paste the following code in it.

<html>
<head>
<title>Demo Encrypt Decrypt String PHP - AllPHPTricks.com</title>
<style>
body {
    font-family: Arial, sans-serif;
    line-height: 1.6;
}
</style>
</head>
<body>

<div style="width:700px; margin:50 auto;">
<h1>Demo Encrypt Decrypt String PHP</h1>

<?php
// Function to Encrypt Decrypt String
function encrypt_decrypt($action, $string) {
    $output = false;
    $encrypt_method = "AES-256-CBC";
    // Update your secret key before use
    $secret_key = '$7PHKqGt$yRlPjyt89rds4ioSDsglpk/';
    // Update your secret iv before use
    $secret_iv = '$QG8$hj7TRE2allPHPlBbrthUtoiu23bKJYi/';
    // hash
    $key = hash('sha256', $secret_key);
    
    // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
    $iv = substr(hash('sha256', $secret_iv), 0, 16);
    if ( $action == 'encrypt' ) {
        $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
        $output = base64_encode($output);
    } else if( $action == 'decrypt' ) {
        $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
    }
    return $output;
}

// Plain String
$string = "AllPHPTricks";
echo "<p><strong>Plain String:</strong> ". $string . "</p>";

//Encrypted String
$encrypted_string = encrypt_decrypt('encrypt', $string);
echo "<p><strong>Encrypted String:</strong> ". $encrypted_string . "</p>";

//Decrypted String
$decrypted_string = encrypt_decrypt('decrypt', $encrypted_string);
echo "<p><strong>Decrypted String:</strong> ". $decrypted_string . "</p>";
?>

</div>    
</body>
</html>

The above code contain a PHP function which will help you to encrypt / decrypt a text in PHP.

First I have created a PHP function which is doing encryption and decryption, then I created a variable and print it value without any encryption, then I encrypted that variable value and print the encrypted variable on the screen.

After that I decrypt that encrypted variable using our PHP function and then print it on the screen.

If you want to learn how to format PHP date, then you can check out my tutorial on how to convert PHP date format using PHP function.

Conclusion

I hope by now you know how to easily use encryption and decryption for string in PHP. Encryption is easy in PHP and you can use this function in your application, just make sure that you update the secret key and secret iv before using this function.

If you found this tutorial helpful, share it with your friends and developers group.

I spent several hours to create this tutorial, if you want to say thanks so like my page on Facebook, Twitter and share it.

Demo Download

Facebook Official Page: All PHP Tricks

Twitter Official Page: All PHP Tricks

Article By
Javed Ur Rehman is a passionate blogger and web developer, he loves to share web development tutorials and blogging tips. He usually writes about HTML, CSS, JavaScript, Jquery, Ajax, PHP and MySQL.

Leave a Reply

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