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(t xtKey.Text )
IVbyt = Convert.FromBase64String(t xtIV.Text)
--->> I added this part
mCSP.IV = IVbyt
mCSP.Key = KeyByt
ct = mCSP.CreateDecryptor(mCSP. Key, mCSP.IV)
End Try
byt = Convert.FromBase64String(V alue)
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!
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.
Catch
KeyByt = Convert.FromBase64String(t
IVbyt = Convert.FromBase64String(t
--->> I added this part
mCSP.IV = IVbyt
mCSP.Key = KeyByt
ct = mCSP.CreateDecryptor(mCSP.
End Try
byt = Convert.FromBase64String(V
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
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!
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
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 TripleDESCryptoServiceProv ider.
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(t xtKey.Text )
IVbyt = Convert.FromBase64String(t xtIV.Text)
--->> I added this part
mCSP.IV = IVbyt
mCSP.Key = KeyByt
ct = mCSP.CreateDecryptor(mCSP. Key, mCSP.IV)
End Try
byt = Convert.FromBase64String(V alue)
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() )
mCSP = SetEnc()
------------------
Which relates to doing...
mCSP = New DESCryptoServiceProvider
or
mCSP = New TripleDESCryptoServiceProv
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.
Catch
KeyByt = Convert.FromBase64String(t
IVbyt = Convert.FromBase64String(t
--->> I added this part
mCSP.IV = IVbyt
mCSP.Key = KeyByt
ct = mCSP.CreateDecryptor(mCSP.
End Try
byt = Convert.FromBase64String(V
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(t xtKey.Text )
IVbyt = Convert.FromBase64String(t xtIV.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(t xtKey.Text )
IVbyt = Convert.FromBase64String(t xtIV.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
If Not mCSP Is Nothing Then
Try
ct = mCSP.CreateDecryptor(mCSP.
Catch
KeyByt = Convert.FromBase64String(t
IVbyt = Convert.FromBase64String(t
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.
End Try
Else
KeyByt = Convert.FromBase64String(t
IVbyt = Convert.FromBase64String(t
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.
End If
ASKER
Thanks Everything is working Well!!
===========
Also... where if the "Value" variable coming from... just incase this is the bit causing the error:
byt = Convert.FromBase64String(V