Prev | Current Page 637 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

Each is referenced by an abbreviation: ecb, cbc, cfb, ofb, nofb, and stream,
respectively. Finally, the iv parameter initializes cbc, cfb, ofb, and certain algorithms
used in stream mode. Consider an example:
$ivs = mcrypt_get_iv_size(MCRYPT_DES, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($ivs, MCRYPT_RAND);
$key = "F925T";
$message = "This is the message I want to encrypt.";
$enc = mcrypt_encrypt(MCRYPT_DES, $key, $message, MCRYPT_MODE_CBC, $iv);
echo bin2hex($enc);
?>
This returns the following:
f5d8b337f27e251c25f6a17c74f93c5e9a8a21b91f2b1b0151e649232b486c93b36af467914bc7d8
You can then decrypt the text with the mcrypt_decrypt() function, introduced next.
Decrypting Data with MCrypt
The mcrypt_decrypt() function decrypts a previously encrypted cipher, provided
that the cipher, key, and mode are the same as those used to encrypt the data. Its
prototype follows:
CHAPTER 21 ?–  SECURE PHP PROGRAMMING 565
string mcrypt_decrypt(string cipher, string key, string data,
string mode [, string iv])
Go ahead and insert the following line into the previous example, directly after the last
statement:
echo mcrypt_decrypt(MCRYPT_DES, $key, $enc, MCRYPT_MODE_CBC, $iv);
This returns the following:
This is the message I want to encrypt.
The methods in this section are only those that are in some way incorporated into
the PHP extension set. However, you are not limited to these encryption/hashing
solutions.


Pages:
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649