Code:
Public Class SimpleRijndael
' Notes about the function:
' 1. the initVector parameter MUST BE 16 BYTES. No more, no less.
' 2. keySize can ONLY take the values 128, 192 or 256. Anything else will break.
' 3. salt should be 8 characters or longer, but the function will allocate more characters for you if needs be.
Public Shared Function Encrypt(ByVal data As Byte(), ByVal password As String, ByVal salt As String, ByVal initVector As Byte(), Optional ByVal keySize As Integer = 128) As Byte()
salt = salt.PadRight(8, "+"c)
Dim bSalt As Byte() = Encoding.ASCII.GetBytes(salt)
Dim derivedPassword As New Rfc2898DeriveBytes(password, bSalt, 2)
Dim bKey As Byte() = derivedPassword.GetBytes(keySize / 8)
Using rijndael = New RijndaelManaged() With {.Mode = CipherMode.CBC}
Using encryptor = rijndael.CreateEncryptor(bKey, initVector)
Using bufferStream = New MemoryStream()
Using cryptoStream = New CryptoStream(bufferStream, encryptor, CryptoStreamMode.Write)
cryptoStream.Write(data, 0, data.Length)
cryptoStream.FlushFinalBlock()
Return bufferStream.ToArray()
End Using
End Using
End Using
End Using
End Function
' Notes about the function
' Same as Encrypt. Vector must be exactly 16 bytes, salt.Length should be >= 8 and keysize should be 128, 192 or 256.
Public Shared Function Decrypt(ByVal data As Byte(), ByVal password As String, ByVal salt As String, ByVal initVector As Byte(), Optional ByVal keySize As Integer = 128) As Byte()
salt = salt.PadRight(8, "+"c)
Dim bSalt As Byte() = Encoding.ASCII.GetBytes(salt)
Dim derivedPassword As New Rfc2898DeriveBytes(password, bSalt, 2)
Dim bKey As Byte() = derivedPassword.GetBytes(keySize / 8)
Using rijndael = New RijndaelManaged() With {.Mode = CipherMode.CBC}
Using decryptor = rijndael.CreateDecryptor(bKey, initVector)
Using bufferStream = New MemoryStream(data)
Using decryptoStream = New CryptoStream(bufferStream, decryptor, CryptoStreamMode.Read)
Dim buffer(data.Length - 1) As Byte
Dim read As Integer = decryptoStream.Read(buffer, 0, buffer.Length)
Dim decrypted(read - 1) As Byte
Array.Copy(buffer, decrypted, read)
Return decrypted
End Using
End Using
End Using
End Using
End Function
' A few functional versions for ease-of-use encrypting string data
Public Shared Function EncryptString(ByVal data As String, ByVal password As String, ByVal salt As String, ByVal initVector As Byte(), Optional ByVal keySize As Integer = 128, Optional ByVal stringEncoding As Encoding = Nothing) As Byte()
If stringEncoding Is Nothing Then stringEncoding = Encoding.ASCII
Return Encrypt(stringEncoding.GetBytes(data), password, salt, initVector, keySize)
End Function
Public Shared Function DecryptString(ByVal data As Byte(), ByVal password As String, ByVal salt As String, ByVal initVector As Byte(), Optional ByVal keySize As Integer = 128, Optional ByVal stringEncoding As Encoding = Nothing) As String
If stringEncoding Is Nothing Then stringEncoding = Encoding.ASCII
Return stringEncoding.GetString(Decrypt(data, password, salt, initVector, keySize))
End Function
End Class
Now to use it you need a few bits of information:
Code:
Dim password As String = "password123" 'password
Dim salt As String = "salty" 'extra protection for the password
Dim initVector As Byte() = Encoding.ASCII.GetBytes(New String("*"c, 16)) '16-byte init vector.
Dim phrase As String = "this is some piece of text, it is quite nice" 'data to be encrypted.
Dim encrypted As Byte() = SimpleRijndael.EncryptString(phrase, password, salt, initVector) 'encrypted data.
Dim decrypted As String = SimpleRijndael.DecryptString(encrypted, password, salt, initVector) 'decrypted data.
MessageBox.Show(decrypted)