Error during decryption with SymmetricAlgorithm

  • Thread starter Thread starter Maciej Wolniewicz
  • Start date Start date
M

Maciej Wolniewicz

Hello Group,

I\m trying to add encryption to my appplication. it will be used to
encrypt communication between device and some external system.
Encryption and decryption works fine when I work with files, but I do
not want to use files, because I will read data from HTTP Stream and
using files adds to much overhead with IO operation. Then I thought
that I will use MemoryStream class like on dexktop application and
there start some problems.
Encryption looks like is working correctly, but I'm getting exception
when I want to decrypt data I'm getting
System.Security.Cryptography.CryptographicException with message:
"Unknown Error '80007001'."

Below is my code that I use.

Try

Dim enc As System.Security.Cryptography.SymmetricAlgorithm =
System.Security.Cryptography.SymmetricAlgorithm.Create("RC2")
Dim bKey As Byte() = System.Text.Encoding.UTF8.GetBytes("some
string")
ReDim Preserve bKey(15)
enc.Key = bKey

dim data2Encrypt(32) as Byte
Dim rg As System.Security.Cryptography.RandomNumberGenerator =
System.Security.Cryptography.RandomNumberGenerator.Create
rg.GetBytes(data2Encrypt)

Dim encryptedData() As Byte
Using ms As IO.MemoryStream = New IO.MemoryStream()
Dim cStream As CryptoStream = New CryptoStream(ms,
enc.CreateEncryptor(), CryptoStreamMode.Write)
cStream.Write(data2Encrypt, 0, data2Encrypt.Length)
encryptedData = ms.ToArray()
cStream.Close()
End Using

Dim decryptedData(encryptedData.Length - 1) As Byte
Using ms As IO.MemoryStream = New
IO.MemoryStream(encryptedData)
Dim cStream As CryptoStream = New CryptoStream(ms,
enc.CreateDecryptor(), CryptoStreamMode.Read)
cStream.Read(decryptedData, 0, decryptedData.Length)
cStream.Close()
End Using

Catch ex As Exception

End Try

And here is my StackTrace:

at System.Security.Cryptography.Utils._DecryptData()
at
System.Security.Cryptography.CryptoAPITransform.TransformFinalBlock()
at System.Security.Cryptography.CryptoStream.Read()
at EncryptionTest.fMain.MenuItem5_Click()
at System.Windows.Forms.MenuItem.OnClick()
at System.Windows.Forms.Menu.ProcessMnuProc()
at System.Windows.Forms.Form.WnProc()
at System.Windows.Forms.Control._InternalWnProc()
at Microsoft.AGL.Forms.EVL.EnterMainLoop()
at System.Windows.Forms.Application.Run()
at EncryptionTest.fMain.Main()

Can somebody know what's wrong?

Thanks in advance for your responce.

Reagrds,

Maciej Wolniewicz
 
This is because you close the cryptostream and then close the underlying
memorystream (the "using" keyword does that).
Closing the cryptostream frees ressources, such as your memorystream.
Only close the cryptostream, it will handle the memorystream itself. (you
need to rearrange your code a bit.)
 
Hi fred,

thanks for the solution. It works perfect!!
I have done it in different way, without using CryptoStream, but I
prefer this solution at least till now I have done it in my way, but
because it was not working I had to
callICryptoTransform.TransformFinalBlock method explicitly instead of
using streams. Thanks again to resolve this problem.

Regards,
Maciej Wolniewicz
 
im face the same problem with u .....


u solve this problem already???
reply me if u solve this problem.....
unknow error code on decrypt() is 80007001

can anyone help me??/ thank .....
 
Back
Top