Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should reuse the cypher instead of reinitialising it with the key and IV each time.</p> <p>The calling code:</p> <pre><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 &lt; 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)); </code></pre> <p>And the modified Encrypt part method:</p> <pre><code>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; } } </code></pre> <p>And the results:</p> <pre><code>=============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 </code></pre> <p>These values match the ones you specified, and those in this <a href="http://www.itl.nist.gov/fipspubs/fip113.htm" rel="nofollow">specification</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload