I want to make one function like It should have some secret key to mix with so that one one can break it
function encrypt($string)
{
$key ="mastermind"
return encryptfunc($string,$key)
}
and same thing for decryption
I want to make one function like It should have some secret key to mix with so that one one can break it
function encrypt($string)
{
$key ="mastermind"
return encryptfunc($string,$key)
}
and same thing for decryption
If you are using PHP >= 5.3, the new openssl_encrypt
might help you : it allows encryption of data using a wide range of cypher methods.
Those data can later be decrypted with openssl_decrypt
, which, obviously, does the exact opposite.
And if you want to know which cypher functions you can use, openssl_get_cipher_methods
will be helpful ;-)
There is quite a lot of those, it seems ^^
Here's a portion of code I posted on my blog some time ago, that should demonstrate the usage of those three functions :
$methods = openssl_get_cipher_methods();
var_dump($methods);
$texteACrypter = "he who doesn't do anything, doesn't go wrong -- Zeev Suraski";
$clefSecrete = "glop";
echo '<pre>';
foreach ($methods as $method) {
$encrypted = openssl_encrypt($texteACrypter, $method, $clefSecrete);
$decrypted = openssl_decrypt($encrypted, $method, $clefSecrete);
echo $method . ' : ' . $encrypted . ' ; ' . $decrypted . "\n";
}
echo '</pre>';
The output I got when writting this was something like that :
bf-ecb : /nyRYCzQPE1sunxSBclxXBd7p7gl1fUnE80gBCS1NM4s3wS1Eho6rFHOOR73V9UtnolYW+flbiCwIKa/DYh5CQ== ; he who doesn't do anything, doesn't go wrong -- Zeev Suraski
bf-ofb : M9wwf140zhwHo98k8sj2MEXdogqXEQ+TjN81pebs2tmhNOsfU3jvMy91MBM76dWM7GVjeh95p8oDybDt ; he who doesn't do anything, doesn't go wrong -- Zeev Suraski
cast5-cbc : xKgdC1y654PFYW1rIjdevu8MsQOegvJoZx0KmMwb8aCHFmznxIQVy1yvAWR3bZztvGCGrM84WkpbG33pZcxUiQ== ; he who doesn't do anything, doesn't go wrong -- Zeev Suraski
cast5-cfb : t8ABR9mPvocRikrX0Kblq2rUXHiVnA/OnjR/mDJDq8+/nn6Z9yfPbpcpRat0lYqfVAcwlypT4A4KNq4S ; he who doesn't do anything, doesn't go wrong -- Zeev Suraski
cast5-ecb : xKgdC1y654NIzRl9gJqbhYKtmJoXBoFpgLhwgdtPtYB7VZ1tRHLX0MjErtfREMJBAonp48zngSiTKlsKV0/WhQ== ; he who doesn't do anything, doesn't go wrong -- Zeev Suraski
cast5-ofb : t8ABR9mPvofCv9+AKTcRO4Q0doYlavn8zRzLvV3dZk0niO7l20KloA4nUll4VN1B5n89T/IuGh9piPte ; he who doesn't do anything, doesn't go wrong -- Zeev Suraski
des-cbc : WrCiOVPU1ipF+0trwXyVZ/6cxiNVft+TK2+vAP0E57b9smf9x/cZlQQ4531aDX778S3YJeP/5/YulADXoHT/+Q== ; he who doesn't do anything, doesn't go wrong -- Zeev Suraski
des-cfb : cDDlaifQN+hGOnGJ2xvGna7y8+qRxwQG+1DJBwQm/4abKgdZYUczC4+aOPGesZM1nKXjgoqB4+KTxGNo ; he who doesn't do anything, doesn't go wrong -- Zeev Suraski
And if you are not using PHP 5.3, you might want to take a look to the Mcrypt section of the manual, and functions such as mcrypt_encrypt
;-)
This is an interface to the mcrypt library, which supports a wide variety of block algorithms such as DES, TripleDES, Blowfish (default), 3-WAY, SAFER-SK64, SAFER-SK128, TWOFISH, TEA, RC2 and GOST in CBC, OFB, CFB and ECB cipher modes.
I'm not a crypto guy, but I use this kind of things :
function crypt($dataToEncrypt){
$appKey = '%39d15#13P0£df458asdc%/dfr_A!8792*dskjfzaesdfpopdfo45s4dqd8d4fsd+dfd4s"Z1';
$td = mcrypt_module_open(MCRYPT_SERPENT, '', MCRYPT_MODE_CBC, '');
// Creates IV and gets key size
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
$ks = mcrypt_enc_get_key_size($td);
// Creates key from application key
$key = substr($appKey, 0, $ks);
// Initialization
mcrypt_generic_init($td, $key, $iv);
// Crypt data
$encrypted = mcrypt_generic($td, $dataToEncrypt);
// Close
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return array($encrypted, $iv);
}
To decrypt a string you need the key and the initialization vector ($iv
).
function decrypt($encryptedData, $iv){
$appKey = '%39d15#13P0£df458asdc%/dfr_A!8792*dskjfzaesdfpopdfo45s4dqd8d4fsd+dfd4s"Z1';
$td = mcrypt_module_open(MCRYPT_SERPENT, '', MCRYPT_MODE_CBC, '');
// Gets key size
$ks = mcrypt_enc_get_key_size($td);
// Creates key from application key
$key = substr($appKey, 0, $ks);
// Initialization
mcrypt_generic_init($td, $key, $iv);
// Decrypt data
$decrypted = mdecrypt_generic($td, $encryptedData);
// Close
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return trim($decrypted);
}