AES encryption/decryption

假如想象 提交于 2019-12-04 18:08:42

Just copy everything in a new function starting from Dim ByteCount As Integer = 0 to SymmetricKey.Clear() to get rid of all strings? After that you only need to define the arguments to the function.

PapaGeorgo

I am using this (found on Google) for strings AES Encryption/Decryption:

Imports System.Security.Cryptography

Namespace TextCrypters

    Public Class AESCrypter

        Public Shared pass As String = "password"

        Public Shared Function AES_Encrypt(ByVal input As String) As String
            Dim AES As New System.Security.Cryptography.RijndaelManaged
            Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
            Dim encrypted As String = ""
            Try
                Dim hash(31) As Byte
                Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
                Array.Copy(temp, 0, hash, 0, 16)
                Array.Copy(temp, 0, hash, 15, 16)
                AES.Key = hash
                AES.Mode = CipherMode.ECB
                Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
                Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
                encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
                Return encrypted
            Catch ex As Exception
                Return Nothing
            End Try

        End Function

        Public Shared Function AES_Decrypt(ByVal input As String) As String
            Dim AES As New System.Security.Cryptography.RijndaelManaged
            Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
            Dim decrypted As String = ""
            Try
                Dim hash(31) As Byte
                Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
                Array.Copy(temp, 0, hash, 0, 16)
                Array.Copy(temp, 0, hash, 15, 16)
                AES.Key = hash
                AES.Mode = CipherMode.ECB
                Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
                Dim Buffer As Byte() = Convert.FromBase64String(input)
                decrypted = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
                Return decrypted
            Catch ex As Exception
                Return Nothing
            End Try

        End Function

    End Class

End Namespace

To use it just do this:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox2.Text = AESCrypter.AES_Encrypt(TextBox1.Text)
End Sub

Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    TextBox4.Text = AESCrypter.AES_Decrypt(TextBox3.Text)
End Sub

The simplest way is to use a wrapper function that just converts the byte array to a string, encrypts it with your AESEncrypt function, and converts the string back to a byte array. You can find conversion functions for VB.net here.

Edited to add: I think I got this wrong. It seems that a VB String is in Unicode format, and these translation functions convert it to/from a UTF8 byte array. Which is not what was required...

Public Function AESEncrypt(ByVal PlainBytes As Byte, ByVal Password As String, ByVal salt As String)
Dim HashAlgorithm As String = "SHA1" 'Can be SHA1 or MD5
Dim PasswordIterations As String = 2
Dim InitialVector As String = "CanEncryption123" 'This should be a string of 16 ASCII characters.
Dim KeySize As Integer = 256 'Can be 128, 192, or 256.

Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector)
Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(salt)
Dim DerivedPassword As PasswordDeriveBytes = New PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations)
Dim KeyBytes As Byte() = DerivedPassword.GetBytes(KeySize / 8)
Dim SymmetricKey As RijndaelManaged = New RijndaelManaged()
SymmetricKey.Mode = CipherMode.CBC

Dim CipherTextBytes As Byte() = Nothing
Using Encryptor As ICryptoTransform = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes)
    Using MemStream As New MemoryStream()
        Using CryptoStream As New CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write)
            CryptoStream.Write(PlainBytes, 0, PlainBytes.Length)
            CryptoStream.FlushFinalBlock()
            CipherTextBytes = MemStream.ToArray()
            MemStream.Close()
            CryptoStream.Close()
        End Using
    End Using
End Using
SymmetricKey.Clear()
Return Convert.ToBase64String(CipherTextBytes)
End Function
Public Function AESDecrypt(ByVal CipherBytes As Byte, ByVal password As String, ByVal salt As String) As String
Dim HashAlgorithm As String = "SHA1"
Dim PasswordIterations As String = 2
Dim InitialVector As String = "CanEncryption123"
Dim KeySize As Integer = 256

If (String.IsNullOrEmpty(CipherText)) Then
    Return ""
End If
Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector)
Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(salt)
Dim DerivedPassword As PasswordDeriveBytes = New PasswordDeriveBytes(password, SaltValueBytes, HashAlgorithm, PasswordIterations)
Dim KeyBytes As Byte() = DerivedPassword.GetBytes(KeySize / 8)
Dim SymmetricKey As RijndaelManaged = New RijndaelManaged()
SymmetricKey.Mode = CipherMode.CBC
Dim PlainTextBytes As Byte() = New Byte(CipherBytes.Length - 1) {}

Dim ByteCount As Integer = 0

Using Decryptor As ICryptoTransform = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes)
    Using MemStream As MemoryStream = New MemoryStream(CipherBytes)
        Using CryptoStream As CryptoStream = New CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read)
            ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length)
            MemStream.Close()
            CryptoStream.Close()
        End Using
    End Using
End Using
SymmetricKey.Clear()
Return Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount)
End Function

The code you are using actually converts the input string to byte array. This code accepts byte array.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!