I'm trying to implement this question i asked in the past Securely Encrypt 64bits w/o per element overhead?
In the immediate window i entered TripleDES.Create().LegalBlockSizes.First()
and got
{System.Security.Cryptography.KeySizes}
MaxSize: 64
MinSize: 64
SkipSize: 0
64bits/8bits per byte is 8bytes. Exactly what length a long is. Anyways i run it through the code below and the exception throws. The length of the block is 16bytes. Not what i want to have... I would ask how to change it to 64bits but as the results say the min and max are both 64bits so why am i getting 128bits instead??
long enc(long v, byte[] iv)
{
using (var m = new MemoryStream())
{
using (var c = des.CreateEncryptor(des.Key, iv))
using (var s = new CryptoStream(m, c, CryptoStreamMode.Write))
{
var b = BitConverter.GetBytes(v);
s.Write(b, 0, b.Length);
}
m.Flush();
var arr = m.ToArray();
if(arr.Length!=8)
throw new Exception();
return BitConverter.ToInt64(arr, 0);
}
}
I believe that this is due to padding. The default padding mode for symmetric ciphers in the .NET Framework is PKCS7:
If you add a line:
Before the rest of your encryption code, you should find the array is 8 bytes in length now. Of course, this means that you must ensure that any plaintext to be encrypted is exactly divisible by the block length.
And, also, you still need to transmit the IV which is another 8 bytes anyway. IVs should not be reused, so you've still doubled the size of storage/transmission compared to the plaintext.
Padding:
(Emphasis added. CBC is the default mode for ciphers in .NET Framework)