|
I found working code (Rijndael Algorithm => Advanced Encryption Standard) which could interest you.
Let's start with the class => Crypto.vb
Imports System.IO Imports System.Text Imports System.ComponentModel Imports System.Security.Cryptography
Public Class Crypto
Public Function EncryptString(ByVal InputString As String, ByVal PassPhrase As String, Optional ByVal IVString As String = vbNullString) As String
Dim CryptProvider As New RijndaelManaged() Dim hashMD5 As New MD5CryptoServiceProvider() Dim InputbyteArray() As Byte = Encoding.UTF8.GetBytes(InputString)
CryptProvider.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(PassPhrase) ) CryptProvider.IV = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(IVString)) CryptProvider.Mode = CipherMode.CBC
Dim ms As MemoryStream = New MemoryStream() Dim cs As CryptoStream = New CryptoStream(ms, _ CryptProvider.CreateEncryptor(), CryptoStreamMode.Write) cs.Write(InputbyteArray, 0, InputbyteArray.Length) cs.FlushFinalBlock()
Dim ret As StringBuilder = New StringBuilder() Dim b() As Byte = ms.ToArray Dim I As Integer Dim Out As String For I = 0 To UBound(b) 'Format as hex ret.AppendFormat("{0:X2}", b(I)) Next Return ret.ToString()
End Function
Public Function DecryptString(ByVal InputString As String, ByVal PassPhrase As String, ByVal IVString As String) As String
Dim DES As New RijndaelManaged()
Dim hashMD5 As New MD5CryptoServiceProvider()
DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(PassPhrase) ) DES.IV = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(IVString)) DES.Mode = CipherMode.CBC 'Put the input string into the byte array Dim inputByteArray(InputString.Length / 2 - 1) As Byte Dim X As Integer For X = 0 To InputString.Length / 2 - 1 Dim IJ As Integer = (Convert.ToInt32(InputString.Substring(X * 2, 2), 16)) Dim BT As New ByteConverter() inputByteArray(X) = New Byte() inputByteArray(X) = BT.ConvertTo(IJ, GetType(Byte)) Next
'Create the crypto objects Dim MS As MemoryStream = New MemoryStream() Dim CS As CryptoStream = New CryptoStream(MS, DES.CreateDecryptor(), CryptoStreamMode.Write)
'Flush the data through the crypto stream into the memory stream CS.Write(inputByteArray, 0, inputByteArray.Length) CS.FlushFinalBlock()
'Get the decrypted data back from the memory stream Dim ret As StringBuilder = New StringBuilder() Dim B() As Byte = MS.ToArray Dim I As Integer For I = 0 To UBound(B) ret.Append(Chr(B(I))) Next Return ret.ToString()
End Function
End Class
Then create an other page whatever.aspx, create an instance of our Crypto class and use the 2 functions of that Crypto class for encrypt and decrypt your string:
Sub Page_Load(.................
Dim Crypt As New Crypto() Dim TestString, PassPhrase, IVPhrase, CryptString, DecryptString As String TestString = "Testing" PassPhrase = "This is a passphrase" IVPhrase = "This is an IV phrase"
Response.Write("Original String: " & TestString & "<br />")
CryptString = Crypt.EncryptString(TestString, PassPhrase, IVPhrase) Response.Write("EnCrypt String: " & CryptString & "<br />")
DecryptString = Crypt.DecryptString(CryptString, PassPhrase, IVPhrase) Response.Write("DeCrypt String: " & DecryptString & "<br />")
End Sub
------------- Martial Boissonneault
http://getElementById.com/
|