Question Managed Rijndael, encr. output is larger than input

LastUser

New member
Joined
Feb 17, 2010
Messages
2
Programming Experience
5-10
Hi,

I'm tried all the different samples for Managed Rijndael I found in the internet for .NET, like the one attached.

Everytime I crypt an array of bytes whitch has a size of 128 Bytes (16*8), the encrypted result has a size of 144 Bytes (128+16).

Everywhere you can read that Rijndaels AES is an blockchiffre which keeps the size the same as the origin, or does it not? :confused:

I tried a source of 127 Bytes for example and the target grows to 128, thats okay. But the exact size of 128 does not work correct. The same happens to 256 Bytes, they grow.
Btw: The crypting and decrypting works correctly. But size matters to.

I need: source 128 Bytes -> encrypted 128 Byte-> decrypted 128 Byte.

Does any one have the same problem or an idea to solve it? Help is appreciated :) .

Example, but any other sample using Managed Rijndael I found does the same fault:
VB.NET:
Imports System.Security.Cryptography
Imports System.IO

    Public Function EncryptString128Bit(ByVal bytValue As Byte(), ByVal bytKey As Byte()) As Byte()

        Dim bytEncoded() As Byte
        Dim bytIV() As Byte = {121, 241, 10, 1, 132, 74, 11, 39, 255, 91, 45, 78, 14, 211, 22, 62}

        Dim objMemoryStream As New MemoryStream()
        Dim objCryptoStream As CryptoStream
        Dim objRijndaelManaged As RijndaelManaged
        ReDim bytEncoded(0)

        objRijndaelManaged = New RijndaelManaged()

        objCryptoStream = New CryptoStream(objMemoryStream, objRijndaelManaged.CreateEncryptor(bytKey, bytIV), CryptoStreamMode.Write)
        objCryptoStream.Write(bytValue, 0, bytValue.Length)

        objCryptoStream.FlushFinalBlock()

        bytEncoded = objMemoryStream.ToArray

        ReDim Preserve bytEncoded(255)
        objMemoryStream.Close()
        objCryptoStream.Close()

        objMemoryStream = Nothing
        objCryptoStream = Nothing
        Return bytEncoded

    End Function

    Public Function DecryptString128Bit(ByVal bytDataToBeDecrypted As Byte(), ByVal bytDecryptionKey As Byte()) As Byte()

        Dim bytTemp() As Byte
        Dim bytIV() As Byte = {121, 241, 10, 1, 132, 74, 11, 39, 255, 91, 45, 78, 14, 211, 22, 62}
        Dim objRijndaelManaged As New RijndaelManaged()
        Dim objMemoryStream As MemoryStream
        Dim objCryptoStream As CryptoStream
        Dim strReturnString As String = String.Empty

        ReDim bytTemp(bytDataToBeDecrypted.Length)

        objMemoryStream = New MemoryStream(bytDataToBeDecrypted)

        objCryptoStream = New CryptoStream(objMemoryStream, objRijndaelManaged.CreateDecryptor(bytDecryptionKey, bytIV), CryptoStreamMode.Read)

        objCryptoStream.Read(bytTemp, 0, bytTemp.Length)

        objMemoryStream.Close()
        objCryptoStream.Close()
        objMemoryStream = Nothing
        objCryptoStream = Nothing
        Return bytTemp

    End Function

Thx, LastUser
 
Solution

Solution:

Set the Padding to none, because there will be no more control informations attached to the crypted data.

VB.NET:
'
objRijndaelManaged.Padding=PaddingMode.None
'


thx to Dave S. :)

LastUser
 
Solution:
NOT using padding isn't really a "solution". In that case you have to do the padding yourself or otherwise only can work with multiples of the block size.

Who cares if the output is longer than the input? Some ppl like to use IVs (and ALL ppl should use them) and usually write the used IV in front of the output stream. This makes output longer than input anyway.
 
Back
Top