• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

Security Encryption ( From encypt to decrypt )

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
shutuplonnie
Asked:
shutuplonnie
  • 4
  • 2
1 Solution
 
S-TwilleyCommented:
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
 
shutuplonnieAuthor Commented:
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
 
S-TwilleyCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
S-TwilleyCommented:
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
 
S-TwilleyCommented:
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
 
shutuplonnieAuthor Commented:
Thanks Everything is working Well!!
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now