Link to home
Start Free TrialLog in
Avatar of shutuplonnie
shutuplonnie

asked on

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!

Avatar of S-Twilley
S-Twilley

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)
Avatar of shutuplonnie

ASKER

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
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())

ASKER CERTIFIED SOLUTION
Avatar of S-Twilley
S-Twilley

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Thanks Everything is working Well!!