11Imports System.IO
22Imports System.Security.Cryptography
3+ Imports System.Text
34
45Public Class AESEngine
56
67 Public Shared Function Encrypt(clearData As Byte (), Key As Byte (), IV As Byte (), cipherMode As CipherMode, paddingMode As PaddingMode) As Byte ()
7- Dim ms As New MemoryStream()
8- Dim alg As Rijndael = Rijndael.Create()
9-
10- alg.Mode = cipherMode
11- alg.Padding = paddingMode
12- alg.Key = Key
13- alg.IV = IV
14-
15- Dim cs As New CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write)
16- cs.Write(clearData, 0 , clearData.Length)
17- cs.Close()
18-
19- Dim encryptedData As Byte () = ms.ToArray()
20-
21- Return encryptedData
8+ Using ms As New MemoryStream()
9+ Using alg As Aes = Aes.Create()
10+ alg.Mode = cipherMode
11+ alg.Padding = paddingMode
12+ alg.Key = Key
13+ alg.IV = IV
14+
15+ Using cs As New CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write)
16+ cs.Write(clearData, 0 , clearData.Length)
17+ End Using
18+ End Using
19+ Return ms.ToArray()
20+ End Using
2221 End Function
2322
2423 Public Shared Function Encrypt(clearText As String , Password As String , cipherMode As CipherMode, paddingMode As PaddingMode) As String
25- Dim clearBytes As Byte () = Text. Encoding.Unicode.GetBytes(clearText)
26- Dim pdb As New PasswordDeriveBytes(Password, New Byte () { &H49 , &H76 , &H61 , &H6E , &H20 , &H4D , &H65 , &H64 , &H76 , &H65 , &H64 , &H65 , &H76 })
27- Dim encryptedData As Byte () = Encrypt(clearBytes, pdb.GetBytes( 32 ), pdb.GetBytes( 16 ), cipherMode, paddingMode)
24+ Dim clearBytes As Byte () = Encoding.Unicode.GetBytes(clearText)
25+ Dim salt As Byte () = New Byte () { &H49 , &H76 , &H61 , &H6E , &H20 , &H4D , &H65 , &H64 , &H76 , &H65 , &H64 , &H65 , &H76 }
26+ Dim pdb As New Rfc2898DeriveBytes(Password, salt, 10000 , HashAlgorithmName.SHA256) ' New overload using SHA256
2827
28+ Dim encryptedData As Byte () = Encrypt(clearBytes, pdb.GetBytes( 32 ), pdb.GetBytes( 16 ), cipherMode, paddingMode)
2929 Return Convert.ToBase64String(encryptedData)
3030 End Function
3131
3232 Public Shared Function Encrypt(clearData As Byte (), Password As String , cipherMode As CipherMode, paddingMode As PaddingMode) As Byte ()
33- Dim pdb As New PasswordDeriveBytes(Password, New Byte () { &H49 , &H76 , &H61 , &H6E , &H20 , &H4D , &H65 , &H64 , &H76 , &H65 , &H64 , &H65 , &H76 })
33+ Dim salt As Byte () = New Byte () { &H49 , &H76 , &H61 , &H6E , &H20 , &H4D , &H65 , &H64 , &H76 , &H65 , &H64 , &H65 , &H76 }
34+ Dim pdb As New Rfc2898DeriveBytes(Password, salt, 10000 , HashAlgorithmName.SHA256)
3435 Return Encrypt(clearData, pdb.GetBytes( 32 ), pdb.GetBytes( 16 ), cipherMode, paddingMode)
3536 End Function
3637
3738 Public Shared Sub Encrypt(fileIn As String , fileOut As String , Password As String , cipherMode As CipherMode, paddingMode As PaddingMode)
38- ' First we are going to open the file streams
39- Dim fsIn As New FileStream(fileIn, FileMode.Open, FileAccess.Read)
40- Dim fsOut As New FileStream(fileOut, FileMode.OpenOrCreate, FileAccess.Write)
41-
42- ' Then we are going to derive a Key and an IV from the
43- ' Password and create an algorithm
44-
45- Dim pdb As New PasswordDeriveBytes(Password, New Byte () { &H49 , &H76 , &H61 , &H6E , &H20 , &H4D , _
46- &H65 , &H64 , &H76 , &H65 , &H64 , &H65 , _
47- &H76 })
48-
49- Dim alg As Rijndael = Rijndael.Create()
50-
51- alg.Mode = cipherMode
52- alg.Padding = paddingMode
53- alg.Key = pdb.GetBytes( 32 )
54- alg.IV = pdb.GetBytes( 16 )
55-
56- ' Now create a crypto stream through which we are going
57- ' to be pumping data.
58- ' Our fileOut is going to be receiving the encrypted bytes.
59-
60- Dim cs As New CryptoStream(fsOut, alg.CreateEncryptor(), CryptoStreamMode.Write)
61-
62- ' Now will will initialize a buffer and will be processing
63- ' the input file in chunks.
64- ' This is done to avoid reading the whole file (which can
65- ' be huge) into memory.
66-
67- Dim bufferLen As Integer = 4096
68- Dim buffer As Byte () = New Byte (bufferLen - 1 ) {}
69- Dim bytesRead As Integer
70-
71- Do
72- ' read a chunk of data from the input file
73- bytesRead = fsIn.Read(buffer, 0 , bufferLen)
74-
75- ' encrypt it
76- cs.Write(buffer, 0 , bytesRead)
77- Loop While bytesRead <> 0
78-
79- cs.Close()
80- fsIn.Close()
39+ Dim salt As Byte () = New Byte () { &H49 , &H76 , &H61 , &H6E , &H20 , &H4D , &H65 , &H64 , &H76 , &H65 , &H64 , &H65 , &H76 }
40+ Dim pdb As New Rfc2898DeriveBytes(Password, salt, 10000 , HashAlgorithmName.SHA256)
41+ Using fsIn As New FileStream(fileIn, FileMode.Open, FileAccess.Read),
42+ fsOut As New FileStream(fileOut, FileMode.OpenOrCreate, FileAccess.Write)
43+ Using alg As Aes = Aes.Create()
44+ alg.Mode = cipherMode
45+ alg.Padding = paddingMode
46+ alg.Key = pdb.GetBytes( 32 )
47+ alg.IV = pdb.GetBytes( 16 )
48+
49+ Using cs As New CryptoStream(fsOut, alg.CreateEncryptor(), CryptoStreamMode.Write)
50+ Dim bufferLen As Integer = 4096
51+ Dim buffer As Byte () = New Byte (bufferLen - 1 ) {}
52+ Dim bytesRead As Integer
53+
54+ Do
55+ bytesRead = fsIn.Read(buffer, 0 , bufferLen)
56+ If bytesRead > 0 Then
57+ cs.Write(buffer, 0 , bytesRead)
58+ End If
59+ Loop While bytesRead <> 0
60+ End Using
61+ End Using
62+ End Using
8163 End Sub
8264
8365 Public Shared Function Decrypt(cipherData As Byte (), Key As Byte (), IV As Byte (), cipherMode As CipherMode, paddingMode As PaddingMode) As Byte ()
84- Dim ms As New MemoryStream()
85- Dim alg As Rijndael = Rijndael.Create()
86-
87- alg.Mode = cipherMode
88- alg.Padding = paddingMode
89- alg.Key = Key
90- alg.IV = IV
91-
92- Dim cs As New CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write)
93-
94- cs.Write(cipherData, 0 , cipherData.Length)
95- cs.Close()
96-
97- Dim decryptedData As Byte () = ms.ToArray()
98-
99- Return decryptedData
66+ Using ms As New MemoryStream()
67+ Using alg As Aes = Aes.Create()
68+ alg.Mode = cipherMode
69+ alg.Padding = paddingMode
70+ alg.Key = Key
71+ alg.IV = IV
72+
73+ Using cs As New CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write)
74+ cs.Write(cipherData, 0 , cipherData.Length)
75+ End Using
76+ End Using
77+ Return ms.ToArray()
78+ End Using
10079 End Function
10180
10281 Public Shared Function Decrypt(cipherText As String , Password As String , cipherMode As CipherMode, paddingMode As PaddingMode) As String
10382 Dim cipherBytes As Byte () = Convert.FromBase64String(cipherText)
104- Dim pdb As New PasswordDeriveBytes(Password, New Byte () { &H49 , &H76 , &H61 , &H6E , &H20 , &H4D , &H65 , &H64 , &H76 , &H65 , &H64 , &H65 , &H76 })
83+ Dim salt As Byte () = New Byte () { &H49 , &H76 , &H61 , &H6E , &H20 , &H4D , &H65 , &H64 , &H76 , &H65 , &H64 , &H65 , &H76 }
84+ Dim pdb As New Rfc2898DeriveBytes(Password, salt, 10000 , HashAlgorithmName.SHA256)
10585 Dim decryptedData As Byte () = Decrypt(cipherBytes, pdb.GetBytes( 32 ), pdb.GetBytes( 16 ), cipherMode, paddingMode)
106-
107- Return Text.Encoding.Unicode.GetString(decryptedData)
86+ Return Encoding.Unicode.GetString(decryptedData)
10887 End Function
10988
11089 Public Shared Function Decrypt(cipherData As Byte (), Password As String , cipherMode As CipherMode, paddingMode As PaddingMode) As Byte ()
111- Dim pdb As New PasswordDeriveBytes(Password, New Byte () { &H49 , &H76 , &H61 , &H6E , &H20 , &H4D , &H65 , &H64 , &H76 , &H65 , &H64 , &H65 , &H76 })
112-
90+ Dim salt As Byte () = New Byte () { &H49 , &H76 , &H61 , &H6E , &H20 , &H4D , &H65 , &H64 , &H76 , &H65 , &H64 , &H65 , &H76 }
91+ Dim pdb As New Rfc2898DeriveBytes(Password, salt, 10000 , HashAlgorithmName.SHA256)
11392 Return Decrypt(cipherData, pdb.GetBytes( 32 ), pdb.GetBytes( 16 ), cipherMode, paddingMode)
114-
11593 End Function
11694
11795 Public Shared Sub Decrypt(fileIn As String , fileOut As String , Password As String , cipherMode As CipherMode, paddingMode As PaddingMode)
118- Dim fsIn As New FileStream(fileIn, FileMode.Open, FileAccess.Read)
119- Dim fsOut As New FileStream(fileOut, FileMode.OpenOrCreate, FileAccess.Write )
120-
121- Dim pdb As New PasswordDeriveBytes(Password, New Byte () { &H49 , &H76 , &H61 , &H6E , &H20 , &H4D , &H65 , &H64 , &H76 , &H65 , &H64 , &H65 , &H76 } )
122- Dim alg As Rijndael = Rijndael .Create()
123-
124- alg.Mode = cipherMode
125- alg.Padding = paddingMode
126- alg.Key = pdb.GetBytes( 32 )
127- alg.IV = pdb.GetBytes( 16 )
128-
129- Dim cs As New CryptoStream(fsOut, alg.CreateDecryptor(), CryptoStreamMode.Write)
130- Dim bufferLen As Integer = 4096
131- Dim buffer As Byte () = New Byte (bufferLen - 1 ) {}
132- Dim bytesRead As Integer
133-
134- Do
135- bytesRead = fsIn.Read(buffer, 0 , bufferLen)
136-
137- cs.Write(buffer, 0 , bytesRead)
138- Loop While bytesRead <> 0
139-
140- cs.Close()
141- fsIn.Close()
96+ Dim salt As Byte () = New Byte () { &H49 , &H76 , &H61 , &H6E , &H20 , &H4D , &H65 , &H64 , &H76 , &H65 , &H64 , &H65 , &H76 }
97+ Dim pdb As New Rfc2898DeriveBytes(Password, salt, 10000 , HashAlgorithmName.SHA256 )
98+ Using fsIn As New FileStream(fileIn, FileMode.Open, FileAccess.Read),
99+ fsOut As New FileStream(fileOut, FileMode.OpenOrCreate, FileAccess.Write )
100+ Using alg As Aes = Aes .Create()
101+ alg.Mode = cipherMode
102+ alg.Padding = paddingMode
103+ alg.Key = pdb.GetBytes( 32 )
104+ alg.IV = pdb.GetBytes( 16 )
105+
106+ Using cs As New CryptoStream(fsOut, alg.CreateDecryptor(), CryptoStreamMode.Write)
107+ Dim bufferLen As Integer = 4096
108+ Dim buffer As Byte () = New Byte (bufferLen - 1 ) {}
109+ Dim bytesRead As Integer
110+
111+ Do
112+ bytesRead = fsIn.Read(buffer, 0 , bufferLen)
113+ If bytesRead > 0 Then
114+ cs.Write(buffer, 0 , bytesRead)
115+ End If
116+ Loop While bytesRead <> 0
117+ End Using
118+ End Using
119+ End Using
142120 End Sub
143121
144- End Class
122+ End Class
0 commit comments