I intend to use TripleDES in one of my project. I was doing some experiments to be comfortable with it. I understand block size of triple DES is 8 bytes so I assume that if give 8 byte of data, I should get 8 bytes of encrypted data. But what I get is:
Input Size | Encrypted Size . | . . | . 6 bytes | 8 bytes 7 bytes | 8 bytes 8 bytes | 16 bytes 9 bytes | 16 bytes . | . . | .
Is it normal? Is it the way it is supposed to work. Here is how I am trying to use triple DES:
class TripleDESEncryption
{
private readonly TripleDESCryptoServiceProvider engine;
public TripleDESEncryption () : this (256) { }
public TripleDESEncryption (int keySizeInBits) {
engine = new TripleDESCryptoServiceProvider { KeySize = keySizeInBits };
engine.GenerateKey ();
}
public byte[] Encrypt (byte[] plain) {
return engine.CreateEncryptor ().TransformFinalBlock (plain, 0, plain.Length);
}
public byte[] Decrypt (byte[] encrypted) {
return engine.CreateDecryptor ().TransformFinalBlock (encrypted, 0, encrypted.Length);
}
}
class Program
{
static readonly int MAX_TEXT_LENGTH = 128;
static void Main (string[] args) {
Console.WriteLine ("{0,10}{1,10}{2,10}{3,10}", "Algo", "Key Size", "Input Size", "Encrypted Size");
var tripleDES = new TripleDESEncryption ();
var input = new List<byte> ();
for (int i = 0; i <= MAX_TEXT_LENGTH; i++) {
var plain = input.ToArray ();
var encrypted = tripleDES.Encrypt (plain);
Console.WriteLine ("{0,10}{1,10}{2,10}{3,10}", "Triple DES", keySize, input.Count, encrypted.Length);
input.Add (0x65);
}
Console.ReadLine ();
}
}
TripleDESCryptoServiceProvider defaults to using PKCS7-padding. This pads any message to the next multiple of the block-size.
To avoid using padding, just set the Padding
-property to PaddingMode.None
new TripleDESCryptoServiceProvider {
KeySize = keySizeInBits,
Padding = PaddingMode.None
};