471,337 Members | 934 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

convert string to CipherMessage

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
2 2536
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
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.

Similar topics

3 posts views Thread by Convert TextBox.Text to Int32 Problem | last post: by
7 posts views Thread by patang | last post: by
6 posts views Thread by patang | last post: by
2 posts views Thread by Nathan | last post: by
4 posts views Thread by tshad | last post: by
9 posts views Thread by Marco Nef | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.