RE: convert string to CipherMessage

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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-us/cpguide/html/cpconencryptingdecryptingdata.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.
> >
.



Relevant Pages

  • Re: Internal String Visibility
    ... Public Function EasyEncrypt(Plaintext As String) As String ... ' Painfully simple encryption for comments etc. ... Dim n As Integer ...
    (microsoft.public.vb.general.discussion)
  • Tripple DES Encrypt/Decrypt string Function or Example
    ... Has anyone made a simple tripple des encryption function? ... generate an encrypted output. ... IE. Public Function EncryptMe (StringtoEncrypt, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Byte array to string and back - newbie question
    ... // Create a symmetric algorithm. ... This is done to make encryption more ... // Encrypt a string into a string using a password ... // Decrypt a byte array into a byte array using a key and an IV ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Using Python To Create An Encrypted Container
    ... an encrypted archive utility designed for secure archiving ... A match string allows you to only extract files matching a given ... Encrypt the string s using passwd and encryption cipher enc ...
    (comp.lang.python)
  • Re: How good an encryption algorithm is this?
    ... As long as the string can be converted to/from a byte stream, ... then you can apply that after the encryption. ... > So I decided to invent my own algorithm, and I just wanted anybody's> opinion on how secure this could be compared to the Win32 API version. ... > HCRYPTHASH hCryptHash; ...
    (microsoft.public.vc.language)