The encryption and decryption module can meet common requirements for symmetric encryption and decryption and hash functions. To add a module to an application, follow these steps:
1) Add an Assembly reference to the module. Add a reference to the Assembly Microsoft. Practices. EnterpriseLibrary. Security. Cryptography. dll.
2) Add references to the Assembly Microsoft. Practices. ObjectBuilder2.dll and Microsoft. Practices. EnterpriseLibrary. Common. dll.
3) introduce the namespace in the file that requires module functions
Using Microsoft. Practices. EnterpriseLibrary. Security. Cryptography;
4) use the functions provided by the module in the code
Typical Features
1. Use symmetric encryption algorithms to encrypt data
1.1 The encrypted result is in string format.
// Encrypt the Sensitive Data String
String encryptedContentBase64 = Cryptographer. encryptpolicric ("symatrix rovider", "password ");
1.2 The encrypted result is in bytep [] format.
byte[]valueToEncrypt=System.Text.Encoding .Unicode .GetBytes ("passowrd");
byte []encryptedContents=Cryptographer .EncryptSymmetric ("symmProvider",valueToEncrypt );
Array.Clear (valueToEncrypt ,0,valueToEncrypt .Length );
1.3 pay attention to the following two points during use
- Make sure that symatrix rovider is a symmetric encryption and decryption algorithm that exists in the configuration, and the appropriate algorithm is configured.
- Sensitive data should be cleared from memory in time. The Array. Clear method is used to store sensitive data in the memory.
2. Use symmetric encryption algorithms to decrypt data
2.1 decrypt a string
// Encrypt the Sensitive Data String
String encryptedContentBase64 = Cryptographer. EncryptSymmetric ("symatrix rovider", "SensitiveData ");
// Decrypt the base64 encoded string
String readableString = string. Empty;
ReadableString = Cryptographer. decryptequalric ("symatrix rovider", encryptedContentBase64 );
2.2 decrypt character array
Byte [] valueToEncrypt = System. Text. Encoding. Unicode. GetBytes ("passowrd ");
Byte [] encryptedContents = Cryptographer. EncryptSymmetric ("summProvider", valueToEncrypt );
Byte [] decryptContents = Cryptographer. DecryptSymmetric ("symatrix rovider", encryptedContentBase64 );
String plainText = (new System. Text. UnicodeEncoding (). GetString (decryptContents );
2.3 notes
Make sure that the correct algorithm provider is configured in the configuration file.
3. Obtain the hash value of the data.
3.1 obtain the hash value
Byte [] valutHash = (new System. Text. UnicodeEncoding (). GetBytes ("password ");
Byte [] generatedHash = Cryptographer. CreateHash ("hashProvider", valutHash );
Array. Clear (generatedHash, 0, generatedHash. Length );
3.2 notes
- The CreateHash method has two reloads. The difference is that the return value of the method is string and the return value is byte [].
- Make sure that the corresponding hash provider is configured in the configuration.
- It is dangerous to clear sensitive data in time and keep sensitive data in the memory. You should know that the value in the memory can be written back to the hard disk because the operating system will write the data to the swap file. If the system crashes, the system may throw data in the memory to the hard disk.
4. Check whether the hash value and text match
Byte [] valutHash = (new System. Text. UnicodeEncoding (). GetBytes ("password ");
Byte [] generatedHash = Cryptographer. CreateHash ("hashProvider", valutHash );
Byte [] stringToCompare = (new System. Text. UnicodeEncoding (). GetBytes ("TestValue ");
Bool comparisionSuccessed = Cryptographer. CompareHash ("hashProvider", stringToCompare, generatedHash );
Note that the appropriate hash provider must be configured.
Expand and modify the encryption and decryption module
1. Create a custom hash algorithm provider
1. Create a class
2. Add references to the sub-File
Using Microsoft. Practices. EnterpriseLibrary. Security. Cryptography;
Using Microsoft. Practices. EnterpriseLibrary. Security. Cryptography. Configuration;
3. Implement the IHashProvider interface for the class
4. Add the ConfigurationElementType feature and CustomHashProviderData as the feature parameter.
5. Add a constructor. The parameter is of the NameValueCollection type.
6. Two Methods for implementing Interfaces
Using System;
Using System. Collections. Generic;
Using Microsoft. Practices. EnterpriseLibrary. Common;
Using Microsoft. Practices. EnterpriseLibrary. Common. Configuration;
Using Microsoft. Practices. EnterpriseLibrary. Security. Cryptography;
Using Microsoft. Practices. EnterpriseLibrary. Security. Cryptography. Configuration;
Namespace BeautyCode. ConApp
{
[ConfigurationElementType (typeof (mhmhashproviderdata)]
Public class MyHashProvider: IHashProvider
{
Public MyHashProvider (System. Collections. Specialized. NameValueCollection attributes)
{
}
Public byte [] CreateHash (byte [] plaintext)
{
Throw new NotImplementedException ();
}
Public bool CompareHash (byte [] plaintext, byte [] hashedtext)
{
Throw new NotImplementedException ();
}
}
}
2. Create a custom symmetric encryption/decryption algorithm
2.1 Add a class file
2.2 add reference
Using System;
Using System. Collections. Generic;
Using Microsoft. Practices. EnterpriseLibrary. Common;
Using Microsoft. Practices. EnterpriseLibrary. Common. Configuration;
Using Microsoft. Practices. EnterpriseLibrary. Security. Cryptography;
Using Microsoft. Practices. EnterpriseLibrary. Security. Cryptography. Configuration;
2.3 Implementation interface isypolicriccryptoprovider
2.4 Add the ConfigurationElementType feature. The parameter is of the custompolicriccryptoproviderdata type.
2.5 Add a constructor. The parameter is of the NameValueCollection type.
2.6 Encrypt and Decrypt methods for implementing Interfaces
Using System;
Using System. Collections. Generic;
Using Microsoft. Practices. EnterpriseLibrary. Common;
Using Microsoft. Practices. EnterpriseLibrary. Common. Configuration;
Using Microsoft. Practices. EnterpriseLibrary. Security. Cryptography;
Using Microsoft. Practices. EnterpriseLibrary. Security. Cryptography. Configuration;
Namespace BeautyCode. ConApp
{
[ConfigurationElementType (typeof (CustomSymmetricCryptoProviderData)]
Public class my‑riccryptoprovider: isy‑riccryptoprovider
{
Public mydomainriccryptoprovider (System. Collections. Specialized. NameValueCollection attributes)
{}
Public byte [] Encrypt (byte [] plaintext)
{
Throw new NotImplementedException ();
}
Public byte [] Decrypt (byte [] ciphertext)
{
Throw new NotImplementedException ();
}
}
}