Private rsa As RSACryptoServic eProvider
Private rc2 As RC2CryptoServic eProvider
Public Function EncryptMessage( ByVal [text] As String) As CipherMessage
' Convert string to a byte array
Dim message As New CipherMessage
Dim plainBytes As Byte() =
Encoding.Unicod e.GetBytes([text].ToCharArray())
' A new key and iv are generated for every message
Dim bEncryptedValue As Object
Dim bIV As Object
Dim oPassword As Password = New Password
Dim m_bDESKEY As Byte() = ASCIIEncoding.A SCII.GetBytes(" ys6s5s4s")
Dim m_bDESIV As Byte() = ASCIIEncoding.A SCII.GetBytes(" fu27shw6")
Dim sPass As String
rc2.Key = m_bDESKEY
rc2.IV = m_bDESIV
'rc2.GenerateKe y()
'rc2.GenerateIV ()
' The rc2 initialization doesnt need to be encrypted, but will
' be used in conjunction with the key to decrypt the message.
message.rc2IV = rc2.IV
Try
' Encrypt the RC2 key using RSA encryption
message.rc2Key = rsa.Encrypt(rc2 .Key, False)
Catch e As CryptographicEx ception
' The High Encryption Pack is required to run this sample
' because we are using a 128-bit key. See the readme for
' additional information.
Console.WriteLi ne(("Encryption Failed. Ensure that the" + " High
Encryption Pack is installed."))
Console.WriteLi ne(("Error Message: " + e.Message))
Environment.Exi t(0)
End Try
' Encrypt the Text Message using RC2 (Symmetric algorithm)
Dim sse As ICryptoTransfor m = rc2.CreateEncry ptor()
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms , sse, CryptoStreamMod e.Write)
Try
cs.Write(plainB ytes, 0, plainBytes.Leng th)
cs.FlushFinalBl ock()
message.cipherB ytes = ms.ToArray()
Catch e As Exception
Console.WriteLi ne(e.Message)
Finally
ms.Close()
cs.Close()
End Try
Return message
End Function 'EncryptMessage
Public Function DecryptMessage( ByVal message As CipherMessage)
Dim sMessage As String
' Get the RC2 Key and Initialization Vector
rc2.IV = message.rc2IV
Try
' Try decrypting the rc2 key
rc2.Key = rsa.Decrypt(mes sage.rc2Key, False)
Catch e As CryptographicEx ception
sMessage = "Decryption Failed: " + e.Message
Return sMessage
End Try
Dim ssd As ICryptoTransfor m = rc2.CreateDecry ptor()
' Put the encrypted message in a memorystream
Dim ms As New MemoryStream(me ssage.cipherByt es)
' the CryptoStream will read cipher text from the MemoryStream
Dim cs As New CryptoStream(ms , ssd, CryptoStreamMod e.Read)
Dim initialText() As Byte = New [Byte](message.cipher Bytes.Length) {}
Try
' Decrypt the message and store in byte array
cs.Read(initial Text, 0, initialText.Len gth)
Catch e As Exception
Console.WriteLi ne(e.Message)
Finally
ms.Close()
cs.Close()
End Try
' Display the message received
sMessage = name + " received the following message:"
sMessage += " " & Encoding.Unicod e.GetString(ini tialText)
Return sMessage
End Function 'DecryptMessage
"Rakesh Rajan" wrote:
Hi,
What is the function you are using? Which encryption class is it using?
MSDN details encryption and decryption of strings here:
http://msdn.microsoft.com/library/en...yptingdata.asp
--
HTH,
Rakesh Rajan
MVP, MCSD
http://www.msmvps.com/rakeshrajan/
"Nathan" wrote:
Is there a way to convert a string to a CipherMessage? I am calling a
function that decrypts a CipherMessage and returns the value. The only
problem is when I want to use an encrypted value stored in a querystring, I
can't figure out how to convert it back to a CipherMessage.