cryptostream,padding

Discussion in 'ASP .Net Security' started by Fred Herring, Jan 17, 2005.

  1. Fred Herring

    Fred Herring Guest

    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
    Fred Herring, Jan 17, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    7
    Views:
    5,645
    Joe Smith
    Jul 22, 2004
  2. RA
    Replies:
    1
    Views:
    364
  3. Becker

    Padding between textboxes

    Becker, Jun 24, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    1,267
    Eliyahu Goldin
    Jun 24, 2004
  4. =?Utf-8?B?U2FuZHk=?=

    VB code and Sql Server Ansi Padding

    =?Utf-8?B?U2FuZHk=?=, May 11, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,460
    =?Utf-8?B?U2FuZHk=?=
    May 11, 2005
  5. Fred Herring

    CryptoStream

    Fred Herring, Jan 6, 2005, in forum: ASP .Net Security
    Replies:
    4
    Views:
    117
    Alek Davis
    Jan 7, 2005
Loading...

Share This Page