Imports System.Security.Cryptography

Public Class MyEncrypt

    Dim EncryptionKey As String = "password scelta da te"

    Public Function EncryptString128Bit(ByVal vstrTextToBeEncrypted As String) As String
        Dim bytValue() As Byte
        Dim bytKey() As Byte
        Dim bytEncoded() As Byte = {}
        Dim bytIV() As Byte = {121, 241, 10, 1, 132, 74, 11, 39, 255, 91, 45, 78, 14, 211, 22, 62}
        Dim intLength As Integer
        Dim intRemaining As Integer
        Dim objMemoryStream As New MemoryStream()
        Dim objCryptoStream As CryptoStream
        Dim objRijndaelManaged As RijndaelManaged
        vstrTextToBeEncrypted = StripNullCharacters(vstrTextToBeEncrypted)
        bytValue = Encoding.ASCII.GetBytes(vstrTextToBeEncrypted.ToCharArray)
        intLength = Len(EncryptionKey)
        If intLength >= 32 Then
            EncryptionKey = Strings.Left(EncryptionKey, 32)
        Else
            intLength = Len(EncryptionKey)
            intRemaining = 32 - intLength
            EncryptionKey = EncryptionKey & Strings.StrDup(intRemaining, "X")
        End If
        bytKey = Encoding.ASCII.GetBytes(EncryptionKey.ToCharArray)
        objRijndaelManaged = New RijndaelManaged()
        Try
            objCryptoStream = New CryptoStream(objMemoryStream, _
              objRijndaelManaged.CreateEncryptor(bytKey, bytIV), _
              CryptoStreamMode.Write)
            objCryptoStream.Write(bytValue, 0, bytValue.Length)
            objCryptoStream.FlushFinalBlock()
            bytEncoded = objMemoryStream.ToArray
            objMemoryStream.Close()
            objCryptoStream.Close()
        Catch
        End Try
        Return Convert.ToBase64String(bytEncoded)
    End Function

    Public Function DecryptString128Bit(ByVal vstrStringToBeDecrypted As String) As String
        Dim bytDataToBeDecrypted() As Byte
        Dim bytTemp() As Byte
        Dim bytIV() As Byte = {121, 241, 10, 1, 132, 74, 11, 39, 255, 91, 45, 78, 14, 211, 22, 62}
        Dim objRijndaelManaged As New RijndaelManaged()
        Dim objMemoryStream As MemoryStream
        Dim objCryptoStream As CryptoStream
        Dim bytDecryptionKey() As Byte
        Dim intLength As Integer
        Dim intRemaining As Integer
        Dim strReturnString As String = String.Empty
        bytDataToBeDecrypted = Convert.FromBase64String(vstrStringToBeDecrypted)
        intLength = Len(EncryptionKey)
        If intLength >= 32 Then
            EncryptionKey = Strings.Left(EncryptionKey, 32)
        Else
            intLength = Len(EncryptionKey)
            intRemaining = 32 - intLength
            EncryptionKey = EncryptionKey & Strings.StrDup(intRemaining, "X")
        End If
        bytDecryptionKey = Encoding.ASCII.GetBytes(EncryptionKey.ToCharArray)
        ReDim bytTemp(bytDataToBeDecrypted.Length)
        objMemoryStream = New MemoryStream(bytDataToBeDecrypted)
        Try
            objCryptoStream = New CryptoStream(objMemoryStream, _
               objRijndaelManaged.CreateDecryptor(bytDecryptionKey, bytIV), _
               CryptoStreamMode.Read)
            objCryptoStream.Read(bytTemp, 0, bytTemp.Length)
            objCryptoStream.FlushFinalBlock()
            objMemoryStream.Close()
            objCryptoStream.Close()
        Catch
        End Try
        Return StripNullCharacters(Encoding.ASCII.GetString(bytTemp))
    End Function

    Public Function StripNullCharacters(ByVal vstrStringWithNulls As String) As String
        Dim intPosition As Integer
        Dim strStringWithOutNulls As String
        intPosition = 1
        strStringWithOutNulls = vstrStringWithNulls
        Do While intPosition > 0
            intPosition = InStr(intPosition, vstrStringWithNulls, vbNullChar)
            If intPosition > 0 Then
                strStringWithOutNulls = Left$(strStringWithOutNulls, intPosition - 1) & _
                                  Right$(strStringWithOutNulls, Len(strStringWithOutNulls) - intPosition)
            End If
            If intPosition > strStringWithOutNulls.Length Then
                Exit Do
            End If
        Loop
        Return strStringWithOutNulls
    End Function

End Class