wdacrypto.vb

来自「Beginning VB.NET DatabasesAll_Code.rar」· VB 代码 · 共 65 行

VB
65
字号
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography

Public Class WDACrypto
    Implements IDisposable

    Private bytKey() As Byte
    Private bytIV() As Byte

    Public Sub New(ByVal Key() As Byte, ByVal IV() As Byte)
        bytKey = Key
        bytIV = IV
    End Sub

    Public Sub Dispose() Implements System.IDisposable.Dispose

    End Sub

    Public Function Encrypt(ByVal strToEncrypt As String) As String
        Try
            'Initialize a new instance of the TripleDESCryptoServiceProvider class
            Dim objTripleDES As New TripleDESCryptoServiceProvider
            'Convert the input string to a byte array
            Dim bytInput() As Byte = Encoding.UTF8.GetBytes(strToEncrypt)
            'Initialize a new instance of the MemoryStream class
            Dim objOutputStream As New MemoryStream
            'Encrypt the byte array
            Dim objCryptoStream As New CryptoStream(objOutputStream, _
                objTripleDES.CreateEncryptor(bytKey, bytIV), _
                CryptoStreamMode.Write)
            objCryptoStream.Write(bytInput, 0, bytInput.Length)
            objCryptoStream.FlushFinalBlock()
            'Return the byte array as a Base64 string
            Return Convert.ToBase64String(objOutputStream.ToArray())
        Catch ExceptionErr As Exception
            Throw New System.Exception(ExceptionErr.Message, _
                ExceptionErr.InnerException)
        End Try
    End Function

    Public Function Decrypt(ByVal strToDecrypt As String) As String
        Try
            'Initialize a new instance of the TripleDESCryptoServiceProvider class
            Dim objTripleDES As New TripleDESCryptoServiceProvider
            'Convert the input string to a byte array
            Dim inputByteArray() As Byte = Convert.FromBase64String(strToDecrypt)
            'Initialize a new instance of the MemoryStream class
            Dim objOutputStream As New MemoryStream
            'Decrypt the byte array
            Dim objCryptoStream As New CryptoStream(objOutputStream, _
                objTripleDES.CreateDecryptor(bytKey, bytIV), _
                CryptoStreamMode.Write)
            objCryptoStream.Write(inputByteArray, 0, inputByteArray.Length)
            objCryptoStream.FlushFinalBlock()
            'Return the byte array as a string
            Return Encoding.UTF8.GetString(objOutputStream.ToArray())
        Catch ExceptionErr As Exception
            Throw New System.Exception(ExceptionErr.Message, _
                ExceptionErr.InnerException)
        End Try
    End Function

End Class

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?