ASP.NET 加解密

摘要:ASP.NET 加解密

crypt.vb
 

Imports System.Security.Cryptography
Imports System.IO
Imports System.Text

Public Class crypt
    Private key() As Byte
    Private iv() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}

    Public Function decrypt(ByVal stringToDecrypt As String, ByVal sEncryptionKey As String) As String
        Dim inputByteArray(stringToDecrypt.Length) As Byte
        Try
            key = System.Text.Encoding.UTF8.GetBytes(Left(sEncryptionKey, 8))
            Dim des As New DESCryptoServiceProvider
            inputByteArray = Convert.FromBase64String(stringToDecrypt)
            Dim ms As New memorystream
            Dim cs As New CryptoStream(ms, des.CreateDecryptor(key, iv), CryptoStreamMode.Write)
            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()
            Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
            Return encoding.GetString(ms.toarray())
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function


    Public Function encrypt(ByVal stringtoencrypt As String, ByVal sencryptionkey As String) As String
        Try
            key = System.Text.Encoding.UTF8.GetBytes(Left(sencryptionkey, 8))
            Dim des As New DESCryptoServiceProvider
            Dim inputbytearray() As Byte = Encoding.utf8.getbytes(stringtoencrypt)
            Dim ms As New MemoryStream
            Dim cs As New CryptoStream(ms, des.CreateEncryptor(key, iv), CryptoStreamMode.Write)
            cs.Write(inputbytearray, 0, inputbytearray.Length)
            cs.FlushFinalBlock()
            Return Convert.ToBase64String(ms.ToArray())
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

  
End Class

 

common.vb
 

"--加密--"

"--解密--"

 

testpage.vb
 

"--PageLoad--"