1 #Region "Microsoft.VisualBasic::e5cfa890b656e93e3a8988f76e1e286f, Microsoft.VisualBasic.Core\Extensions\Security\AES.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class AES
35     
36     '         Constructor: (+1 OverloadsSub New
37     '         Function: Decrypt, DecryptString, Encrypt, EncryptData
38     
39     
40     ' /********************************************************************************/
41
42 #End Region
43
44 Imports System.Runtime.CompilerServices
45 Imports System.Security.Cryptography
46 Imports System.Text
47 Imports Microsoft.VisualBasic.Text
48
49 Namespace SecurityString
50
51     Public Class AES : Inherits SecurityStringModel
52
53         ReadOnly textEncoding As Encoding
54
55         Sub New(pass$, Optional encoding As Encodings = Encodings.UTF8WithoutBOM)
56             Me.strPassphrase = pass
57             Me.textEncoding = encoding.CodePage
58         End Sub
59
60         Public Overrides Function Decrypt(input() As ByteAs Byte()
61             Dim AES As New RijndaelManaged
62             Dim hashAES As New MD5CryptoServiceProvider
63             Dim hash(31) As Byte
64             Dim temp As Byte() = hashAES.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strPassphrase))
65             Array.Copy(temp, 0, hash, 0, 16)
66             Array.Copy(temp, 0, hash, 15, 16)
67             AES.Key = hash
68             AES.Mode = CipherMode.ECB
69             Dim DESDecrypter As ICryptoTransform = AES.CreateDecryptor
70             Dim Buffer As Byte() = input
71             Buffer = DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)
72             Return Buffer
73         End Function
74
75         ''' <summary>
76         ''' 
77         ''' </summary>
78         ''' <param name="text">密文数据的base64字符串</param>
79         ''' <returns></returns>
80         <MethodImpl(MethodImplOptions.AggressiveInlining)>
81         Public Overrides Function DecryptString(text As StringAs String
82             Return textEncoding.GetString(Decrypt(Convert.FromBase64String(text)))
83         End Function
84
85         Public Overrides Function Encrypt(input() As ByteAs Byte()
86             Dim AES As New RijndaelManaged
87             Dim hashAES As New MD5CryptoServiceProvider
88             Dim hash(31) As Byte
89             Dim temp As Byte() = hashAES.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strPassphrase))
90             Array.Copy(temp, 0, hash, 0, 16)
91             Array.Copy(temp, 0, hash, 15, 16)
92             AES.Key = hash
93             AES.Mode = CipherMode.ECB
94             Dim DESEncrypter As ICryptoTransform = AES.CreateEncryptor
95             Dim Buffer As Byte() = input
96             Buffer = DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)
97             Return Buffer
98         End Function
99
100         ''' <summary>
101         ''' 加密明文,然后使用base64字符串输出结果
102         ''' </summary>
103         ''' <param name="text"></param>
104         ''' <returns></returns>
105         ''' 
106         <MethodImpl(MethodImplOptions.AggressiveInlining)>
107         Public Overrides Function EncryptData(text As StringAs String
108             Return Convert.ToBase64String(Encrypt(textEncoding.GetBytes(text)))
109         End Function
110     End Class
111 End Namespace