This is because we use PKCS style padding, and we need to add a block of padding. So 
24 bytes will yield 32 bytes of encrypted text. When this encrypted bytes will be 
decrypted, we will know how to remove the 8 bytes of padding and will return the 
original 24 bytes of plain text.

Thanks,
--Tarik

-----Original Message-----
From: Valentina Shkolnikov [mailto:[EMAIL PROTECTED]]
Sent: Friday, June 14, 2002 12:22 PM
To: [EMAIL PROTECTED]
Subject: [DOTNET] TransformFinalBlock returns


Why the 'ICryptoTransform.TransformFinalBlock' method returns an array
larger than the .OutputBlockSize property states?  As a result I'm getting
discrepancy when encrypting-decrypting.
Is this an undocumented feature, or I'm doing something wrong way:

...
Private mCryptor As ICryptoTransform
Private mDesCSP As New DESCryptoServiceProvider()
Private mIV() As Byte
...
Public Function Encrypt(ByVal bytesin() As Byte, _
                    ByRef bytesout() As Byte, _
                    ByVal byteskey() As Byte, _
                    ByRef bytesiv() As Byte) As Integer
   '
   mCryptor = mDesCSP.CreateEncryptor(byteskey, mIV)
   Return MyCrypt(bytesin, bytesout)
End Function
...
Public Function Decrypt(ByVal bytesin() As Byte, _
                    ByRef bytesout() As Byte, _
                    ByVal byteskey() As Byte, _
                    ByRef bytesiv() As Byte) As Integer
   '
   mCryptor = mDesCSP.CreateDecryptor(byteskey, mIV)
   Return MyCrypt(bytesin, bytesout)
End Function
...
Private Function MyCrypt(ByVal bytesin() As Byte, _
                    ByRef bytesout() As Byte) As Integer
        '
        Dim stat As Integer
        Dim bIn() As Byte
        Dim bOut() As Byte
        '
        bIn = bytesin   '16 bytes
        Console.WriteLine("Out block size: ",
mCryptor.OutputBlockSize.ToString)   '8 bytes
        '
        '  This always returns 8 bytes larger arr.than the input (24 bytes)
        bOut = mCryptor.TransformFinalBlock(bIn, 0, bIn.GetLength(0))
        bytesout = bOut
        Return 0
End Function

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to