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