You should reuse the cypher instead of reinitialising it with the key and IV each time.
The calling code:
var IV = new byte[8];//empty byte array
var key = new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
var data = Encoding.ASCII.GetBytes("7654321 Now is the time for ");
DES DESalg = DES.Create();
DESalg.Mode = CipherMode.CBC;
DESalg.Padding = PaddingMode.None;
ICryptoTransform crypt = DESalg.CreateEncryptor(key, IV);
var result = new byte[8];
int LoopCount = data.Length / 8;
for (int i = 0; i < LoopCount; i++)
{
Console.WriteLine("=============Round {0}==============", i + 1);
byte[] part = new byte[8];
Array.Copy(data, i * 8, part, 0, 8);
Console.WriteLine("Plain text : {0}", ByteArrayToString(part));
part = XorArray(part, result);
Console.WriteLine("DES INPUT : {0}", ByteArrayToString(part));
result = EncryptPart(crypt, part);
}
int remain = data.Length % 8;
if (remain != 0)
{
Console.WriteLine("===========Final Round==============");
byte[] LastPart = new byte[8];//
Array.Copy(data, data.Length - remain, LastPart, 0, remain);
Console.WriteLine("Plain text : " + ByteArrayToString(LastPart));
LastPart = XorArray(LastPart, result);
Console.WriteLine("DES INPUT : " + ByteArrayToString(LastPart));
result = EncryptPart(crypt, LastPart);
}
Console.WriteLine("Result: {0}", ByteArrayToString(result));
And the modified Encrypt part method:
public static byte[] EncryptPart(ICryptoTransform crypt, byte[] toEncrypt)
{
try
{
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream,
crypt,
CryptoStreamMode.Write);
cStream.Write(toEncrypt, 0, toEncrypt.Length);
cStream.FlushFinalBlock();
byte[] ret = mStream.ToArray();
cStream.Close();
mStream.Close();
Console.WriteLine("DES OUTPUT : " + ByteArrayToString(ret));
return ret;
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
}
And the results:
=============Round 1==============
Plain text : 3736353433323120
DES INPUT : 3736353433323120
DES OUTPUT : 21fb193693a16c28
=============Round 2==============
Plain text : 4e6f772069732074
DES INPUT : 6f946e16fad24c5c
DES OUTPUT : 6c463f0cb7167a6f
=============Round 3==============
Plain text : 68652074696d6520
DES INPUT : 04231f78de7b1f4f
DES OUTPUT : 956ee891e889d91e
===========Final Round============
Plain text : 666f722000000000
DES INPUT : f3019ab1e889d91e
DES OUTPUT : f1d30f6849312ca4
Result: f1d30f6849312ca4
These values match the ones you specified, and those in this specification