|
|
|
These 2 functions are used to perform encryptions. They are currently
set up to perform triple des encrypting on information that will be
stored in a file. The str_replace calls at the beginning of each function
were put there after hours of debugging, i am sure the replacements could
be done quicker... These functions with a little modification could be used
to store encrypted information in a database... I prefer to use mcrypt over
any other encryption, because of the security.
_mencrypt ( string $input, string $key )
$input - stuff to encrypt
$key - the secret key to use
_mdecrypt ( string $input, string $key )
$input - stuff to decrypt
$key - the secret key to use
both function perform these:
substr(md5($key),0,24) - this make sure the secret key is the max
length, the longer the key the harder to
break it.
str_replace - a few characters , both function must be the same
Usage:
1. For storing Credit Card Numbers in a database.
2. encrypting a user file based on their password, their password would
be the key to decrypting. This also brings up a point that in this
user file could be used to stored other keys for decrypting other
sensitive data, such as credit card numbers.
These functions are currently running in an application for a hospital.
Any questions, or comments feel free to get in contact, i have received
allot from others, its time i gave back.
<?
//$input - stuff to encrypt
//$key - the secret key to use
function _mencrypt($input,$key){
$input = str_replace("\n","",$input);
$input = str_replace("\t","",$input);
$input = str_replace("\r","",$input);
$key = substr(md5($key),0,24);
$td = mcrypt_module_open ('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
mcrypt_generic_init ($td, $key, $iv);
$encrypted_data = mcrypt_generic ($td, $input);
mcrypt_generic_deinit ($td);
mcrypt_module_close ($td);
return trim(chop(base64_encode($encrypted_data)));
}
//$input - stuff to decrypt
//$key - the secret key to use
function _mdecrypt($input,$key){
$input = str_replace("\n","",$input);
$input = str_replace("\t","",$input);
$input = str_replace("\r","",$input);
$input = trim(chop(base64_decode($input)));
$td = mcrypt_module_open ('tripledes', '', 'ecb', '');
$key = substr(md5($key),0,24);
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
mcrypt_generic_init ($td, $key, $iv);
$decrypted_data = mdecrypt_generic ($td, $input);
mcrypt_generic_deinit ($td);
mcrypt_module_close ($td);
return trim(chop($decrypted_data));
}
?> |
|
| Query2Report : Generating Html, Pdf and Csv Reports from SQL Query Categories : PHP, PHP, HTML, PDF, Excel | | | mcrypt_cbc -- Encrypt/decrypt data in CBC mode Categories : PHP, PHP Functions, mcrypt | | | JDToGregorian -- Converts Julian Day Count to Gregorian date Categories : PHP, PHP Functions, Calendar | | | Variable serialization and unserialization. Loading and saving variable structures
to and from file. Categories : Arrays, Filesystem, Variables, Strings, PHP | | | Free PDF file creation using PHP. Categories : PDF, PHP | | | How to get the name of the user which is accessing
the current script. Categories : Environment Variables, PHP | | | PHP4 MYSQL Authentication Script with cookie. Short & Sweet
Categories : Authentication, Apache, Cookies, PHP, MySQL | | | Latitude-Longitude to Miles Categories : PHP, Utilities, Math. | | | Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | | | A Complete table(ADD,EDIT,VIEW,DELETE) management System PHP,MYSQL, JAVASCRIPT Categories : PHP, MySQL, Java Script, Databases | | | Retrieve text from table and email to your e-
address in pipe delimited format. Categories : PHP, MySQL | | | An efficient iterative and buffered text file reader Categories : PHP, Classes and Objects, Filesystem, PHP Classes, Log Files | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | | | Create and restore backup of MySQL databases Categories : MySQL, Databases, PHP, PHP Classes, Complete Programs | | | Using $PHP_AUTH_USER and $PHP_AUTH_PW to authenticate. Categories : Authentication, PHP | |
| | | | dee artagnanh wrote :1822
As a PHP beginner, I find these functions useful. Is there
a bug here, though, as when I use '1234567' as input and
encrypt it, it can't decrypt it back right. Same happens
for any input string that starts with '12345678'
or '123456789'.
| |
|
|