ArticleDirectory
- Roaming: encryption secrets
- Roaming: decrypts secrets
- Roaming: Get hash from data
- Roaming: Verify that the hash value matches certain text
This article maintains in: http://wiki.entlib.net.cn/EntlibHelp31CryptographyApplicationBlock.ashx
Enterprise Library Quick Start is a simple and easy-to-understand applicationProgramAn example of the key features of a block is used to describe these features using a roaming set that implements common scenarios.
If you want to understand an Application Block, the Quick Start is the ideal starting point, and you can use the testSource codeIt is also very comfortable to learn new technologies. If you are familiar with the. NET Framework and want to check whether it is simple and helpful to understand how to solve specific problemsCodeFor example, they are very good resources.
To use all the advantages of Quick Start, you need to be familiar with the concepts and technologies related to object-oriented programming.
Quick Start to encryption
The Quick Start of encryption application blocks demonstrates the following scenarios:
- Encryption secrets
- Decrypt secrets
- Obtain a hash from text
- Check whether the hash matches certain text
System Requirements
To build and run Quick Start, you need the following:
- Microsoft Windows 2000, Windows XP Professional, or Windows Server 2003 Operating System
- Microsoft. NET Framework, version 2.0 or 3.0
- Microsoft Visual Studio 2005 Development System
Quick Start does not need to perform any installation steps before building and running applications.
Build Quick Start
Quick Start is in the form of source code, which means you must compile it before running it. You can use Visual Studio to build a quick start.
Build Quick Start
- Confirm that the Enterprise Library kernel is installed.
- Open the source code of the Enterprise Library from winidows resource manager, or clickStart, PointingProgram, PointingMicrosoft patterns and practices, PointingEnterprise Library 3.1-May 2007And then selectEnterprise Library 3.1 source folder.
- OpenQuickstartsFolder, and thenCryptographyAnd thenCS(For C #) orVB(For Visual Basic. net ).
- Double-clickCryptographyquickstart. slnIcon.
- Visual Studio opens and displays the solution file. In the menu, clickGenerate.
- ClickRegenerate Solution. By default, this is a debug build.
- Press F5 To Run quick start.
Quick Start Configuration
A default configuration is included in the app. config file. This file is in the same directory as the Quick Start project file.
To modify or view these settings, use the Enterprise Library configuration console to open the app. config file in the directory containing the Quick Start project file. The app. config file contains configuration data.
Each time you build code, Visual Studio copies the app. config file to the output directory (the directory for creating the Quick Start Executable File) for the project and changes it to cryptographyquickstart.exe. config.
This
This means that if you want to use the configuration console to modify any configuration settings, such as the encryption key and the solution to be rebuilt, you must open the app. config in the Quick Start source code directory.
File to modify the configuration. You can also use the Enterprise Library configuration console to open
Cryptographyquickstart.exe. config
To modify the application configuration. However, these modifications will be overwritten during the next successful build. The encryption Quick Start contains the following default configurations:
Name : Hash provider
Type : Sha1managed
Saltenabled : True
Name : Symprovider
Type : Microsoft. Practices. enterpriselibrary. Security. cryptography. symmetricalgorithmprovider
Algorithmtype : Rijndaelmanaged
Protectedkeyfilename : <Installdir> "quickstarts" Cryptography "symmetrickeyfile.txt
Note: : The Quick Start method createkeys generates a key and saves it in this file.
Protectedkeyprotectionscope : Localmachine
Roaming: encryption secrets
This roaming shows how to use an encrypted application block to encrypt some text.
Reconstruction example
1. Declare appropriate variables and constants. C #
Const string symatrix rovider = "symprovider ";
String stringtoencrypt;
String encryptedcontentsbase64;
Visual Basic
Const symatrix rovider as string = "symprovider"
Dim stringtoencrypt as string
Dim encryptedcontentsbase64 as string
2. Get text from the user C #
Stringtoencrypt = "sample text ";
Visual Basic
Stringtoencrypt = "sample text"
3. CallCryptographerClass static methodEncryptsymmetricEncryption is completed. The method supports the use of configured symmetric provider names. C #
Encryptedcontentsbase64 = cryptographer. encryptpolicric (symatrix rovider, stringtoencrypt );
Visual Basic
Encryptedcontentsbase64 = cryptographer. encryptpolicric (symatrix rovider, stringtoencrypt)
Roaming: decrypts secrets
This roaming shows how to use encrypted application blocks to decrypt text.
Reconstruction example
1. Declare appropriate variables and constants. C #
Const string symatrix rovider = "symprovider ";
String encryptedcontentsbase64;
Visual Basic
Const symatrix rovider as string = "symprovider"
Dim encryptedcontentsbase64 as string
2. CallCryptographerClass static methodDecryptsymmetricThe method supports the use of the configured symmetric provider name. The following code assumes that the member variableEncryptedcontentsContains strings encrypted using the same symmetric provider. C #
String decryptedcontents = cryptographer. decryptsymmetric (symatrix rovider, encryptedcontentsbase64 );
Visual Basic
Dim decryptedcontents as string = cryptographer. decryptsymmetric (symatrix rovider, encryptedcontentsbase64)
Roaming: Get hash from data
This roaming shows how to use an encrypted application block to obtain a hash from some text.
Reconstruction example
1. Declare appropriate variables and constants. C #
Private const string hashprovider = "hashprovider ";
Private string stringforhash;
Private byte [] generatedhash;
Visual Basic
Const hashprovider as string = "hashprovider"
Dim stringforhash as string
Dim generatedhash as byte ()
2. obtain data from the user. C #
Byte [] plaintext = encoding. utf8.getbytes ("password ");
Visual Basic
Dim valuetohash as byte () = encoding. utf8.getbytes ("password ")
3. CallCryptographerClass static methodCreatehashThe method supports the use of the configured hash provider name. C #
Generatedhash = cryptographer. createhash (hashprovider, valuetohash );
Visual Basic
Generatedhash = cryptographer. createhash (hashprovider, valuetohash)
Roaming: Verify that the hash value matches certain text
This roaming model demonstrates how to use an encrypted application block to verify that the previously generated hash matches the hash of some text.
Reconstruction example
1. Declare appropriate variables and constants c #
Const string hashprovider = "hashprovider ";
Byte [] generatedhash;
Visual Basic
Const hashprovider as string = "hashprovider"
Dim generatedhash as byte ()
2. Obtain Text C # From the user #
Byte [] plaintext = encoding. utf8.getbytes ("password ");
Visual Basic
Dim valuetohash as byte () = encoding. utf8.getbytes ("password ")
3. CallCryptographerClass static methodComparehashYou can use the name of the hash provider. This Code assumes that the member variableGeneratedhashContains the hash previously generated for some text. C #
Bool comparisonsucceeded = cryptographer. comparehash (hashprovider, valuetohash, generatedhash );
Visual Basic
Dim comparisonsucceeded as Boolean = cryptographer. comparehash (hashprovider, valuetohash, generatedhash)