Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Security > cryptostream,padding

Reply
Thread Tools

cryptostream,padding

 
 
Fred Herring
Guest
Posts: n/a
 
      01-17-2005
My application produces BLOB's which need to be compressed and encrypted
prior to transfer over the internet. I have inplemented a class called
RijndaelSimple in my application to provide encrypt/decrypt methods. The
procedure I would like to follow is to create my BLOB as a byte array,
compress, encrypt. I use a third party compression component. My question
has to do with the padding that occurs during the encryption. When I load my
encrypted BLOB and decrypt it, do I need to manually strip padding or is
there someway to have this done automatically during the decrypt. My
compression/decompression routine is senstive to checksum errors?

Public Class RijndaelSimple
Public Shared Function Encrypt(ByVal BLOB As Byte()) As Byte()
Try
Dim ms As New MemoryStream
Dim symmetricKey As RijndaelManaged = New RijndaelManaged
symmetricKey.Mode = CipherMode.CBC
symmetricKey.Padding = PaddingMode.PKCS7
Dim initVectorBytes As Byte() = {100, 200, 50, 12, 19, 123, 220,
55, 22, 190, 215, 45, 254, 1, 76, 15}
Dim keyBytes As Byte() = {3, 91, 52, 200, 33, 207, 88, 13, 140,
2, 22, 99, 1, 254, 4, 200, 95, 23, 77, 245, 11, 194, 205, 2, 7, 201, 65, 90,
6, 32, 0, 8}
Dim encryptor As ICryptoTransform =
symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)
Dim cryptoStream As CryptoStream = New CryptoStream(ms,
encryptor, CryptoStreamMode.Write)
cryptoStream.Write(BLOB, 0, BLOB.Length)
cryptoStream.FlushFinalBlock()
Dim EncryptedBLOB As Byte() = ms.ToArray()
ms.Close()
cryptoStream.Close()
Encrypt = EncryptedBLOB
Catch MyException As Exception
MsgBox("Error during encryption due to: " & MyException.Message)
End Try
End Function

Public Shared Function Decrypt(ByVal EncryptedBLOB As Byte()) As Byte()
Try
Dim ms As New MemoryStream(EncryptedBLOB)
Dim symmetricKey As RijndaelManaged = New RijndaelManaged
symmetricKey.Mode = CipherMode.CBC
symmetricKey.Padding = PaddingMode.PKCS7
Dim initVectorBytes As Byte() = {100, 200, 50, 12, 19, 123, 220,
55, 22, 190, 215, 45, 254, 1, 76, 15}
Dim keyBytes As Byte() = {3, 91, 52, 200, 33, 207, 88, 13, 140,
2, 22, 99, 1, 254, 4, 200, 95, 23, 77, 245, 11, 194, 205, 2, 7, 201, 65, 90,
6, 32, 0, 8}
Dim decryptor As ICryptoTransform =
symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)
Dim cryptoStream As CryptoStream = New CryptoStream(ms,
decryptor, CryptoStreamMode.Read)
Dim DecryptedBLOB As Byte()
ReDim DecryptedBLOB(EncryptedBLOB.Length)
cryptoStream.Read(DecryptedBLOB, 0, DecryptedBLOB.Length)
ms.Close()
cryptoStream.Close()
Decrypt = DecryptedBLOB
Catch MyException As Exception
MsgBox("Error during decryption due to: " & MyException.Message)
End Try
End Function
End Class
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments