⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wdacrypto.vb

📁 数据库学习的绝好例子简单的数据库经典入门
💻 VB
字号:
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography

Public Class WDACrypto
    Implements IDisposable

    'Private variables and objects
    Private bytKey() As Byte
    Private bytIV() As Byte
    Private bytInput() As Byte

    Private objTripleDES As TripleDESCryptoServiceProvider
    Private objOutputStream As MemoryStream

    Private disposed As Boolean = False    Public Sub New(ByVal Key() As Byte, ByVal IV() As Byte)
        'Initialize the security key and initialization vector
        bytKey = Key
        bytIV = IV

        'Instantiate a new instance of the TripleDESCryptoServiceProvider class
        objTripleDES = New TripleDESCryptoServiceProvider
    End Sub
    ' IDisposable    Private Overloads Sub Dispose(ByVal disposing As Boolean)        If Not Me.disposed Then            If disposing Then                ' TODO: put code to dispose managed resources
            End If            'Clean up
            objTripleDES = Nothing
        End If        Me.disposed = True    End Sub#Region " IDisposable Support "    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Overloads Sub Dispose() Implements IDisposable.Dispose        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)        GC.SuppressFinalize(Me)    End Sub    Protected Overrides Sub Finalize()        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(False)        MyBase.Finalize()    End Sub#End Region
    Public Function Encrypt(ByVal strToEncrypt As String) As String
        Try
            'Convert the input string to a byte array
            Dim bytInput() As Byte = Encoding.UTF8.GetBytes(strToEncrypt)

            'Instantiate a new instance of the MemoryStream class
            Using 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
                Encrypt = Convert.ToBase64String(objOutputStream.ToArray())
            End Using

        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
            'Convert the input string to a byte array
            Dim inputByteArray() As Byte = Convert.FromBase64String(strToDecrypt)

            'Instantiate a new instance of the MemoryStream class
            Using 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
                Decrypt = Encoding.UTF8.GetString(objOutputStream.ToArray())
            End Using

        Catch ExceptionErr As Exception
            Throw New System.Exception(ExceptionErr.Message, _
                ExceptionErr.InnerException)
        End Try
    End Function
End Class

⌨️ 快捷键说明

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