MCrypt is a popular data-encryption package available for use with PHP, providing support for two-way encryption (i.e., encryption and decryption). Before you can use it, you need to follow these installation instructions:
MCrypt supports a number of encryption algorithms, all of which are listed here:
This section introduces just a sample of the more than 35 functions made available via this PHP extension. For a complete introduction, consult the PHP manual. Encrypting Data with MCrypt The mcrypt_encrypt() function encrypts the provided data, returning the encrypted result. The prototype follows: string mcrypt_encrypt(string cipher, string key, string data, The provided cipher names the particular encryption algorithm, and the parameter key determines the key used to encrypt the data. The mode parameter specifies one of the six available encryption modes: electronic codebook, cipher block chaining, cipher feedback, 8-bit output feedback, N-bit output feedback, and a special stream mode. 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: <?php This returns the following: -------------------------------------------- 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: string mcrypt_decrypt(string cipher, string key, string data, 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: -------------------------------------------- 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. Keep in mind that you can use functions such as popen() or exec() with any of your favorite third-party encryption technologies, for example, PGP ( http://www.pgpi.org/ ) or GPG ( http://www.gnupg.org/ ). Summary Hopefully the material presented in this chapter provided you with a few important tips and, more importantly, got you thinking about the many attack vectors that your application and server face. However, it’s important to understand that the topics described in this section are but a tiny sliver of the total security pie. If you’re new to the subject, take some time to learn more about some of the more prominent security-related Web sites. Regardless of your prior experience, you need to devise a strategy for staying abreast of breaking security news. Subscribing to the newsletters both from the more prevalent security-focused Web sites and from the product developers may be the best way to do so. However, your strategic preference is somewhat irrelevant; what is important is that you have a strategy and stick to it, lest your castle be conquered.
blog comments powered by Disqus |
|
|
|
|
|
|
|