clsenskripsi.vb

来自「This is a Text Editor Created USing VB.n」· VB 代码 · 共 93 行

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

Public Class clsEnskripsi

    Enum MetodeEnskripsi
        DES
        RC2
        Rijndael
    End Enum

    Private Function GetKeyByteArray(ByVal password As String) As Byte()
        Dim tmpbyte(7) As Byte
        password = password.PadRight(8)
        For i As Integer = 0 To 7
            tmpbyte(i) = Asc(Mid$(password, i + 1, 1))
        Next
        Return tmpbyte
    End Function

    Public Function Enskrip(ByVal plainText As String, ByVal password As String, Optional ByVal Metode As MetodeEnskripsi = MetodeEnskripsi.DES) As String
        On Error Resume Next
        Dim bytekey() As Byte = GetKeyByteArray(password)
        Dim byteIV() As Byte = GetKeyByteArray(StrReverse(password))
        Dim ToEncrypt As Byte() = Encoding.UTF8.GetBytes(plainText)
        Dim Encryptor As ICryptoTransform
        Select Case Metode
            Case MetodeEnskripsi.DES
                Dim Provider As New DESCryptoServiceProvider
                Encryptor = Provider.CreateEncryptor(bytekey, byteIV)
            Case MetodeEnskripsi.RC2
                Dim Provider As New RC2CryptoServiceProvider
                Encryptor = Provider.CreateEncryptor(bytekey, byteIV)
            Case Else
                byteIV = Encoding.ASCII.GetBytes("1234567890123456")
                Dim bSaltValue As Byte() = Encoding.ASCII.GetBytes("12")
                Dim pdbPassword As New PasswordDeriveBytes(password, bSaltValue, "SHA1", 2)
                bytekey = pdbPassword.GetBytes(256 / 8)
                Dim Provider As New RijndaelManaged()
                Encryptor = Provider.CreateEncryptor(bytekey, byteIV)
        End Select
        Dim msEncrypt As New MemoryStream()
        Dim csEncrypt As New CryptoStream(msEncrypt, Encryptor, CryptoStreamMode.Write)

        csEncrypt.Write(ToEncrypt, 0, ToEncrypt.Length)
        csEncrypt.FlushFinalBlock()

        Dim encrypted As Byte() = msEncrypt.ToArray()

        msEncrypt.Close()
        csEncrypt.Close()

        Return Convert.ToBase64String(encrypted)
    End Function

    Public Function Deskrip(ByVal plainText As String, ByVal password As String, Optional ByVal metode As MetodeEnskripsi = MetodeEnskripsi.DES) As String
        On Error Resume Next
        Dim bytekey() As Byte = GetKeyByteArray(password)
        Dim byteIV() As Byte = GetKeyByteArray(StrReverse(password))
        Dim encrypted As Byte() = Convert.FromBase64String(plainText)
        Dim Encryptor As ICryptoTransform

        Select Case metode
            Case MetodeEnskripsi.DES
                Dim Provider As New DESCryptoServiceProvider
                Encryptor = Provider.CreateDecryptor(bytekey, byteIV)
            Case MetodeEnskripsi.RC2
                Dim Provider As New RC2CryptoServiceProvider
                Encryptor = Provider.CreateDecryptor(bytekey, byteIV)
            Case Else
                byteIV = Encoding.ASCII.GetBytes("1234567890123456")
                Dim bSaltValue As Byte() = Encoding.ASCII.GetBytes("12")
                Dim pdbPassword As New PasswordDeriveBytes(password, bSaltValue, "SHA1", 2)
                bytekey = pdbPassword.GetBytes(256 / 8)
                Dim Provider As New RijndaelManaged
                Encryptor = Provider.CreateDecryptor(bytekey, byteIV)
        End Select

        Dim msEncrypt As New MemoryStream(encrypted)
        Dim csEncrypt As New CryptoStream(msEncrypt, Encryptor, CryptoStreamMode.Read)

        Dim ToDescrypt(encrypted.Length) As Byte
        Dim intDecryptedByte As Integer = csEncrypt.Read(ToDescrypt, 0, ToDescrypt.Length)

        msEncrypt.Close()
        csEncrypt.Close()

        Return Encoding.UTF8.GetString(ToDescrypt, 0, intDecryptedByte)
    End Function

End Class

⌨️ 快捷键说明

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