Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Symmetric Algorithms Sample 

Download sample

This sample uses the Rijndael and TripleDESCryptoServiceProvider classes to implement a symmetric (secret-key) encryption, as described in the Cryptography Overview.

Security noteSecurity Note

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

  1. Click the Download Sample button on this page. In the File Download dialog box, click Open.

  2. Save the files to your computer.

  3. On the File menu in Visual Studio, point to Open, and then click Project/Solution.

  4. Browse to the folder where you saved the files and select the solution file. This opens the solution in Visual Studio.

  5. Press F5.

Demonstrates

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 SymmetricAlgorithm. 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.

See Also

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft