title: How can I implement CBC-MAC with DES?
body: <p>I should implement a MAC-CBC generation method in C# with some information about the cryptography algorithm. Here's what I have:</p>
  
  <ul>
  <li>I should use DES.</li>
  <li>The key is <code>byte[] {11, 11, 11, 11, 11, 11, 11, 11}</code></li>
  <li>The data (16 bytes) should be encrypted in 8-byte parts. First 8 bytes is encrypted using <code>Instance Vector = new byte[8]</code> (8 bytes with 0 value). (CBC?)</li>
  <li>that last 8 bytes of the encrypted value should be converted to Hex string. this is the result I should send.</li>
  </ul>
  
  <p>With this information, I have implemented the following method:</p>
  
  <pre><code>public static string Encrypt(byte[] data)
  {
      var IV = new byte[8];
      var key = new byte[] { 11, 11, 11, 11, 11, 11, 11, 11 };
      var result = new byte[16];
  
      // Create DES and encrypt.
      var des = DES.Create();
      des.Key = key;
      des.IV = IV;
      des.Padding = PaddingMode.None;
      des.Mode = CipherMode.CBC;
      ICryptoTransform cryptoTransform = des.CreateEncryptor(key, IV);
      cryptoTransform.TransformBlock(data, 0, 16, result, 0);
  
      // Get the last eight bytes of the encrypted data.
      var lastEightBytes = new byte[8];
      Array.Copy(result, 8, lastEightBytes, 0, 8);
  
      // Convert to hex.
      var hexResult = string.Empty;
      foreach (byte ascii in lastEightBytes)
      {
          int n = (int)ascii;
          hexResult += n.ToString("X").PadLeft(2, '0');
      }
  
      return hexResult;
  }
  </code></pre>
  
  <p>The sample raw data they have provided me is: <code>input=byte[] {0, 6, 4, 1, 6, 4, 1, 7, E, E, F, F, F, F, B, B)</code> which should return the output of value: <code>A7CBFB3C730B059C</code>. This means the last eight bytes of encrypted data should be: <code>byte[] {167, 203, 251, 60, 115, 11, 05, 156}</code>.  </p>
  
  <p>But unfortunately using the above method, I get: <code>32D91200D0007632</code>. meaning my encrypted data is not correct. (the last eight byte of my method's generated encrypted value is <code>byte[] {50, 207, 18, 0, 208, 0, 118, 50}</code>).  </p>
  
  <p>Is there any way that I can find out what I should do to get to A7CB...? Am I doing something wrong?</p>