Encrypt string with php

How to Encrypt and Decrypt a String in PHP

PHP allows encrypting and decrypting a string with many methods, in this page we focus on one of the Cryptography Extensions, known as OpenSSL. To be short, it can be used to encrypt and decrypt data.

This extension binds functions of OpenSSL library for symmetric and asymmetric encryption and decryption, PBKDF2, PKCS7, PKCS12, X509 and other crypto operations. In addition to that it provides implementation of TLS streams.

Encrypting Data with openssl_encrypt()

The openssl_encrypt() ope function can be applied for encrypting data in PHP.

The syntax of openssl_encrypt() will look as follows:

string openssl_encrypt( string $data, string $method, string $key, $options = 0, string $iv, string $tag= NULL, string $aad, int $tag_length = 16 )

On success, it returns the encrypted string. Otherwise, it returns FALSE.

Decrypting Data with openssl_decrypt()

You can use openssl_decrypt() for decrypting data in PHP.

The syntax of this function is:

string openssl_decrypt( string $data, string $method, string $key, int $options = 0, string $iv, string $tag, string $aad )

On success, it returns the decrypted string. Otherwise, it returns FALSE.

Examples of Encrypting and Decrypting a String in PHP

To be more precise, let’s have a look at examples of encrypting and decrypting a string.

Example 1

 // Storing a string into the variable which // needs to be Encrypted $simple_string = "Welcome to W3docs\n"; // Displaying the original string echo "Original String: " . $simple_string; // Storingthe cipher method $ciphering = "AES-128-CTR"; // Using OpenSSl Encryption method $iv_length = openssl_cipher_iv_length($ciphering); $options = 0; // Non-NULL Initialization Vector for encryption $encryption_iv = '1234567891011121'; // Storing the encryption key $encryption_key = "W3docs"; // Using openssl_encrypt() function to encrypt the data $encryption = openssl_encrypt($simple_string, $ciphering, $encryption_key, $options, $encryption_iv); // Displaying the encrypted string echo "Encrypted String: " . $encryption . "\n"; // Non-NULL Initialization Vector for decryption $decryption_iv = '1234567891011121'; // Storing the decryption key $decryption_key = "W3docs"; // Using openssl_decrypt() function to decrypt the data $decryption = openssl_decrypt($encryption, $ciphering, $decryption_key, $options, $decryption_iv); // Displaying the decrypted string echo "Decrypted String: " . $decryption; ?>

The output of the code above will be:

Original String: Welcome to W3docs Encrypted String: kZEv65uJVrtngs6rhfX9WG2U Decrypted String: Welcome to W3docs

Example 2

In the second example, the string to be encrypted and decrypted is the same, yet the encrypted string randomly changes specifically.

 // Storing a string into the variable which // needs to be Encrypted $simple_string = "Welcome to W3docs"; // Displaying the original string echo "Original String: " . $simple_string . "\n"; // Storing cipher method $ciphering = "BF-CBC"; // Using OpenSSl encryption method $iv_length = openssl_cipher_iv_length($ciphering); $options = 0; // Using random_bytes() function which gives // randomly 16 digit values $encryption_iv = random_bytes($iv_length); // Alternatively, any 16 digits may be used // characters or numeric for iv $encryption_key = openssl_digest(php_uname(), 'MD5', true); // Encryption of string process begins $encryption = openssl_encrypt($simple_string, $ciphering, $encryption_key, $options, $encryption_iv); // Display the encrypted string echo "Encrypted String: " . $encryption . "\n"; // Decryption of string process begins // Used random_bytes() that gives randomly // 16 digit values $decryption_iv = random_bytes($iv_length); // Store the decryption key $decryption_key = openssl_digest(php_uname(), 'MD5', true); // Decrypting the string $decryption = openssl_decrypt($encryption, $ciphering, $decryption_key, $options, $encryption_iv); // Showing the decrypted string echo "Decrypted String: " . $decryption; ?>
Original String: Welcome to W3docs Encrypted String: Zm4a3DgDwfrlsYVLmc8iANj1PXw3uCUe Decrypted String: Welcome to W3docs

