Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ms.assetid: 1f51e40a-2f88-43e2-a83e-28a0b5c0d6fd
---
# Walkthrough: Encrypting and Decrypting Strings in Visual Basic

This walkthrough shows you how to use the <xref:System.Security.Cryptography.DESCryptoServiceProvider> class to encrypt and decrypt strings using the cryptographic service provider (CSP) version of the Triple Data Encryption Standard (<xref:System.Security.Cryptography.TripleDES>) algorithm. The first step is to create a simple wrapper class that encapsulates the 3DES algorithm and stores the encrypted data as a base-64 encoded string. Then, that wrapper is used to securely store private user data in a publicly accessible text file.
This walkthrough shows you how to use the <xref:System.Security.Cryptography.TripleDES> class to encrypt and decrypt strings using the Triple Data Encryption Standard (3DES) algorithm. The first step is to create a simple wrapper class that encapsulates the 3DES algorithm and stores the encrypted data as a base-64 encoded string. Then, that wrapper is used to securely store private user data in a publicly accessible text file.

You can use encryption to protect user secrets (for example, passwords) and to make credentials unreadable by unauthorized users. This can protect an authorized user's identity from being stolen, which protects the user's assets and provides non-repudiation. Encryption can also protect a user's data from being accessed by unauthorized users.

Expand All @@ -21,28 +21,28 @@ This walkthrough shows you how to use the <xref:System.Security.Cryptography.DES
> The Rijndael (now referred to as Advanced Encryption Standard [AES]) and Triple Data Encryption Standard (3DES) algorithms provide greater security than DES because they are more computationally intensive. For more information, see <xref:System.Security.Cryptography.DES> and <xref:System.Security.Cryptography.Rijndael>.

### To create the encryption wrapper

1. Create the `Simple3Des` class to encapsulate the encryption and decryption methods.

[!code-vb[VbVbalrStrings#38](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class3.vb#38)]

2. Add an import of the cryptography namespace to the start of the file that contains the `Simple3Des` class.

[!code-vb[VbVbalrStrings#77](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class3.vb#77)]

3. In the `Simple3Des` class, add a private field to store the 3DES cryptographic service provider.

[!code-vb[VbVbalrStrings#39](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class3.vb#39)]

4. Add a private method that creates a byte array of a specified length from the hash of the specified key.

[!code-vb[VbVbalrStrings#41](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class3.vb#41)]
5. Add a constructor to initialize the 3DES cryptographic service provider.

5. Add a constructor to initialize the 3DES cryptographic algorithm.

The `key` parameter controls the `EncryptData` and `DecryptData` methods.
[!code-vb[VbVbalrStrings#40](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class3.vb#40)]

[!code-vb[VbVbalrStrings#40](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class3.vb#40)]

6. Add a public method that encrypts a string.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ Class Class1f51e40a2f8843e2a83e28a0b5c0d6fd
End Class

Public NotInheritable Class Simple3Des
Implements IDisposable

' <snippet39>
Private TripleDes As New TripleDESCryptoServiceProvider
Private TripleDes As TripleDES = TripleDES.Create()
' </snippet39>

' <snippet40>
Expand All @@ -80,16 +82,16 @@ Class Class1f51e40a2f8843e2a83e28a0b5c0d6fd
ByVal key As String,
ByVal length As Integer) As Byte()

Dim sha1 As New SHA1CryptoServiceProvider

' Hash the key.
Dim keyBytes() As Byte =
System.Text.Encoding.Unicode.GetBytes(key)
Dim hash() As Byte = sha1.ComputeHash(keyBytes)
Using sha256 As SHA256 = SHA256.Create()
' Hash the key.
Dim keyBytes() As Byte =
System.Text.Encoding.Unicode.GetBytes(key)
Dim hash() As Byte = sha256.ComputeHash(keyBytes)

' Truncate or pad the hash.
ReDim Preserve hash(length - 1)
Return hash
' Truncate or pad the hash.
ReDim Preserve hash(length - 1)
Return hash
End Using
End Function
' </snippet41>

Expand Down Expand Up @@ -140,5 +142,9 @@ Class Class1f51e40a2f8843e2a83e28a0b5c0d6fd
End Function
' </snippet43>

Public Sub Dispose() Implements IDisposable.Dispose
TripleDes?.Dispose()
End Sub

End Class
End Class