Skip to content

Commit c5c948f

Browse files
CopilotBillWagner
andauthored
Update obsolete cryptography APIs and improve security in Visual Basic encryption documentation (dotnet#48458)
* Initial plan * Update obsolete cryptography APIs to use modern .NET 6+ equivalents Co-authored-by: BillWagner <[email protected]> * Update from SHA-1 to SHA-256 for improved security Co-authored-by: BillWagner <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]>
1 parent c2fc4a3 commit c5c948f

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

docs/visual-basic/programming-guide/language-features/strings/walkthrough-encrypting-and-decrypting-strings.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ms.assetid: 1f51e40a-2f88-43e2-a83e-28a0b5c0d6fd
1111
---
1212
# Walkthrough: Encrypting and Decrypting Strings in Visual Basic
1313

14-
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.
14+
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.
1515

1616
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.
1717

@@ -21,28 +21,28 @@ This walkthrough shows you how to use the <xref:System.Security.Cryptography.DES
2121
> 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>.
2222
2323
### To create the encryption wrapper
24-
24+
2525
1. Create the `Simple3Des` class to encapsulate the encryption and decryption methods.
26-
26+
2727
[!code-vb[VbVbalrStrings#38](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class3.vb#38)]
28-
28+
2929
2. Add an import of the cryptography namespace to the start of the file that contains the `Simple3Des` class.
30-
30+
3131
[!code-vb[VbVbalrStrings#77](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class3.vb#77)]
32-
32+
3333
3. In the `Simple3Des` class, add a private field to store the 3DES cryptographic service provider.
34-
34+
3535
[!code-vb[VbVbalrStrings#39](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class3.vb#39)]
36-
36+
3737
4. Add a private method that creates a byte array of a specified length from the hash of the specified key.
38-
38+
3939
[!code-vb[VbVbalrStrings#41](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class3.vb#41)]
40-
41-
5. Add a constructor to initialize the 3DES cryptographic service provider.
42-
40+
41+
5. Add a constructor to initialize the 3DES cryptographic algorithm.
42+
4343
The `key` parameter controls the `EncryptData` and `DecryptData` methods.
44-
45-
[!code-vb[VbVbalrStrings#40](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class3.vb#40)]
44+
45+
[!code-vb[VbVbalrStrings#40](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class3.vb#40)]
4646

4747
6. Add a public method that encrypts a string.
4848

samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class3.vb

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ Class Class1f51e40a2f8843e2a83e28a0b5c0d6fd
6363
End Class
6464

6565
Public NotInheritable Class Simple3Des
66+
Implements IDisposable
67+
6668
' <snippet39>
67-
Private TripleDes As New TripleDESCryptoServiceProvider
69+
Private TripleDes As TripleDES = TripleDES.Create()
6870
' </snippet39>
6971

7072
' <snippet40>
@@ -80,16 +82,16 @@ Class Class1f51e40a2f8843e2a83e28a0b5c0d6fd
8082
ByVal key As String,
8183
ByVal length As Integer) As Byte()
8284

83-
Dim sha1 As New SHA1CryptoServiceProvider
84-
85-
' Hash the key.
86-
Dim keyBytes() As Byte =
87-
System.Text.Encoding.Unicode.GetBytes(key)
88-
Dim hash() As Byte = sha1.ComputeHash(keyBytes)
85+
Using sha256 As SHA256 = SHA256.Create()
86+
' Hash the key.
87+
Dim keyBytes() As Byte =
88+
System.Text.Encoding.Unicode.GetBytes(key)
89+
Dim hash() As Byte = sha256.ComputeHash(keyBytes)
8990

90-
' Truncate or pad the hash.
91-
ReDim Preserve hash(length - 1)
92-
Return hash
91+
' Truncate or pad the hash.
92+
ReDim Preserve hash(length - 1)
93+
Return hash
94+
End Using
9395
End Function
9496
' </snippet41>
9597

@@ -140,5 +142,9 @@ Class Class1f51e40a2f8843e2a83e28a0b5c0d6fd
140142
End Function
141143
' </snippet43>
142144

145+
Public Sub Dispose() Implements IDisposable.Dispose
146+
TripleDes?.Dispose()
147+
End Sub
148+
143149
End Class
144150
End Class

0 commit comments

Comments
 (0)