By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,364 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,364 IT Pros & Developers. It's quick & easy.

convert string to CipherMessage

P: n/a
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.

Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

Jul 21 '05 #2

P: n/a
Private rsa As RSACryptoServiceProvider
Private rc2 As RC2CryptoServiceProvider

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.Unicode.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.ASCII.GetBytes("ys6s5s4s")
Dim m_bDESIV As Byte() = ASCIIEncoding.ASCII.GetBytes("fu27shw6")
Dim sPass As String

rc2.Key = m_bDESKEY
rc2.IV = m_bDESIV
'rc2.GenerateKey()
'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 CryptographicException
' 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.WriteLine(("Encryption Failed. Ensure that the" + " High
Encryption Pack is installed."))
Console.WriteLine(("Error Message: " + e.Message))
Environment.Exit(0)
End Try
' Encrypt the Text Message using RC2 (Symmetric algorithm)
Dim sse As ICryptoTransform = rc2.CreateEncryptor()
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, sse, CryptoStreamMode.Write)
Try
cs.Write(plainBytes, 0, plainBytes.Length)
cs.FlushFinalBlock()
message.cipherBytes = ms.ToArray()
Catch e As Exception
Console.WriteLine(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(message.rc2Key, False)
Catch e As CryptographicException
sMessage = "Decryption Failed: " + e.Message
Return sMessage
End Try

Dim ssd As ICryptoTransform = rc2.CreateDecryptor()
' Put the encrypted message in a memorystream
Dim ms As New MemoryStream(message.cipherBytes)
' the CryptoStream will read cipher text from the MemoryStream
Dim cs As New CryptoStream(ms, ssd, CryptoStreamMode.Read)
Dim initialText() As Byte = New [Byte](message.cipherBytes.Length) {}

Try
' Decrypt the message and store in byte array
cs.Read(initialText, 0, initialText.Length)
Catch e As Exception
Console.WriteLine(e.Message)
Finally
ms.Close()
cs.Close()
End Try

' Display the message received
sMessage = name + " received the following message:"
sMessage += " " & Encoding.Unicode.GetString(initialText)
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.

Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.