c# code 39 barcode Exercise: Implementing the SymmetricCrypt Class in C#

Paint Code 39 in C# Exercise: Implementing the SymmetricCrypt Class

Exercise: Implementing the SymmetricCrypt Class
Code-39 Generator In Visual C#
Using Barcode maker for VS .NET Control to generate, create ANSI/AIM Code 39 image in .NET applications.
www.OnBarcode.com
Code 39 Reader In Visual C#
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
1. Add a new file in the business directory called symmetric_crypt.php with the following code in it: < php class SymmetricCrypt { // Encryption/decryption key private static $_msSecretKey = 'From Dusk Till Dawn'; // The initialization vector private static $_msHexaIv = 'c7098adc8d6128b5d4b4f7b2fe7f7f05'; // Use the Rijndael Encryption Algorithm private static $_msCipherAlgorithm = MCRYPT_RIJNDAEL_128; /* Function encrypts plain-text string received as parameter and returns the result in hexadecimal format */
Print UPC - 13 In C#.NET
Using Barcode creation for Visual Studio .NET Control to generate, create GTIN - 13 image in Visual Studio .NET applications.
www.OnBarcode.com
Print EAN 128 In C#
Using Barcode maker for Visual Studio .NET Control to generate, create USS-128 image in .NET applications.
www.OnBarcode.com
CHAPTER 11 MANAGING CUSTOMER DETAILS
Creating Matrix In Visual C#
Using Barcode creation for Visual Studio .NET Control to generate, create 2D image in .NET applications.
www.OnBarcode.com
Linear 1D Barcode Maker In C#
Using Barcode creation for Visual Studio .NET Control to generate, create Linear image in Visual Studio .NET applications.
www.OnBarcode.com
public static function Encrypt($plainString) { // Pack SymmetricCrypt::_msHexaIv into a binary string $binary_iv = pack('H*', self::$_msHexaIv); // Encrypt $plainString $binary_encrypted_string = mcrypt_encrypt( self::$_msCipherAlgorithm, self::$_msSecretKey, $plainString, MCRYPT_MODE_CBC, $binary_iv); // Convert $binary_encrypted_string to hexadecimal format $hexa_encrypted_string = bin2hex($binary_encrypted_string); return $hexa_encrypted_string; } /* Function decrypts hexadecimal string received as parameter and returns the result in hexadecimal format */ public static function Decrypt($encryptedString) { // Pack Symmetric::_msHexaIv into a binary string $binary_iv = pack('H*', self::$_msHexaIv); // Convert string in hexadecimal to byte array $binary_encrypted_string = pack('H*', $encryptedString); // Decrypt $binary_encrypted_string $decrypted_string = mcrypt_decrypt( self::$_msCipherAlgorithm, self::$_msSecretKey, $binary_encrypted_string, MCRYPT_MODE_CBC, $binary_iv); return $decrypted_string; } } > 2. Add a test file in the hatshop folder called test_encryption.php with the following code: < php if (isset ($_GET['my_string'])) { require_once 'include/config.php';
Printing UPC A In Visual C#.NET
Using Barcode generation for VS .NET Control to generate, create UPC-A image in Visual Studio .NET applications.
www.OnBarcode.com
International Standard Book Number Maker In C#
Using Barcode generation for .NET Control to generate, create ISBN - 10 image in .NET framework applications.
www.OnBarcode.com
CHAPTER 11 MANAGING CUSTOMER DETAILS
Painting Code-39 In .NET
Using Barcode encoder for Reporting Service Control to generate, create Code-39 image in Reporting Service applications.
www.OnBarcode.com
Code-39 Generator In None
Using Barcode generator for Font Control to generate, create Code 39 Full ASCII image in Font applications.
www.OnBarcode.com
require_once BUSINESS_DIR . 'symmetric_crypt.php'; $string = $_GET['my_string']; echo 'The string is:<br />' . $string . '<br /><br />'; $encrypted_string = SymmetricCrypt::Encrypt($string); echo 'Encrypted string: <br />' . $encrypted_string . '<br /><br />'; $decrypted_string = SymmetricCrypt::Decrypt($encrypted_string); echo 'Decrypted string:<br />' . $decrypted_string; } > <br /><br /> <form action="test_encryption.php"> Enter string to encrypt: <input type="text" name="my_string" /><br /> <input type="submit" value="Encrypt" /> </form> 3. Load the newly created test_encryption.php file in your favorite browser and give a string to encrypt/decrypt (see Figure 11-2).
Paint Code 128 Code Set A In VB.NET
Using Barcode maker for .NET Control to generate, create ANSI/AIM Code 128 image in .NET framework applications.
www.OnBarcode.com
Denso QR Bar Code Encoder In Visual Studio .NET
Using Barcode printer for Visual Studio .NET Control to generate, create QR-Code image in .NET framework applications.
www.OnBarcode.com
Note If the mcrypt library wasn t installed or configured correctly, you ll receive a fatal error about the
Painting Code 128 Code Set C In Objective-C
Using Barcode creation for iPad Control to generate, create Code 128 image in iPad applications.
www.OnBarcode.com
Printing Barcode In Java
Using Barcode creator for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
call to mcrypt_encrypt(). If that happens, check the installation instructions in Appendix A.
PDF 417 Drawer In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create PDF417 image in ASP.NET applications.
www.OnBarcode.com
Making QR Code 2d Barcode In None
Using Barcode generation for Word Control to generate, create Quick Response Code image in Office Word applications.
www.OnBarcode.com
Figure 11-2. Testing encryption
Print PDF417 In None
Using Barcode drawer for Microsoft Excel Control to generate, create PDF 417 image in Office Excel applications.
www.OnBarcode.com
Make Denso QR Bar Code In VB.NET
Using Barcode creator for VS .NET Control to generate, create QR Code image in .NET applications.
www.OnBarcode.com
CHAPTER 11 MANAGING CUSTOMER DETAILS
Data Matrix Drawer In None
Using Barcode generation for Software Control to generate, create ECC200 image in Software applications.
www.OnBarcode.com
Print USS-128 In Java
Using Barcode drawer for Java Control to generate, create GS1-128 image in Java applications.
www.OnBarcode.com
Caution As you might have noticed after running the test page, the decrypted string always has a length that is a multiple of 32 bytes. If the original string is less than 32 bytes, null characters are appended until the string s length becomes a multiple of 32 bytes. You need to be careful with this detail because it means the decrypted value of the string may not be identical to the encrypted value. For our HatShop project, because we ll encrypt XML data and the values of interest are between XML tags, we won t need to worry about having additional void characters at the end of the string.
How It Works: Encryption Functionality in the Business Tier
The SymmetricCrypt class has two static methods, Encrypt() and Decrypt(), which encrypt and decrypt data, and a number of encryption configurations parameters stored as static members: // Encryption/decryption key private static $_msSecretKey = 'From Dusk Till Dawn'; // The initialization vector private static $_msHexaIv = 'c7098adc8d6128b5d4b4f7b2fe7f7f05'; // Use the Rijndael Encryption Algorithm private static $_msCipherAlgorithm = MCRYPT_RIJNDAEL_128; The secret key is 16 characters (bytes) long for AES algorithms. Using a smaller key is allowed by the mcrypt library but will reduce the encryption security. The IV should be exactly 16 bytes long for AES and will be kept as a hexadecimal string (2x16=32 chars long). Both $_msSecretKey and $_msHexaIv variables are set to temporary values here. They could just as easily take any other values, depending on the key you want to use. Encrypt() starts by converting the IV from its hexadecimal value to a byte array because this is the format expected by the mcrypt_encrypt function (the one that does the actual encryption): // Pack SymmetricCrypt::_msHexaIv into a binary string $binary_iv = pack('H*', self::$_msHexaIv); The conversion is done using PHP s pack function (learn more about it at http://www.php.net/pack). The call to mcrypt_encrypt follows: // Encrypt $plainString $binary_encrypted_string = mcrypt_encrypt( self::$_msCipherAlgorithm, self::$_msSecretKey, $plainString, MCRYPT_MODE_CBC, $binary_iv); This is the call that performs the actual encryption. Its parameters are obvious, and you can find more detail about the mcrypt_encrypt function at http://www.php.net/mcrypt. The MCRYPT_MODE_CBC specifies the cipher block chaining encryption method; this method uses a chaining mechanism in which the encryption of
Copyright © OnBarcode.com . All rights reserved.