1 |
#Region "Microsoft.VisualBasic::a02842f1a9fece5b27e2130dfce93204, Microsoft.VisualBasic.Core\Extensions\Security\SHA.vb"
|
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 |
#End Region
|
46 |
|
47 |
Imports System.Security
|
48 |
Imports System.Security.Cryptography
|
49 |
Imports System.IO
|
50 |
Imports System.Runtime.InteropServices
|
51 |
Imports System.Text.RegularExpressions
|
52 |
Imports System.Text
|
53 |
Imports Microsoft.VisualBasic.Linq.Extensions
|
54 |
|
55 |
Namespace SecurityString
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
Public Class SHA256 : Inherits SecurityStringModel
|
62 |
|
63 |
Const hashAlgorithm As String = "SHA1"
|
64 |
Const keySize As Integer = 256
|
65 |
Const passwordIterations As Integer = 2
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
Dim initVectorBytes As Byte()
|
73 |
Dim saltValueBytes As Byte()
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
<param name="password"></param>
|
79 |
<param name="saltValue">8 Bytes</param>
|
80 |
Sub New(password As String, saltValue As String)
|
81 |
Const initVector = "@1B2c3D4e5F6g7H8"
|
82 |
|
83 |
Me.strPassphrase = password
|
84 |
Me.initVectorBytes = Encoding.ASCII.GetBytes(initVector)
|
85 |
Me.saltValueBytes = Encoding.ASCII.GetBytes(saltValue)
|
86 |
End Sub
|
87 |
|
88 |
Public Overrides Function ToString() As String
|
89 |
Return MyBase.ToString() & $"// {NameOf(saltValueBytes)}:={String.Join(", ", saltValueBytes.Select(Of String)(Function(b) CStr(b)).ToArray)}"
|
90 |
End Function
|
91 |
|
92 |
Public ReadOnly Property Passphrase As String
|
93 |
Get
|
94 |
Return Me.strPassphrase
|
95 |
End Get
|
96 |
End Property
|
97 |
|
98 |
Public Overloads Overrides Function Decrypt(cipherTextBytes As Byte()) As Byte()
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
Dim password As New PasswordDeriveBytes(strPassphrase, saltValueBytes, hashAlgorithm, passwordIterations)
|
107 |
|
108 |
|
109 |
|
110 |
#Disable Warning
|
111 |
Dim keyBytes As Byte() = password.GetBytes(keySize \ 8)
|
112 |
#Enable Warning
|
113 |
|
114 |
|
115 |
Dim symmetricKey As New RijndaelManaged()
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
symmetricKey.Mode = CipherMode.CBC
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
Dim decryptor As ICryptoTransform = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)
|
127 |
|
128 |
|
129 |
|
130 |
Dim memoryStream As New MemoryStream(cipherTextBytes)
|
131 |
|
132 |
|
133 |
|
134 |
Dim cryptoStream As New CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
Dim plainTextBytes As Byte() = New Byte(cipherTextBytes.Length - 1) {}
|
141 |
|
142 |
|
143 |
|
144 |
Dim decryptedByteCount As Integer = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length)
|
145 |
|
146 |
|
147 |
|
148 |
memoryStream.Close()
|
149 |
cryptoStream.Close()
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
Return plainTextBytes
|
155 |
End Function
|
156 |
|
157 |
|
158 |
字符串的解密方法
|
159 |
|
160 |
<param name="cipherText"></param>
|
161 |
<returns></returns>
|
162 |
|
163 |
Public Overrides Function DecryptString(cipherText As String) As String
|
164 |
|
165 |
|
166 |
|
167 |
Dim cipherTextBytes As Byte() = Convert.FromBase64String(cipherText)
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
Dim password As New PasswordDeriveBytes(strPassphrase, saltValueBytes, hashAlgorithm, passwordIterations)
|
176 |
|
177 |
|
178 |
|
179 |
#Disable Warning
|
180 |
Dim keyBytes As Byte() = password.GetBytes(keySize \ 8)
|
181 |
#Enable Warning
|
182 |
|
183 |
|
184 |
Dim symmetricKey As New RijndaelManaged()
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
symmetricKey.Mode = CipherMode.CBC
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
Dim decryptor As ICryptoTransform = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)
|
196 |
|
197 |
|
198 |
|
199 |
Dim memoryStream As New MemoryStream(cipherTextBytes)
|
200 |
|
201 |
|
202 |
|
203 |
Dim cryptoStream As New CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
Dim plainTextBytes As Byte() = New Byte(cipherTextBytes.Length - 1) {}
|
210 |
|
211 |
|
212 |
|
213 |
Dim decryptedByteCount As Integer = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length)
|
214 |
|
215 |
|
216 |
|
217 |
memoryStream.Close()
|
218 |
cryptoStream.Close()
|
219 |
|
220 |
|
221 |
|
222 |
Dim plainText As String = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount)
|
223 |
Return plainText
|
224 |
End Function
|
225 |
|
226 |
Public Overloads Overrides Function Encrypt(plainTextBytes() As Byte) As Byte()
|
227 |
Dim password As New PasswordDeriveBytes(strPassphrase, saltValueBytes, hashAlgorithm, passwordIterations)
|
228 |
#Disable Warning
|
229 |
Dim keyBytes As Byte() = password.GetBytes(keySize \ 8)
|
230 |
#Enable Warning
|
231 |
Dim symmetricKey As New RijndaelManaged()
|
232 |
|
233 |
symmetricKey.Mode = CipherMode.CBC
|
234 |
|
235 |
Dim encryptor As ICryptoTransform = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)
|
236 |
Dim memoryStream As New MemoryStream()
|
237 |
Dim cryptoStream As New CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)
|
238 |
|
239 |
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)
|
240 |
cryptoStream.FlushFinalBlock()
|
241 |
|
242 |
Dim cipherTextBytes As Byte() = memoryStream.ToArray()
|
243 |
|
244 |
memoryStream.Close()
|
245 |
cryptoStream.Close()
|
246 |
|
247 |
Return cipherTextBytes
|
248 |
End Function
|
249 |
|
250 |
Public Function Serialization(data As Byte()) As String
|
251 |
data = Encrypt(data)
|
252 |
Return System.Text.Encoding.Unicode.GetString(data)
|
253 |
End Function
|
254 |
|
255 |
Public Function Deserialization(data As String) As Byte()
|
256 |
Dim Buffer As Byte() = System.Text.Encoding.Unicode.GetBytes(data)
|
257 |
Return Decrypt(Buffer)
|
258 |
End Function
|
259 |
|
260 |
|
261 |
Encrypt the plain text string.
|
262 |
|
263 |
<param name="plainText"></param>
|
264 |
<returns></returns>
|
265 |
Public Overrides Function EncryptData(plainText As String) As String
|
266 |
|
267 |
Dim plainTextBytes As Byte() = Encoding.UTF8.GetBytes(plainText)
|
268 |
Dim password As New PasswordDeriveBytes(strPassphrase, saltValueBytes, hashAlgorithm, passwordIterations)
|
269 |
#Disable Warning
|
270 |
Dim keyBytes As Byte() = password.GetBytes(keySize \ 8)
|
271 |
#Enable Warning
|
272 |
Dim symmetricKey As New RijndaelManaged()
|
273 |
|
274 |
symmetricKey.Mode = CipherMode.CBC
|
275 |
|
276 |
Dim encryptor As ICryptoTransform = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)
|
277 |
Dim memoryStream As New MemoryStream()
|
278 |
Dim cryptoStream As New CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)
|
279 |
|
280 |
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)
|
281 |
cryptoStream.FlushFinalBlock()
|
282 |
|
283 |
Dim cipherTextBytes As Byte() = memoryStream.ToArray()
|
284 |
|
285 |
memoryStream.Close()
|
286 |
cryptoStream.Close()
|
287 |
|
288 |
Dim cipherText As String = Convert.ToBase64String(cipherTextBytes)
|
289 |
|
290 |
Return cipherText
|
291 |
End Function
|
292 |
|
293 |
|
294 |
The previous key of the sha256 encryption will be expired after the rebuild of this module,
|
295 |
so that this method is not working on the statics data storage job.
|
296 |
(在本模块进行重新编译之后,原有的密匙将会失效,故这个属性不适合于静态存储加密使用)
|
297 |
|
298 |
<value></value>
|
299 |
<returns></returns>
|
300 |
|
301 |
Public Shared ReadOnly Property CertificateSigned As SHA256 =
|
302 |
New SHA256(password:=Microsoft.VisualBasic.SecurityString.GetFileHashString(GetType(SHA256).Assembly.Location), saltValue:="ATCGCGAT")
|
303 |
|
304 |
|
305 |
双重动态数据签名
|
306 |
|
307 |
<typeparam name="T"></typeparam>
|
308 |
<returns></returns>
|
309 |
|
310 |
Public Shared Function GetDynamicsCertification(Of T)() As SHA256
|
311 |
Return GetDynamicsCertification(GetType(T))
|
312 |
End Function
|
313 |
|
314 |
|
315 |
双重动态数据签名
|
316 |
|
317 |
<returns></returns>
|
318 |
|
319 |
Public Shared Function GetDynamicsCertification(TypeInfo As Type) As SHA256
|
320 |
Dim Password As String = Microsoft.VisualBasic.SecurityString.GetFileHashString(TypeInfo.Assembly.Location)
|
321 |
Dim saltValue As String = Microsoft.VisualBasic.SecurityString.GetFileHashString(GetType(SHA256).Assembly.Location)
|
322 |
saltValue = Mid(saltValue, 3, 8)
|
323 |
Return New SHA256(Password, saltValue)
|
324 |
End Function
|
325 |
End Class
|
326 |
End Namespace
|