tags:

views:

54

answers:

1
Public Function EncryptString(ByVal vstrStringToBeEnrypted As String) As String

    Dim sa As SymmetricAlgorithm = SymmetricAlgorithm.Create("TripleDES")
    sa.Key = Convert.FromBase64String("cPSQAC05GBXzMhRRz7tm8cqg+vHdHyN5")
    sa.IV = Convert.FromBase64String("jIShBJVBfXo=")

    Dim inputByteArray() As Byte = Encoding.ASCII.GetBytes(TextBox1.Text)


    Dim mS As MemoryStream = New MemoryStream()

    Dim trans As ICryptoTransform = sa.CreateEncryptor
    Dim buf() As Byte = New Byte(2048) {}
    Dim cs As CryptoStream = New CryptoStream(mS, trans, CryptoStreamMode.Write)
    cs.Write(inputByteArray, 0, inputByteArray.Length)
    cs.FlushFinalBlock()

    Return Convert.ToBase64String(mS.ToArray)
End Function
Public Function DecryptString(ByVal vstrStringToBeDerypted As String) As String
    Dim tripleDESKey As SymmetricAlgorithm = SymmetricAlgorithm.Create("TripleDES")
    tripleDESKey.Key = Convert.FromBase64String("cPSQAC05GBXzMhRRz7tm8cqg+vHdHyN5")
    tripleDESKey.IV = Convert.FromBase64String("jIShBJVBfXo=")

    'load our encrypted value into a memory stream
    Dim encryptedValue As String = TextBox1.Text
    Dim encryptedStream As MemoryStream = New MemoryStream()
    encryptedStream.Write(Convert.FromBase64String(encryptedValue), 0, Convert.FromBase64String(encryptedValue).Length)
    encryptedStream.Position = 0

    'set up a stream to do the decryption
    Dim cs As CryptoStream = New CryptoStream(EncryptedStream, tripleDESKey.CreateDecryptor, CryptoStreamMode.Read)
    Dim decryptedStream As MemoryStream = New MemoryStream()
    Dim buf() As Byte = New Byte(2048) {}
    Dim bytesRead As Integer

    'keep reading from encrypted stream via the crypto stream
    'and store that in the decrypted stream
    bytesRead = cs.Read(buf, 0, buf.Length)
    While (bytesRead > 0)
        decryptedStream.Write(buf, 0, bytesRead)
        bytesRead = cs.Read(buf, 0, buf.Length)
    End While

    'reassemble the decrypted stream into a string  
    Dim decryptedValue As String = Encoding.ASCII.GetString(DecryptedStream.ToArray())

    Return decryptedValue.ToString()

End Function
+2  A: 

Need to do something yourself... Though, thanks for pointing out how to do base64 in .NET for me and saving me some Googling

Matthew Scharley