Source Code:
(back to article)
<?php // 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; ?>
Result:
Report an issue