I found the below code online, but in its original state, the encryption was not using any padding (Padding.None), and was encrypting in blocks of 8. I wasn't able to get this code to properly encrypt / decrypt data so I was forced to have it encrypt the full length of the text. Is there something fundamentally wrong with what I have done? If so, could someone please show me the proper way to encrypt text in blocks of 8?
// 168-bit (three-key) 3DES (Triple-DES) encrypt a single 8-byte block (ECB mode)
// plain-text should be 8-bytes, key should be 24 bytes.
public byte[] TripleDesEncryptOneBlock(byte[] plainText, byte[] key)
{
// Create a new 3DES key.
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
// Set the KeySize = 192 for 168-bit DES encryption.
// The msb of each byte is a parity bit, so the key length is actually 168 bits.
des.KeySize = 192;
des.Key = key;
des.Mode = CipherMode.ECB;
des.Padding = PaddingMode.None;
ICryptoTransform ic = des.CreateEncryptor();
byte[] enc = ic.TransformFinalBlock(plainText, 0, 8);
return enc;
}
public byte[] TripleDesDecryptBlock(byte[] plainText, byte[] key)
{
// Create a new 3DES key.
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
// Set the KeySize = 192 for 168-bit DES encryption.
// The msb of each byte is a parity bit, so the key length is actually 168 bits.
des.KeySize = 192;
des.Key = key;
des.Mode = CipherMode.ECB;
des.Padding = PaddingMode.None;
ICryptoTransform ic = des.CreateDecryptor();
byte[] dec = ic.TransformFinalBlock(plainText, 0, 8);
return dec;
}
// Encrypt Text
textBox5.Text = ByteToString(TripleDesEncryptOneBlock(StringToByte(textBox5.Text),
StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));
// Decrypt Text
textBox5.Text = ByteToString(TripleDesDecryptBlock(StringToByte(textBox5.Text),
StringToByte("1 2 3 4 5 6 7 8 9 1 1 2 ")));