Microsoft enterprise database 4.1 learning notes (23) encryption and decryption module 3 sample code

Source: Internet
Author: User

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 ();
}
}

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.