1 |
#Region "Microsoft.VisualBasic::76f6ffbd3409930cf0d209db5e69d946, Microsoft.VisualBasic.Core\Extensions\Security\TripleDES.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 |
#End Region
|
44 |
|
45 |
Imports System.IO
|
46 |
Imports System.Text
|
47 |
Imports System.Security.Cryptography
|
48 |
|
49 |
Namespace SecurityString
|
50 |
|
51 |
Public Class TripleDES : Inherits SecurityString.SecurityStringModel
|
52 |
|
53 |
Private ReadOnly _des As New TripleDESCryptoServiceProvider
|
54 |
Private ReadOnly _uni As New UnicodeEncoding
|
55 |
Private ReadOnly _key() As Byte
|
56 |
Private ReadOnly _iv() As Byte
|
57 |
|
58 |
|
59 |
|
60 |
</summary>
|
61 |
<param name="key">24byte</param>
|
62 |
<param name="iv">8byte</param>
|
63 |
<remarks></remarks>
|
64 |
Public Sub New(key() As Byte, iv() As Byte)
|
65 |
_key = key
|
66 |
_iv = iv
|
67 |
End Sub
|
68 |
|
69 |
Sub New(Optional password As String = "")
|
70 |
If Not String.IsNullOrEmpty(password) Then
|
71 |
strPassphrase = password
|
72 |
End If
|
73 |
|
74 |
Dim md5 = SecurityString.GetMd5Hash(strPassphrase)
|
75 |
md5 = md5 & md5 & md5 & md5
|
76 |
Dim bytes = _uni.GetBytes(md5)
|
77 |
If bytes.Length < 24 Then
|
78 |
bytes = {bytes, New Byte() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}}.ToVector
|
79 |
End If
|
80 |
_key = bytes.Take(24).ToArray
|
81 |
|
82 |
Dim n As Integer = bytes.Length / 3
|
83 |
_iv = _key.Take(8).Reverse.ToArray
|
84 |
End Sub
|
85 |
|
86 |
Public Overrides Function ToString() As String
|
87 |
Return strPassphrase
|
88 |
End Function
|
89 |
|
90 |
Public Overrides Function Encrypt(input() As Byte) As Byte()
|
91 |
Return InternalCryptoTransform(input, _des.CreateEncryptor(_key, _iv))
|
92 |
End Function
|
93 |
|
94 |
Public Overrides Function EncryptData(text As String) As String
|
95 |
Dim input() = _uni.GetBytes(text)
|
96 |
Dim output() = InternalCryptoTransform(input, _des.CreateEncryptor(_key, _iv))
|
97 |
Return Convert.ToBase64String(output)
|
98 |
End Function
|
99 |
|
100 |
Public Overrides Function Decrypt(input() As Byte) As Byte()
|
101 |
Return InternalCryptoTransform(input, _des.CreateDecryptor(_key, _iv))
|
102 |
End Function
|
103 |
|
104 |
Public Overrides Function DecryptString(text As String) As String
|
105 |
Dim input() = Convert.FromBase64String(text)
|
106 |
Dim output() = InternalCryptoTransform(input, _des.CreateDecryptor(_key, _iv))
|
107 |
Return _uni.GetString(output)
|
108 |
End Function
|
109 |
|
110 |
Private Function InternalCryptoTransform(input() As Byte, transform As ICryptoTransform) As Byte()
|
111 |
Dim result As Byte() = Nothing
|
112 |
|
113 |
Using ms As New MemoryStream
|
114 |
Using cs As New CryptoStream(ms, transform, CryptoStreamMode.Write)
|
115 |
cs.Write(input, 0, input.Length)
|
116 |
Try
|
117 |
cs.FlushFinalBlock()
|
118 |
Catch ex As Exception
|
119 |
Call Console.WriteLine(ex.ToString)
|
120 |
End Try
|
121 |
ms.Position = 0
|
122 |
Try
|
123 |
result = ms.ToArray()
|
124 |
Catch ex As Exception
|
125 |
Call Console.WriteLine(ex.ToString)
|
126 |
End Try
|
127 |
|
128 |
End Using
|
129 |
End Using
|
130 |
|
131 |
Return result
|
132 |
End Function
|
133 |
End Class
|
134 |
End Namespace
|