Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cryptostream troubles

Status
Not open for further replies.

kettch

Programmer
Mar 5, 2001
110
US
I am trying to decrypt an xmldocument object from a memory stream:

Code:
        Dim rm As New RijndaelManaged()
        rm.Key = rsa.Decrypt(symmetricKey, False)
        rm.IV = rsa.Decrypt(symmetricIV, False)
        'Dim doc As Byte()
        Dim cryptDoc As Byte() = My.Computer.FileSystem.ReadAllBytes("c:\temp\encryptedxmlbytestream")
        Dim ms As New MemoryStream
        For Each b As Byte In cryptDoc
            ms.WriteByte(b)
        Next
        'ms.Flush()

        Dim CryptStream As New CryptoStream(ms, rm.CreateDecryptor(rm.Key, rm.IV), CryptoStreamMode.Read)
        Dim settings As New XmlReaderSettings
        Dim reader As XmlReader = XmlReader.Create(CryptStream)

        Dim decryptedXML As New XmlDocument
        decryptedXML.LoadXml(reader.ReadOuterXml)

the problem is that when it gets to the XmlReader.Create line, it throws an indexoutofrangeexception. Here is the stack trace:
Code:
   at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
   at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
   at System.Security.Cryptography.CryptoStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.Xml.XmlTextReaderImpl.InitStreamInput(Uri baseUri, String baseUriStr, Stream stream, Byte[] bytes, Int32 byteCount, Encoding encoding)
   at System.Xml.XmlTextReaderImpl..ctor(Stream stream, Byte[] bytes, Int32 byteCount, XmlReaderSettings settings, Uri baseUri, String baseUriStr, XmlParserContext context, Boolean closeInput)
   at System.Xml.XmlReader.CreateReaderImpl(Stream input, XmlReaderSettings settings, Uri baseUri, String baseUriStr, XmlParserContext inputContext, Boolean closeInput)
   at System.Xml.XmlReader.Create(Stream input)
   at ProductActivation.Form1.btnDecrypt_Click(Object sender, EventArgs e) in C:\Software Projects\ProductActivation\ProductActivation\Form1.vb:line 164
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at ProductActivation.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
   at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

I'm new to the crypto libraries, so I'm sure I've got something boogered up.

Thanks for any help.
 
try this


StreamReader sr = new StreamReader(CryptoStream);

then read the StreamReader into an XMLDoc
 
I rearranged it to look like this:

Code:
        Dim rsa As New RSACryptoServiceProvider()
        rsa.ImportParameters(privateKey)

        Dim rm As New RijndaelManaged()
        rm.Key = rsa.Decrypt(symmetricKey, False)
        rm.IV = rsa.Decrypt(symmetricIV, False)
        Dim cryptDoc As Byte() = My.Computer.FileSystem.ReadAllBytes("c:\temp\cryptedDoc")
    
        Dim ms As New MemoryStream
        ms.Write(cryptDoc, 0, cryptDoc.Length)
        ms.Position = 0

        Dim CryptStream As New CryptoStream(ms, rm.CreateDecryptor(rm.Key, rm.IV), CryptoStreamMode.Read)
       
               Dim reader As New StreamReader(CryptStream)
        txtDecryptedXML.Text = reader.ReadToEnd

It happens at the line with reader.readtoend
The stacktrace says that it's happening inside the cryptostream's decryption.

I've been working on this for a week and it's starting to get annoying. Every error i've been getting ends up with the only search results on google being my own posts in forums.
 
what is the error message you're getting on the ReadToEnd line now?
 
interesting, if I set the memorstream's position to 0 I get "Length of the data to decrypt is invalid" at the readtoend line. If I don't set it to 0 I get an IndexOutOfRangeException.
 
put a breakpoint on the line txtDecryptedXML.Text = reader.ReadToEnd and then see what the size of the CryptStream is
 
by size do you mean length? CryptoStreams don't support seeking which is required to get the length.
 
It does in VS 2005 ... my intellisense gives me Length and Seek....
 
Yes, they are supported by the base class, but it is overriden and disabled for the cryptostream.
 
I dunno what to tell you. Here is some code I use that works just fine.

Code:
    public static string DecryptCreditCardInfo(string strItem)
    {
        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
        byte[] buffer = Convert.FromBase64String(strItem);
        MemoryStream ms = new MemoryStream(buffer);
        CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateDecryptor(KEY_64, IV_64), CryptoStreamMode.Read);
        StreamReader sr = new StreamReader(cs);
        return sr.ReadToEnd();

    }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top