2018-10-06 00:02:05 +08:00
|
|
|
|
#Region "Microsoft.VisualBasic::e5cfa890b656e93e3a8988f76e1e286f, Microsoft.VisualBasic.Core\Extensions\Security\AES.vb"
|
|
|
|
|
|
|
|
|
|
|
|
' Author:
|
|
|
|
|
|
'
|
|
|
|
|
|
' asuka (amethyst.asuka@gcmodeller.org)
|
|
|
|
|
|
' xie (genetics@smrucc.org)
|
|
|
|
|
|
' xieguigang (xie.guigang@live.com)
|
|
|
|
|
|
'
|
|
|
|
|
|
' Copyright (c) 2018 GPL3 Licensed
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
' GNU GENERAL PUBLIC LICENSE (GPL3)
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
' This program is free software: you can redistribute it and/or modify
|
|
|
|
|
|
' it under the terms of the GNU General Public License as published by
|
|
|
|
|
|
' the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
|
' (at your option) any later version.
|
|
|
|
|
|
'
|
|
|
|
|
|
' This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
' but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
' GNU General Public License for more details.
|
|
|
|
|
|
'
|
|
|
|
|
|
' You should have received a copy of the GNU General Public License
|
|
|
|
|
|
' along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
' /********************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
' Summaries:
|
|
|
|
|
|
|
|
|
|
|
|
' Class AES
|
|
|
|
|
|
'
|
|
|
|
|
|
' Constructor: (+1 Overloads) Sub New
|
|
|
|
|
|
' Function: Decrypt, DecryptString, Encrypt, EncryptData
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
' /********************************************************************************/
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
|
|
|
|
|
#End Region
|
|
|
|
|
|
|
2018-10-03 13:28:23 +08:00
|
|
|
|
Imports System.Runtime.CompilerServices
|
|
|
|
|
|
Imports System.Security.Cryptography
|
|
|
|
|
|
Imports System.Text
|
|
|
|
|
|
Imports Microsoft.VisualBasic.Text
|
|
|
|
|
|
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Namespace SecurityString
|
|
|
|
|
|
|
|
|
|
|
|
Public Class AES : Inherits SecurityStringModel
|
|
|
|
|
|
|
2018-10-03 13:28:23 +08:00
|
|
|
|
ReadOnly textEncoding As Encoding
|
|
|
|
|
|
|
|
|
|
|
|
Sub New(pass$, Optional encoding As Encodings = Encodings.UTF8WithoutBOM)
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Me.strPassphrase = pass
|
2018-10-03 13:28:23 +08:00
|
|
|
|
Me.textEncoding = encoding.CodePage
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End Sub
|
|
|
|
|
|
|
|
|
|
|
|
Public Overrides Function Decrypt(input() As Byte) As Byte()
|
2018-10-03 13:28:23 +08:00
|
|
|
|
Dim AES As New RijndaelManaged
|
|
|
|
|
|
Dim hashAES As New MD5CryptoServiceProvider
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Dim hash(31) As Byte
|
2018-10-03 13:28:23 +08:00
|
|
|
|
Dim temp As Byte() = hashAES.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strPassphrase))
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Array.Copy(temp, 0, hash, 0, 16)
|
|
|
|
|
|
Array.Copy(temp, 0, hash, 15, 16)
|
|
|
|
|
|
AES.Key = hash
|
2018-10-03 13:28:23 +08:00
|
|
|
|
AES.Mode = CipherMode.ECB
|
|
|
|
|
|
Dim DESDecrypter As ICryptoTransform = AES.CreateDecryptor
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Dim Buffer As Byte() = input
|
|
|
|
|
|
Buffer = DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)
|
|
|
|
|
|
Return Buffer
|
|
|
|
|
|
End Function
|
|
|
|
|
|
|
2018-10-03 13:28:23 +08:00
|
|
|
|
''' <summary>
|
|
|
|
|
|
'''
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <param name="text">密文数据的base64字符串</param>
|
|
|
|
|
|
''' <returns></returns>
|
|
|
|
|
|
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Public Overrides Function DecryptString(text As String) As String
|
2018-10-03 13:28:23 +08:00
|
|
|
|
Return textEncoding.GetString(Decrypt(Convert.FromBase64String(text)))
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
|
|
Public Overrides Function Encrypt(input() As Byte) As Byte()
|
2018-10-03 13:28:23 +08:00
|
|
|
|
Dim AES As New RijndaelManaged
|
|
|
|
|
|
Dim hashAES As New MD5CryptoServiceProvider
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Dim hash(31) As Byte
|
2018-10-03 13:28:23 +08:00
|
|
|
|
Dim temp As Byte() = hashAES.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strPassphrase))
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Array.Copy(temp, 0, hash, 0, 16)
|
|
|
|
|
|
Array.Copy(temp, 0, hash, 15, 16)
|
|
|
|
|
|
AES.Key = hash
|
2018-10-03 13:28:23 +08:00
|
|
|
|
AES.Mode = CipherMode.ECB
|
|
|
|
|
|
Dim DESEncrypter As ICryptoTransform = AES.CreateEncryptor
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Dim Buffer As Byte() = input
|
|
|
|
|
|
Buffer = DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)
|
|
|
|
|
|
Return Buffer
|
|
|
|
|
|
End Function
|
|
|
|
|
|
|
2018-10-03 13:28:23 +08:00
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' 加密明文,然后使用base64字符串输出结果
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <param name="text"></param>
|
|
|
|
|
|
''' <returns></returns>
|
|
|
|
|
|
'''
|
|
|
|
|
|
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Public Overrides Function EncryptData(text As String) As String
|
2018-10-03 13:28:23 +08:00
|
|
|
|
Return Convert.ToBase64String(Encrypt(textEncoding.GetBytes(text)))
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End Function
|
|
|
|
|
|
End Class
|
|
|
|
|
|
End Namespace
|