Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Security Encryption ( From encypt to decrypt )

Posted on 2005-03-30
6
Medium Priority
?
255 Views
Last Modified: 2010-04-24
Oh I took an example from microsoft, and i'm trying to Decrypt a string.

Dim ct As ICryptoTransform
    Dim ms As MemoryStream
    Dim cs As CryptoStream
    Dim byt() As Byte
        Dim IVbyt() As Byte
        Dim KeyByt() As Byte

        Try
            ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV)
        Catch


            KeyByt = Convert.FromBase64String(txtKey.Text)
            IVbyt = Convert.FromBase64String(txtIV.Text)

--->> I added this part

            mCSP.IV = IVbyt
            mCSP.Key = KeyByt

            ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV)

        End Try

        byt = Convert.FromBase64String(Value)

        ms = New MemoryStream
        cs = New CryptoStream(ms, ct, CryptoStreamMode.Write)
        cs.Write(byt, 0, byt.Length)
        cs.FlushFinalBlock()

        cs.Close()

        Return Encoding.UTF8.GetString(ms.ToArray())

I wanted to make it so i could enter the two manditory keys in a text box and it would decrypt my string but i get this error!

Object reference not set to an instance of an object.

Any help would be awsome!!

Thanks!

0
Comment
Question by:shutuplonnie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13663052
Which line do you get  the error on?

===========

Also... where if the "Value" variable coming from... just incase this is the bit causing the error:

byt = Convert.FromBase64String(Value)
0
 

Author Comment

by:shutuplonnie
ID: 13663240
Private Function DecryptString(ByVal Value As String) As String

That's where my value is coming from the example if you want to look at is from

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/cryptosimplified.asp

The error line is ..

mCSP.IV = IVbyt
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13663618
My guess is that you haven't initialized your mCSP object using one of the lines given on that page you gave me... e.g:

mCSP = SetEnc()

------------------

Which relates to doing...

mCSP = New DESCryptoServiceProvider

or

mCSP = New TripleDESCryptoServiceProvider.

Im not sure which type you are using but let's say it's the first of the two:


=================

        mCSP = New DESCryptoServiceProvider

        Dim ct As ICryptoTransform
        Dim ms As MemoryStream
        Dim cs As CryptoStream
        Dim byt() As Byte
        Dim IVbyt() As Byte
        Dim KeyByt() As Byte

        Try
            ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV)
        Catch


            KeyByt = Convert.FromBase64String(txtKey.Text)
            IVbyt = Convert.FromBase64String(txtIV.Text)

--->> I added this part

            mCSP.IV = IVbyt
            mCSP.Key = KeyByt

            ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV)

        End Try

        byt = Convert.FromBase64String(Value)

        ms = New MemoryStream
        cs = New CryptoStream(ms, ct, CryptoStreamMode.Write)
        cs.Write(byt, 0, byt.Length)
        cs.FlushFinalBlock()

        cs.Close()

        Return Encoding.UTF8.GetString(ms.ToArray())

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 12

Accepted Solution

by:
S-Twilley earned 2000 total points
ID: 13663644
Should say now... since i posted a bit hastily there... that since you're overwriting mCSP... I would assume that there is now no key/iv value within it... so:

ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV)

will throw an exception and always go into your catch block...

maybe an improvement of your code would be:

       

        Dim ct As ICryptoTransform
        Dim ms As MemoryStream
        Dim cs As CryptoStream
        Dim byt() As Byte
        Dim IVbyt() As Byte
        Dim KeyByt() As Byte

        If Not mCSP Is Nothing Then
            ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV)
        Else
            KeyByt = Convert.FromBase64String(txtKey.Text)
            IVbyt = Convert.FromBase64String(txtIV.Text)

            mCSP = New DESCryptoServiceProvider

            mCSP.IV = IVbyt
            mCSP.Key = KeyByt

            ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV)
        End If

        byt = Convert.FromBase64String(Value)

        ms = New MemoryStream
        cs = New CryptoStream(ms, ct, CryptoStreamMode.Write)
        cs.Write(byt, 0, byt.Length)
        cs.FlushFinalBlock()

        cs.Close()

        Return Encoding.UTF8.GetString(ms.ToArray())
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13663663
This assumes that since mCSP has been intialized... that you've probably set a KEY/IV value to it, you can add more checking, and try catch blocks to it if you like:

        If Not mCSP Is Nothing Then
            Try
                ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV)
            Catch
                KeyByt = Convert.FromBase64String(txtKey.Text)
                IVbyt = Convert.FromBase64String(txtIV.Text)

                mCSP = New DESCryptoServiceProvider ' If you have implemented the SetEnc() function, you may want to use that instead of "New DESCryptoServiceProvider"

                mCSP.IV = IVbyt
                mCSP.Key = KeyByt

                ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV)
            End Try
        Else
            KeyByt = Convert.FromBase64String(txtKey.Text)
            IVbyt = Convert.FromBase64String(txtIV.Text)

            mCSP = New DESCryptoServiceProvider ' If you have implemented the SetEnc() function, you may want to use that instead of "New DESCryptoServiceProvider"

            mCSP.IV = IVbyt
            mCSP.Key = KeyByt

            ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV)
        End If
0
 

Author Comment

by:shutuplonnie
ID: 13663980
Thanks Everything is working Well!!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question