WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Mcrypt Functions - 2 functions to perform encryptions
Categories : PHP, mcrypt Update Picture
Jeremy Stansfield
Date : Oct 30th 2003
Grade : 3 of 5 (graded 10 times)
Viewed : 14622
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Jeremy Stansfield
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

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));
        }
?>



mcrypt_cbc -- Encrypt/decrypt data in CBC mode
Categories : PHP, PHP Functions, mcrypt
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
minus - subtract arrays. Send two arrays and get an array with the operation A-B, elements on A that are not included on B.
Categories : PHP, Arrays, Algorithms
How to connect to MS SQL 6.x+ database server via ODBC functions of PHP3 compiled with iODBC and Openlink drivers under Linux.
Categories : Databases, MS SQL Server, PHP, ODBC
Simple Email address validation
Categories : Email, PHP, Strings
WebFTP: This tool can be used to access FTP sites from behind any firewall or proxy.
Categories : PHP, FTP
IPhider Obscure Any URL Anonymity connection lores obfuscation corporate survival.
Categories : PHP, Algorithms, Security, URLs
Checks Date-Input from HTML-Forms and converts to YYYY-MM-DD Format for MySQL Date-Fields
Categories : MySQL, Date Time, PHP, Databases
Intelligent 404 Handler
Categories : PHP, Errors and Logging
Sending mail to a mailing list and showing progress
Categories : PHP, Mail, Beginner Guides
mhash -- Compute hash
Categories : PHP, PHP Functions, mhash
How can I know about what Operating System is running on server from PHP?
Categories : PHP, Global Variables
Simple class to build tables with style sheets
Categories : HTML and PHP, PHP Classes, PHP
ezRemoteScripter - A little remote scripting (AJAX) helper
Categories : PHP, Java Script, AJAX
Blueshoes PHP Application Framework
Categories : PHP, Frameworks, PHP Classes
 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'.