About OpenSSL

OpenSSL is a robust, general-purpose cryptography library that can encompass both symmetric and asymmetric encryption and decryption.

It is licensed under an Apache-style license. Hence, everyone is free to get and use it for both commercial and non-commercial purposes.

For more information, you can check out this source.

Источник

Encrypt string with php

  • Different ways to write a PHP code
  • How to write comments in PHP ?
  • Introduction to Codeignitor (PHP)
  • How to echo HTML in PHP ?
  • Error handling in PHP
  • How to show All Errors in PHP ?
  • How to Start and Stop a Timer in PHP ?
  • How to create default function parameter in PHP?
  • How to check if mod_rewrite is enabled in PHP ?
  • Web Scraping in PHP Using Simple HTML DOM Parser
  • How to pass form variables from one page to other page in PHP ?
  • How to display logged in user information in PHP ?
  • How to find out where a function is defined using PHP ?
  • How to Get $_POST from multiple check-boxes ?
  • How to Secure hash and salt for PHP passwords ?
  • Program to Insert new item in array on any position in PHP
  • PHP append one array to another
  • How to delete an Element From an Array in PHP ?
  • How to print all the values of an array in PHP ?
  • How to perform Array Delete by Value Not Key in PHP ?
  • Removing Array Element and Re-Indexing in PHP
  • How to count all array elements in PHP ?
  • How to insert an item at the beginning of an array in PHP ?
  • PHP Check if two arrays contain same elements
  • Merge two arrays keeping original keys in PHP
  • PHP program to find the maximum and the minimum in array
  • How to check a key exists in an array in PHP ?
  • PHP | Second most frequent element in an array
  • Sort array of objects by object fields in PHP
  • PHP | Sort array of strings in natural and standard orders
  • How to pass PHP Variables by reference ?
  • How to format Phone Numbers in PHP ?
  • How to use php serialize() and unserialize() Function
  • Implementing callback in PHP
  • PHP | Merging two or more arrays using array_merge()
  • PHP program to print an arithmetic progression series using inbuilt functions
  • How to prevent SQL Injection in PHP ?
  • How to extract the user name from the email ID using PHP ?
  • How to count rows in MySQL table in PHP ?
  • How to parse a CSV File in PHP ?
  • How to generate simple random password from a given string using PHP ?
  • How to upload images in MySQL using PHP PDO ?
  • How to check foreach Loop Key Value in PHP ?
  • How to properly Format a Number With Leading Zeros in PHP ?
  • How to get a File Extension in PHP ?
  • How to get the current Date and Time in PHP ?
  • PHP program to change date format
  • How to convert DateTime to String using PHP ?
  • How to get Time Difference in Minutes in PHP ?
  • Return all dates between two dates in an array in PHP
  • Sort an array of dates in PHP
  • How to get the time of the last modification of the current page in PHP?
  • How to convert a Date into Timestamp using PHP ?
  • How to add 24 hours to a unix timestamp in php?
  • Sort a multidimensional array by date element in PHP
  • Convert timestamp to readable date/time in PHP
  • PHP | Number of week days between two dates
  • PHP | Converting string to Date and DateTime
  • How to get last day of a month from date in PHP ?
  • PHP | Change strings in an array to uppercase
  • How to convert first character of all the words uppercase using PHP ?
  • How to get the last character of a string in PHP ?
  • How to convert uppercase string to lowercase using PHP ?
  • How to extract Numbers From a String in PHP ?
  • How to replace String in PHP ?
  • How to Encrypt and Decrypt a PHP String ?
  • How to display string values within a table using PHP ?
  • How to write Multi-Line Strings in PHP ?
  • How to check if a String Contains a Substring in PHP ?
  • How to append a string in PHP ?
  • How to remove white spaces only beginning/end of a string using PHP ?
  • How to Remove Special Character from String in PHP ?
  • How to create a string by joining the array elements using PHP ?
  • How to prepend a string in PHP ?

Источник

Читайте также:  Питон констракшн телефон отдела
Оцените статью