Its a very simple code. But what I don't understand, When the Blocksize
is 8byte, Cipher size is 16bytes, why? I am expecting it to be same as the Blocksize
. From simple thinking, I give 64bits as plaintext and expect to have 64bits as cipher. And I really don't see any reason to do any padding here. It seems that after every 8bytes(Blocksize
) the Cipher becomes 8bytes more. 16bytes Block becomes 24bytes Cipher etc. Why is it like this? I really want to know.
And just for curiosity, is there any possibility/way to have 8bytes Cipher from 8bytes Block?
The 3DES code: (only encryption part)
static void Main(string[] args)
{
Console.WriteLine("Enter Plain Text: ");
string original =Console.ReadLine();
TripleDESCryptoServiceProvider myTripleDES = new TripleDESCryptoServiceProvider();
byte[] encrypted = EncryptStringToBytes(original,myTripleDES.Key, myTripleDES.IV);
string encrypt = Convert.ToBase64String(encrypted);
string decrypted = DecryptStringFromBytes(encrypted,myTripleDES.Key, myTripleDES.IV);
Console.WriteLine("encryted: " +encrypt);
Console.WriteLine("decrypted: " +decrypted);
Console.ReadLine();
}
static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
{
byte [] data= Encoding.UTF8.GetBytes(plainText);
Console.WriteLine("The Block length: " +data.Length);
TripleDESCryptoServiceProvider tdsAlg = new TripleDESCryptoServiceProvider();
tdsAlg.BlockSize = 64;
tdsAlg.KeySize = 128;
tdsAlg.Key = Key;
tdsAlg.IV = IV;
ICryptoTransform encryptor = tdsAlg.CreateEncryptor(tdsAlg.Key, tdsAlg.IV);
byte[] encrypted = encryptor.TransformFinalBlock(data, 0, data.Length);
Console.WriteLine("The Cipher length: " + encrypted.Length);
return encrypted;
}