I am trying to create a simple script to encrypt and decrypt a message. The script consists of two php files, both being similar. The first PHP file will encrypt the message based upon the "secret key" a user inputs (as opposed to haveing a default set key). The coding for the file file is below:
/ Open the cipher /
$td = mcrypt_module_open(MCRYPT_BLOWFISH, '',
MCRYPT_MODE_ECB, '/usr/lib/mcrypt-modes');
$td = mcrypt_module_open('rijndael-256', '', 'ofb', '');
/ Variable Creation /
$message = DoStripSlashes( $_REQUEST['message'] );
/ Create the IV and determine the keysize length, used MCRYPT_RAND
on Windows instead */
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
$ks = mcrypt_enc_get_key_size($td);
/ Create key /
$ikey = ( $_REQUEST['ikey'] );
$key = substr(md5($ikey), 0, $ks);
/ Intialize encryption /
mcrypt_generic_init($td, $key, $iv);
/ Encrypt data /
$encrypted = mcrypt_generic($td, $message);
/ Terminate encryption handler /
mcrypt_generic_deinit($td);
/ Initialize encryption module for decryption /
mcrypt_generic_init($td, $key, $iv);
/ Decrypt encrypted string /
$decrypted = mdecrypt_generic($td, $encrypted);
/ Terminate decryption handle and close module /
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
/Data Format/
$data = bin2hex ($encrypted);
That coding is the coding to encrypt the message the user inputs. The rest of the script will email the encrypted text to whomever the user wants. The recipient of the encrypted text can then go to a page (the second PHP file) where they can paste in the encrypted text in the textarea box. Below that in a textbox they can type in the secret key (already agreed upon by the two users). Theoretically, the second script will echo the decrypted message. Below is the decrypted coding:
/ Open the cipher /
$td = mcrypt_module_open(MCRYPT_BLOWFISH, '',
MCRYPT_MODE_ECB, '/usr/lib/mcrypt-modes');
$td = mcrypt_module_open('rijndael-256', '', 'ofb', '');
/ Create the IV and determine the keysize length, used MCRYPT_RAND
on Windows instead */
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
$ks = mcrypt_enc_get_key_size($td);
/ Create key /
$ikey = ( $_REQUEST['dkey'] );
$key = substr(md5($ikey), 0, $ks);
/ Intialize encryption /
mcrypt_generic_init($td, $key, $iv);
/ Encrypt data /
$data = bin2hex ( $_REQUEST['idata'] );
$encrypted = mcrypt_generic($td, $data);
/ Terminate encryption handler /
mcrypt_generic_deinit($td);
/ Initialize encryption module for decryption /
mcrypt_generic_init($td, $key, $iv);
/ Decrypt encrypted string /
$decrypted = mdecrypt_generic($td, $encrypted);
/ Terminate decryption handle and close module /
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
/ Show string /
echo "<PRE>";
echo "The decrypted message: " . trim($decrypted) . "\n";
As you can tell, I basically modified the mcrypt example given mcrypt_module_open example on this site. The response I get is odd looking text. So, I have tried adding hex2bin to the data. It gives the data, but does not decrypt properly. Is there something I'm missing? I just started teaching myself PHP so I'm sure it's something pretty simple that I have overlooked.
The test message to be encrypted is "test message" and the secret key for this one I used was "hello". The results it emails me is 8a37c7b9c101b51771df0eff . If I go in and try to decrypt it, the decrypted message is the same as what I input in to the idata field. However, if I add the bin2hex, such as shown in the code:
$data = bin2hex ( $_REQUEST['idata'] );
Then I get the following:
386133376337623963313031623531373731646630656666