AES Encryption

Class AES Encryption

Public Class AESEncryption

Private Shared ReadOnly Key As Byte() = Encoding.UTF8.GetBytes("3aZ!eR@c7xT1Qp#xY8*v1jH9oL$4mF&2")
Private Shared ReadOnly IV As Byte() = Encoding.UTF8.GetBytes("p!5(9S!kjF03(.y7")

Public Shared Function Encrypt(plainText As String) As String
Using aesAlg As Aes = Aes.Create()
aesAlg.Key = Key
aesAlg.IV = IV

Dim encryptor As ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)

Using msEncrypt As New MemoryStream()
Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
Using swEncrypt As New StreamWriter(csEncrypt)
swEncrypt.Write(plainText)
End Using
End Using
Return Convert.ToBase64String(msEncrypt.ToArray())
End Using
End Using
End Function

Public Shared Function Decrypt(cipherText As String) As String
Using aesAlg As Aes = Aes.Create()
aesAlg.Key = Key
aesAlg.IV = IV

Dim decryptor As ICryptoTransform = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)

Using msDecrypt As New MemoryStream(Convert.FromBase64String(cipherText))
Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
Using srDecrypt As New StreamReader(csDecrypt)
Return srDecrypt.ReadToEnd()
End Using
End Using
End Using
End Using
End Function

End Class

Yorum yapın Yanıtı iptal et