Symmetric Algorithms Sample
This sample uses theand classes to implement a symmetric (secret-key) encryption, as described in the .
This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.
To run this sample
Click the Download Sample button on this page. In the File Download dialog box, click Open.
Save the files to your computer.
On the File menu in Visual Studio, point to Open, and then click Project/Solution.
Browse to the folder where you saved the files and select the solution file. This opens the solution in Visual Studio.
The SampleCrypto class encapsulates the encryption algorithm, the salt and initialization vector (IV), and encryption and decryption routines that define one encrypted file. The constructor takes one parameter that determines the encryption type, Rijndael or TripleDES. The corresponding field in the class, crpSym, is of the abstract type. It is set to an instance of the Rijndael class or TripleDESCryptoServiceProvider class. The CreateSaltIVFile method saves the salt and IV values to a .dat file. The Decrypt and Encrypt methods process the source file that is specified in the SourceFileName property.
One instance of the SampleCrypto class is created when the form loads. The controls on the form demonstrate the methods and properties of the SampleCrypto class. When the Encrypt with Password control is checked, then the key is not generated and set automatically but is rather derived from a password that has been "salted". Moreover, the salt and initialization vector (IV) are persisted unencrypted to a .dat file. In this way you can see how you would securely send a document to someone: The encrypted document could be sent over an insecure wire, and the .dat file and password could be transferred securely using asymmetric (or public key) encryption. This type of encryption is the most secure form of encryption, but it takes much longer to process. This is why it is typically used for small items like a secret key, salt/IV file, or password.