-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathmemoryexample.vb
More file actions
105 lines (85 loc) · 4 KB
/
memoryexample.vb
File metadata and controls
105 lines (85 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
' <SNIPPET1>
' WARNING: DES has a 56-bit key and is considered insecure.
' For new applications, use a NIST-approved symmetric encryption algorithm instead.
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
Module DESSample
Sub Main()
Try
Dim key As Byte()
Dim iv As Byte()
' Create a new DES object to generate a key
' and initialization vector (IV).
Using des As DES = DES.Create
key = des.Key
iv = des.IV
End Using
' Create a string to encrypt.
Dim original As String = "Here is some data to encrypt."
' Encrypt the string to an in-memory buffer.
Dim encrypted As Byte() = EncryptTextToMemory(original, key, iv)
' Decrypt the buffer back to a string.
Dim decrypted As String = DecryptTextFromMemory(encrypted, key, iv)
' Display the decrypted string to the console.
Console.WriteLine(decrypted)
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
Function EncryptTextToMemory(text As String, key As Byte(), iv As Byte()) As Byte()
Try
' Create a MemoryStream.
Using mStream As New MemoryStream
' Create a new DES object,
' Create a DES encryptor from the key and IV,
' Create a CryptoStream using the MemoryStream And encryptor
Using des As DES = DES.Create,
encryptor As ICryptoTransform = des.CreateEncryptor(key, iv),
cStream = New CryptoStream(mStream, encryptor, CryptoStreamMode.Write)
' Convert the passed string to a byte array.
Dim toEncrypt As Byte() = Encoding.UTF8.GetBytes(text)
' Write the byte array to the crypto stream and flush it.
cStream.Write(toEncrypt, 0, toEncrypt.Length)
' Ending the using block for the CryptoStream completes the encryption.
End Using
' Get an array of bytes from the MemoryStream that holds the encrypted data.
Dim ret As Byte() = mStream.ToArray()
' Return the encrypted buffer.
Return ret
End Using
Catch e As CryptographicException
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message)
Throw
End Try
End Function
Function DecryptTextFromMemory(encrypted As Byte(), key As Byte(), iv As Byte()) As String
Try
' Create a buffer to hold the decrypted data.
' DES-encrypted data will always be slightly bigger than the decrypted data.
Dim decrypted(encrypted.Length - 1) As Byte
Dim offset As Integer = 0
' Create a new MemoryStream using the provided array of encrypted data.
' Create a new DES object.
' Create a DES decryptor from the key and IV
' Create a CryptoStream using the MemoryStream and decryptor
Using mStream As New MemoryStream(encrypted),
des As DES = DES.Create,
decryptor As ICryptoTransform = des.CreateDecryptor(key, iv),
cStream = New CryptoStream(mStream, decryptor, CryptoStreamMode.Read)
' Keep reading from the CryptoStream until it finishes (returns 0).
Dim read As Integer = 1
While (read > 0)
read = cStream.Read(decrypted, offset, decrypted.Length - offset)
offset += read
End While
End Using
' Convert the buffer into a string and return it.
Return New ASCIIEncoding().GetString(decrypted, 0, offset)
Catch e As CryptographicException
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message)
Return Nothing
End Try
End Function
End Module
' </SNIPPET1>