Post: PHP Encryption/Decryption
04-11-2016, 12:20 AM #1
(adsbygoogle = window.adsbygoogle || []).push({}); I wrote some code in PHP that encrypts/reverse the data passed in it. The data is not reversible unless you have the key. Also if the variable being
encrypted is an object then remove the line "if (serializedContainsObject($decoded)) return '';"
This is resourceful when passing data between two or more servers.

Encrypt PHP Code:
    
<?php
$key = '032848'; // This key should be used when reversing the encrypted data.
$foo = 'foo'; // Data to be encrypted.

// Function that will encrypt the data
// This will return a base64 encoded string.
function mc_encrypt($encrypt, $key){
$cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, ''Winky Winky; // Using BLOWFISH and CBC - Initializing

$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC), MCRYPT_RAND); // Generating a IV
$key = pack('H*', $key); // Packing the key

if (mcrypt_generic_init($cipher, $key, $iv) != -1)
{
$encrypt = serialize($encrypt); // We serialize the data being passed.
$cipherText = mcrypt_generic($cipher, $encrypt); // Encypted
mcrypt_generic_deinit($cipher); // De-initializing

return base64_encode(bin2hex($cipherText) . "\n" . bin2hex($iv) . "\n" . md5($cipherText . $iv)); // Base64 Encoding
}

return ''; // If it didn't encrypt then return a blank string.
}
// Displaying key, data (original), and encrypted data.

echo "Key: {$key} <br />";
echo "Data: {$foo}<br />";
echo "Encrypted: <br />" . mc_encrypt($foo, $key);


Reversing Encryption:
    
function mc_decrypt($decrypt, $key)
{
$step1 = explode("\n", base64_decode($decrypt));
$cipherText = hex2bin($step1[0]);
$iv = hex2bin($step1[1]);
$modified = (md5($cipherText . $iv) != $step1[2]);
if ($modified) {
return '';
}

if (strlen($iv) !== mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC))
return '';

$key = pack('H*', $key);
$decoded = mcrypt_decrypt(MCRYPT_BLOWFISH, $key, $cipherText, MCRYPT_MODE_CBC, $iv);
if (serializedContainsObject($decoded))
return '';

return unserialize($decoded);
}
function serializedContainsObject($serialized)
{
if (strpos($serialized, 'O:'Winky Winky !== false && preg_match('#(?<=^|[;{}])O:[+-]?[0-9]+:"#', $serialized))
{
return true;
}
if (strpos($serialized, 'C:'Winky Winky !== false && preg_match('#(?<=^|[;{}])C:[+-]?[0-9]+:"#', $serialized))
{
return true;
}
if (strpos($serialized, 'o:'Winky Winky !== false && preg_match('#(?<=^|[;{}])o:[+-]?[0-9]+:"#', $serialized))
{
return true;
}
return false;
}
04-22-2016, 10:20 AM #2
Danny
Hurah!
Originally posted by D4tabase View Post
I wrote some code in PHP that encrypts/reverse the data passed in it. The data is not reversible unless you have the key. Also if the variable being
encrypted is an object then remove the line "if (serializedContainsObject($decoded)) return '';"
This is resourceful when passing data between two or more servers.

Encrypt PHP Code:
    
<?php
$key = '032848'; // This key should be used when reversing the encrypted data.
$foo = 'foo'; // Data to be encrypted.

// Function that will encrypt the data
// This will return a base64 encoded string.
function mc_encrypt($encrypt, $key){
$cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, ''Winky Winky; // Using BLOWFISH and CBC - Initializing

$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC), MCRYPT_RAND); // Generating a IV
$key = pack('H*', $key); // Packing the key

if (mcrypt_generic_init($cipher, $key, $iv) != -1)
{
$encrypt = serialize($encrypt); // We serialize the data being passed.
$cipherText = mcrypt_generic($cipher, $encrypt); // Encypted
mcrypt_generic_deinit($cipher); // De-initializing

return base64_encode(bin2hex($cipherText) . "\n" . bin2hex($iv) . "\n" . md5($cipherText . $iv)); // Base64 Encoding
}

return ''; // If it didn't encrypt then return a blank string.
}
// Displaying key, data (original), and encrypted data.

echo "Key: {$key} <br />";
echo "Data: {$foo}<br />";
echo "Encrypted: <br />" . mc_encrypt($foo, $key);


Reversing Encryption:
    
function mc_decrypt($decrypt, $key)
{
$step1 = explode("\n", base64_decode($decrypt));
$cipherText = hex2bin($step1[0]);
$iv = hex2bin($step1[1]);
$modified = (md5($cipherText . $iv) != $step1[2]);
if ($modified) {
return '';
}

if (strlen($iv) !== mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC))
return '';

$key = pack('H*', $key);
$decoded = mcrypt_decrypt(MCRYPT_BLOWFISH, $key, $cipherText, MCRYPT_MODE_CBC, $iv);
if (serializedContainsObject($decoded))
return '';

return unserialize($decoded);
}
function serializedContainsObject($serialized)
{
if (strpos($serialized, 'O:'Winky Winky !== false && preg_match('#(?<=^|[;{}])O:[+-]?[0-9]+:"#', $serialized))
{
return true;
}
if (strpos($serialized, 'C:'Winky Winky !== false && preg_match('#(?<=^|[;{}])C:[+-]?[0-9]+:"#', $serialized))
{
return true;
}
if (strpos($serialized, 'o:'Winky Winky !== false && preg_match('#(?<=^|[;{}])o:[+-]?[0-9]+:"#', $serialized))
{
return true;
}
return false;
}


Very handy thanks Smile

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo