Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I managed to get the RSA encryption working so now that my license server can encrypt the Rijndael key I�ve been working on getting RijndaelManaged based encryption and decryption working. I�m getting a not supported error when the CryptoStream is declared. Heres the code. Any ideas whats wrong?

 

Shared Rijndael As New RijndaelManaged
Shared RijEncryptor As ICryptoTransform = Rijndael.CreateEncryptor()
Shared RijDecryptor As ICryptoTransform = Rijndael.CreateDecryptor()

Public Shared Sub GenerateRijndaelKey()
Try
     	Rijndael.KeySize = 256
           Rijndael.GenerateKey()
           Rijndael.GenerateIV()

           'debug testing encryption and decryption modes
           DecryptRijndael(EncryptRijndael("Testing"))
     Catch ex As Exception
           Console.WriteLine(Err.ToString())
           MsgBox(Err.ToString, MsgBoxStyle.OKOnly)
     End Try
End Sub

Public Shared Function EncryptRijndael(ByVal TextToEncrypt As String) As String

Dim EncryptedData As String
     Dim EncryptedDataBytes As Byte()
     Dim PlainTextBytes As Byte()

     Try
     	' create the memory stream
           Dim MS As MemoryStream = New MemoryStream

           'convert the text into byte form in preperation to be encrypted
           PlainTextBytes = (New ASCIIEncoding).GetBytes(TextToEncrypt)

           'create the CryptoStream that ties together the MemoryStream and the ICryptoTransform
           Dim CS As CryptoStream = New CryptoStream(MS, RijEncryptor, CryptoStreamMode.Write)

           'write the PlainTextBytes out to the CryptoStream
           CS.Write(PlainTextBytes, 0, PlainTextBytes.Length)

           'close the CryptoStream
           CS.Close()

           'get the encrypted text from the MemoryStream
           'I can't simply just grab the string because the stream doesn't work that way
           EncryptedDataBytes = MS.ToArray()

           'convert the EncryptedDataBytes array into a base64 string
           EncryptedData = System.Convert.ToBase64String(EncryptedDataBytes)

           Return (EncryptedData)

Catch ex As Exception
               Console.WriteLine(Err.ToString())
               MsgBox(Err.ToString, MsgBoxStyle.OKOnly)
     End Try

End Function

Public Shared Function DecryptRijndael(ByVal EncryptedText As String) As String

Dim EncryptedDataBytes As Byte()
     Dim DecryptedDataBytes As Byte()
     Dim DecryptedText As String

     Try
     	'create the memory stream
           Dim MS As MemoryStream = New MemoryStream

           'decode String to byte array of encrypted data
           EncryptedDataBytes = System.Convert.FromBase64String(EncryptedText)

           'create the CryptoStream that ties together the MemoryStream and the ICryptoTransform
           Dim cs As CryptoStream = New CryptoStream(MS, RijDecryptor, CryptoStreamMode.Read)

           'decrypt the bytes into a plain text bytes
           cs.Read(EncryptedDataBytes, 0, EncryptedDataBytes.Length)

           'close the CryptoStream
           cs.Close()

           'get the decrypted bytes from the MemoryStream
           DecryptedDataBytes = MS.ToArray()

           'convert the byte array into a string
           DecryptedText = GetStringFromByteArray(DecryptedDataBytes)

           Return (DecryptedText)

Catch ex As Exception
     	Console.WriteLine(Err.ToString())
           MsgBox(Err.ToString, MsgBoxStyle.OKOnly)
     End Try

End Function

Posted

I adjusted the code to as folows to account for your suggestion. In the watch box though cs is still get a not supported exception. Decryption does get encrypted text to decode but even decoded it comes out a garbled mess.

 

Imports System.Security.Cryptography 
Imports System.Security.Cryptography.SymmetricAlgorithm 
Imports System.Net 
Imports System.Net.Sockets 
Imports System.IO 
Imports System.text

 

Shared TextConverter As New ASCIIEncoding 
'create the symmetrical cryptography object 
Shared Rijndael As New RijndaelManaged 
Shared RijEncryptor As ICryptoTransform 
Shared RijDecryptor As ICryptoTransform 
Shared RijndaelKey As Byte() 
Shared RijndaelIV As Byte()

 

Public Shared Sub GenerateRijndaelKey() 
    'This sub is for the server. The client will recieve a copy of the key
    'after it properly identifies itself 
     Try 
          'generate the key 
          Rijndael.KeySize = 256 
          Rijndael.GenerateKey() 
          RijndaelKey = Rijndael.Key 

          'generate the IV 
          Rijndael.GenerateIV() 
          RijndaelIV = Rijndael.IV 

          'prepare the Encryptor and Decryptor objects 
          RijEncryptor = Rijndael.CreateEncryptor(RijndaelKey, RijndaelIV) 
          RijDecryptor = Rijndael.CreateEncryptor(RijndaelKey, RijndaelIV) 

          'debug testing encryption and decryption modes 
          DecryptRijndael(EncryptRijndael("Testing")) 
     Catch ex As Exception 
          Console.WriteLine(Err.ToString()) 
          MsgBox(Err.ToString, MsgBoxStyle.OKOnly) 
     End Try 
End Sub

 

Public Shared Function EncryptRijndael(ByVal TextToEncrypt As String) As String 

    Dim EncryptedData As String 
    Dim EncryptedDataBytes As Byte() 
    Dim PlainTextBytes As Byte() 

    Try 
         ' create the memory stream 
         Dim MS As MemoryStream = New MemoryStream 

         'convert the text into byte form in preperation to be encrypted 
         PlainTextBytes = TextConverter.GetBytes(TextToEncrypt) 

         'create the CryptoStream that ties together the MemoryStream
         'and the ICryptoTransform 
         Dim CS As CryptoStream = New CryptoStream(MS, RijEncryptor, CryptoStreamMode.Write) 

         'write the PlainTextBytes out to the CryptoStream and flush it 
         CS.Write(PlainTextBytes, 0, PlainTextBytes.Length) 
         CS.FlushFinalBlock() 

         'close the CryptoStream 
         CS.Close() 

         'get the encrypted text from the MemoryStream 
         'I can't simply just grab the string because the stream doesn't
         'work that way 
         EncryptedDataBytes = MS.ToArray() 

         'close the MemoryStream 
         MS.Close() 

         'convert the EncryptedDataBytes array into a base64 string 
         EncryptedData = System.Convert.ToBase64String(EncryptedDataBytes) 

         Return (EncryptedData) 

   Catch ex As Exception 
         Console.WriteLine(Err.ToString()) 
         MsgBox(Err.ToString, MsgBoxStyle.OKOnly) 
   End Try 

End Function

 

Public Shared Function DecryptRijndael(ByVal EncryptedText As String) As String 

    Dim EncryptedDataBytes As Byte() 
    Dim DecryptedDataBytes As Byte() 
    Dim DecryptedText As String 

    Try 
         'convert EncryptedText into a byte array to assign to the MemoryStream 
         EncryptedDataBytes = System.Convert.FromBase64String(EncryptedText) 

         ' create the memory stream 
         Dim MS As MemoryStream = New MemoryStream(EncryptedDataBytes) 

         'decode String to byte array of encrypted data 
         EncryptedDataBytes = System.Convert.FromBase64String(EncryptedText) 

         'create the CryptoStream that ties together the MemoryStream
         'and the ICryptoTransform 
         Dim CS As CryptoStream = New CryptoStream(MS, RijDecryptor, CryptoStreamMode.Read) 

         DecryptedDataBytes = New Byte(EncryptedDataBytes.Length) {} 

         'Read the data out of the crypto stream. 
         CS.Read(DecryptedDataBytes, 0, DecryptedDataBytes.Length) 

         'Convert the byte array back into a string. 
         DecryptedText = GetStringFromByteArray(DecryptedDataBytes) 

         MsgBox(DecryptedText, MsgBoxStyle.OKOnly) 

         Return (DecryptedText) 

   Catch ex As Exception 
         Console.WriteLine(Err.ToString()) 
         MsgBox(Err.ToString, MsgBoxStyle.OKOnly) 
   End Try 

End Function

 

'Helpers 

Private Shared Function GetByteArrayFromString(ByVal Text As String) As Byte() 
    Dim b As Byte() 
    Dim i As Integer 

    ReDim b(Text.Length - 1) 
    For i = 0 To Text.Length - 1 
          b(i) = Asc(Mid(Text, i + 1, 1)) 
    Next i 
    Return(b) 
End Function 

Private Shared Function GetSubArray(ByRef bArray As Byte(), ByVal Start As Integer, ByVal Length As Integer) As Byte() 
    Dim b As Byte() 
    Dim i As Integer 

    ReDim b(Length) 
    For i = 0 To Length 
         b(i) = bArray(Start + i) 
    Next i 
    Return(b) 
End Function 

Private Shared Function GetStringFromByteArray(ByRef bArray As Byte()) As String 
    Dim s As String 
    Dim i As Integer 

    For i = 0 To bArray.Length - 1 
         s &= Chr(bArray(i)) 
    Next i 
    Return(s) 
End Function

Posted

I changed the code to use the .net features for string to bytes conversions and back to get rid of the helpers. :D But the the problem with RijndaelManaged reamins. It is decrypting the encrypted message into junk.

 

Here's the watch list for the CryptoWriter and it shows what I'm talking about in regards to the not supported exception.

 

http://studentpages.scad.edu/~ejackm20/error3.png

 

Public Shared Function EncryptRijndael(ByVal TextToEncrypt As String) As String

     Dim EncryptedData As String
     Dim EncryptedDataBytes As Byte()
     Dim PlainTextBytes As Byte()

     Try
          ' create the memory stream
          Dim MS As MemoryStream = New MemoryStream

          'convert the text into byte form in preperation to be encrypted
          PlainTextBytes = TextConverter.GetBytes(TextToEncrypt)

         'create the CryptoStream that ties together the MemoryStream and the ICryptoTransform
         Dim CS As CryptoStream = New CryptoStream(MS, RijEncryptor, CryptoStreamMode.Write) <-- this is where the exception first appears.

         'write the PlainTextBytes out to the CryptoStream and flush it
         CS.Write(PlainTextBytes, 0, PlainTextBytes.Length)
         CS.FlushFinalBlock()

         'close the CryptoStream
         CS.Close()

         'get the encrypted text from the MemoryStream
         'I can't simply just grab the string because the stream doesn't work that way
         EncryptedDataBytes = MS.ToArray()

         'close the MemoryStream
         MS.Close()

         'convert the EncryptedDataBytes array into a base64 string
         EncryptedData = System.Convert.ToBase64String(EncryptedDataBytes)

         Return (EncryptedData)

    Catch ex As Exception
         Console.WriteLine(Err.ToString())
         MsgBox(Err.ToString, MsgBoxStyle.OKOnly)
    End Try

End Function

Public Shared Function DecryptRijndael(ByVal EncryptedText As String) As String

    Dim EncryptedDataBytes As Byte()
    Dim DecryptedDataBytes As Byte()
    Dim DecryptedText As String

    Try
         'convert EncryptedText into a byte array to assign to the MemoryStream
         EncryptedDataBytes = System.Convert.FromBase64String(EncryptedText)

         ' create the memory stream
         Dim MS As MemoryStream = New MemoryStream(EncryptedDataBytes)

         'create the CryptoStream that ties together the MemoryStream and the ICryptoTransform
         Dim CS As CryptoStream = New CryptoStream(MS, RijDecryptor, CryptoStreamMode.Read)

         DecryptedDataBytes = New Byte(EncryptedDataBytes.Length) {}

         'Read the data out of the crypto stream.
         CS.Read(DecryptedDataBytes, 0, DecryptedDataBytes.Length)

         'Convert the byte array back into a string.
         DecryptedText = TextConverter.GetString(DecryptedDataBytes)

         MsgBox(DecryptedText, MsgBoxStyle.OKOnly)

         Return (DecryptedText)

    Catch ex As Exception
         Console.WriteLine(Err.ToString())
         MsgBox(Err.ToString, MsgBoxStyle.OKOnly)
    End Try

End Function

 

Any help on getting this to work would be much appreaciated.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...