cryptostream,padding

F

Fred Herring

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top