Criptografia

Solo disponible en BuenasTareas
  • Páginas : 7 (1648 palabras )
  • Descarga(s) : 0
  • Publicado : 9 de diciembre de 2010
Leer documento completo
Vista previa del texto
14

Module 7: Implementing Cryptography in .NET

Practice: Encrypting and Decrypting Data with a Symmetric Algorithm

*****************************ILLEGAL FOR NON-TRAINER USE****************************** Introduction Length Instructions In this practice, you will implement symmetric encryption and decryption with a password. 30 minutes Log on to 2840A-LONDON as 2840A-LONDON\Administratorwith a password of P@ssw0rd.

! Add the namespace reference for the cryptography namespace
1. Open the symmetric cryptography solution for the language in which you prefer to work. The solution file, named SymmetricCrypto.sln for both Microsoft Visual C#™ and Microsoft Visual Basic®, is located in the following folders: install_folder\Practices\Mod07\Practice01\C#\Starterinstall_folder\Practices\Mod07\Practice01\VB\Starter 2. In Solution Explorer, double-click MainForm.cs or MainForm.vb. 3. Press F7 to view the code for the form. 4. Show TODO comments in the Task List. To show TODO comments, click the View menu, point to Show Tasks, and then click All.

Module 7: Implementing Cryptography in .NET

15

5. Double-click TODO 1 comment in the Task List to move to the comment in thecode. This comment is near the top of the file, after the namespace references. 6. Add the namespace reference for the System.Security.Cryptography namespace.
//C# // TODO 1: Add the namespace reference for the cryptography // namespace using System.Security.Cryptography; 'Visual Basic ' TODO 1: Add the namespace reference for the cryptography ' namespace Imports System.Security.Cryptography

!Add the code to encrypt the data
1. Continuing in the same file, double-click the TODO 2 comment in the Task List. The next seven TODO comments are in the encryptData method. 2. Add the code to create the random number, or salt, which seeds the key generation algorithm.
//C# // TODO 2: Create the salt RandomNumberGenerator randNumGen = RandomNumberGenerator.Create(); byte [] salt = new byte[16];randNumGen.GetBytes(salt); 'Visual Basic ' TODO 2: Create the salt Dim randNumGen As RandomNumberGenerator = _ RandomNumberGenerator.Create() Dim salt(15) As Byte randNumGen.GetBytes(salt)

3. Double-click the TODO 3 comment in the Task List. 4. Add the code to generate the key from the password and salt.
//C# // TODO 3: Generate the key from the password and salt PasswordDeriveBytes passBytes= new PasswordDeriveBytes(password, salt); byte [] key = passBytes.GetBytes(16); 'Visual Basic ' TODO 3: Generate the key from the password and salt Dim passBytes As New PasswordDeriveBytes(password, salt) Dim key() As Byte = passBytes.GetBytes(16)

5. Double-click the TODO 4 comment in the Task List.

16

Module 7: Implementing Cryptography in .NET

6. Add the code to create andconfigure the algorithm object.
//C# // TODO 4: Create and configure the algorithm Rijndael cryptoAlg = Rijndael.Create(); cryptoAlg.Key = key; 'Visual Basic ' TODO 4: Create and configure the algorithm Dim cryptoAlg As Rijndael = Rijndael.Create() cryptoAlg.Key = key

7. Double-click the TODO 5 comment in the Task List. 8. Add the code to write the salt and the initialization vector to the outputstream unencrypted.
//C# // TODO 5: Write the salt and IV to the output // stream unencrypted dataStream.Write(salt, 0, salt.Length); dataStream.Write(cryptoAlg.IV, 0, cryptoAlg.IV.Length); 'Visual Basic ' TODO 5: Write the salt and IV to the output ' stream unencrypted dataStream.Write(salt, 0, salt.Length) dataStream.Write(cryptoAlg.IV, 0, cryptoAlg.IV.Length)

9. Double-click the TODO 6 commentin the Task List. 10. Add the code to create the CryptoStream object. The CryptoStream object wraps the output stream and encrypts the data.
//C# // TODO 6: Create the CryptoStream CryptoStream cryptoStream = new CryptoStream( dataStream, cryptoAlg.CreateEncryptor(), CryptoStreamMode.Write); 'Visual Basic ' TODO 6: Create the CryptoStream Dim cryptoStream As New CryptoStream(dataStream, _...
tracking img