[Mono-list] TripleDES encryption problem

Carlos Guzman Alvarez carlosga@telefonica.net
Sat, 30 Aug 2003 15:45:36 +0200


Hello:

> How the padding length has the same value as each byte on the padding i
> think this is correct on decryption you have (i'm not using PKCS7 for 
> this):
> 
>     fragment + mac + padding + padding length


To clarify this a little here is the code i'm using for encrypt a message.

Using PKCS7 as padding:

public byte[] EncryptRecord(byte[] fragment, byte[] mac)
{
	// Encryption ( fragment + mac [+ padding + padding_length] )
	MemoryStream ms = new MemoryStream();
	CryptoStream cs = new CryptoStream(ms, encryptionCipher, 
CryptoStreamMode.Write);

	cs.Write(fragment, 0, fragment.Length);
	cs.Write(mac, 0, mac.Length);
	if (cipherMode == CipherMode.CBC)
	{
		// Calculate padding_length
		int fragmentLength	= fragment.Length + mac.Length + 1;
		int paddingLength	= (((fragmentLength/blockSize)*8) + blockSize) - 
fragmentLength;

		// Write padding length byte
		cs.WriteByte((byte)paddingLength);
	}
	cs.Close();

	return ms.ToArray();
}


Using None as Padding:

public byte[] EncryptRecord(byte[] fragment, byte[] mac)
{
	// Encryption ( fragment + mac [+ padding + padding_length] )
	MemoryStream ms = new MemoryStream();
	CryptoStream cs = new CryptoStream(ms, encryptionCipher, 
CryptoStreamMode.Write);

	cs.Write(fragment, 0, fragment.Length);
	cs.Write(mac, 0, mac.Length);
	if (cipherMode == CipherMode.CBC)
	{
		// Calculate padding_length
		int fragmentLength	= fragment.Length + mac.Length + 1;
		int paddingLength	= (((fragmentLength/blockSize)*8) + blockSize) - 
fragmentLength;

		// Write padding + padding length bytes
		for (int i = 0; i <= paddingLength; i++)
		{
			cs.WriteByte((byte)paddingLength);
		}
	}
	cs.Close();

	return ms.ToArray();
}





-- 
Best regards

Carlos Guzmán Álvarez
Vigo-Spain