1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
Imports System.Collections.Generic
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text

Module AESCryptoTest
    Public Class EncryptedString
        ''' <summary>
        ''' Encrpyts the sourceString, returns this result as an Aes encrpyted, BASE64 encoded string
        ''' </summary>
        ''' <param name="plainSourceStringToEncrypt">a plain, Framework string (ASCII, null terminated)</param>
        ''' <param name="passPhrase">The pass phrase.</param>
        ''' <returns>
        ''' returns an Aes encrypted, BASE64 encoded string
        ''' </returns>
        Public Shared Function EncryptString(ByVal plainSourceStringToEncrypt As String, ByVal passPhrase As String) As String
            'Set up the encryption objects
            Using acsp As AesCryptoServiceProvider = GetProvider(Encoding.[Default].GetBytes(passPhrase))
                Dim sourceBytes As Byte() = Encoding.UTF8.GetBytes(plainSourceStringToEncrypt)
                Dim ictE As ICryptoTransform = acsp.CreateEncryptor()

                'Set up stream to contain the encryption
                Dim msS As New MemoryStream()

                'Perform the encrpytion, storing output into the stream
                Dim csS As New CryptoStream(msS, ictE, CryptoStreamMode.Write)
                csS.Write(sourceBytes, 0, sourceBytes.Length)
                csS.FlushFinalBlock()

                'sourceBytes are now encrypted as an array of secure bytes
                Dim encryptedBytes As Byte() = msS.ToArray()
                '.ToArray() is important, don't mess with the buffer
                'return the encrypted bytes as a BASE64 encoded string
                Return Convert.ToBase64String(encryptedBytes)
            End Using
        End Function


        ''' <summary>
        ''' Decrypts a BASE64 encoded string of encrypted data, returns a plain string
        ''' </summary>
        ''' <param name="base64StringToDecrypt">an Aes encrypted AND base64 encoded string</param>
        ''' <param name="passphrase">The passphrase.</param>
        ''' <returns>returns a plain string</returns>
        Public Shared Function DecryptString(ByVal base64StringToDecrypt As String, ByVal passphrase As String) As String
            'Set up the encryption objects
            Using acsp As AesCryptoServiceProvider = GetProvider(Encoding.[Default].GetBytes(passphrase))
                Dim RawBytes As Byte() = Convert.FromBase64String(base64StringToDecrypt)
                Dim ictD As ICryptoTransform = acsp.CreateDecryptor()

                'RawBytes now contains original byte array, still in Encrypted state

                'Decrypt into stream
                Dim msD As New MemoryStream(RawBytes, 0, RawBytes.Length)
                Dim csD As New CryptoStream(msD, ictD, CryptoStreamMode.Read)
                'csD now contains original byte array, fully decrypted

                'return the content of msD as a regular string
                Return (New StreamReader(csD)).ReadToEnd()
            End Using
        End Function

        Private Shared Function GetProvider(ByVal key As Byte()) As AesCryptoServiceProvider
            Dim result As New AesCryptoServiceProvider()
            result.BlockSize = 128
            result.KeySize = 256
            result.Mode = CipherMode.CBC
            result.Padding = PaddingMode.PKCS7

            result.GenerateIV()
            result.IV = New Byte() {0, 0, 0, 0, 0, 0, _
             0, 0, 0, 0, 0, 0, _
             0, 0, 0, 0}

            Dim RealKey As Byte() = GetKey(key, result)
            result.Key = RealKey
            ' result.IV = RealKey;
            Return result
        End Function

        Private Shared Function GetKey(ByVal suggestedKey As Byte(), ByVal p As SymmetricAlgorithm) As Byte()
            Dim kRaw As Byte() = suggestedKey
            Dim kList As New List(Of Byte)()

            For i As Integer = 0 To p.LegalKeySizes(0).MinSize - 1 Step 8
                kList.Add(kRaw((i \ 8) Mod kRaw.Length))
            Next
            Dim k As Byte() = kList.ToArray()
            Return k
        End Function


    End Class
End Module