473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 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 2711
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Eric Lilja | last post by:
Hello, I've made a templated class Option (a child of the abstract base class OptionBase) that stores an option name (in the form someoption=) and the value belonging to that option. The value is...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
7
by: patang | last post by:
I want to convert amount to words. Is there any funciton available? Example: $230.30 Two Hundred Thirty Dollars and 30/100
6
by: patang | last post by:
Could someone please tell me where am I supposed to put this code. Actually my project has two forms. I created a new module and have put the following code sent by someone. All the function...
2
by: Nathan | last post by:
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...
4
by: tshad | last post by:
I am trying to convert a string character to an int where the string is all numbers. I tried: int test; string stemp = "5"; test = Convert.ToInt32(stemp); But test is equal to 53.
9
by: Marco Nef | last post by:
Hi there I'm looking for a template class that converts the template argument to a string, so something like the following should work: Convert<float>::Get() == "float"; Convert<3>::Get() ==...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.