title: C# DES ECB Encryption
body: <p>I am having difficulty encrypting something in C#.</p>
  
  <p>I have 3 variables.
  First one is a 16 digit hex,lets call it X value I.E 0072701351979990
  Second one is also a 16 digit hex value, lets call it Y I.E 3008168011FFFFFF</p>
  
  <p>These two values have to be XOR 'ed to get the key for the DES-ECB encryption.</p>
  
  <p>Thus resulting in 307a66934068666f . Now thus is my keyblock for the encryption.
  Then i have this as my datablock,which is 64 bits for encryption 0E329232EA6D0D73</p>
  
  <p>Now i have the following code for encryption this. 
  The result of the encryption should be XOR'ed with the datablock again and 
  result in a 64bit result. This is not the case.</p>
  
  <p>This is my code for the encryption</p>
  
  <pre><code>$ public static string DESEncrypt(string keyBlock,string dataBlock){
          DES desEncrypt = new DESCryptoServiceProvider();
          byte[] keyBlockBytes = BitConverter.GetBytes(Convert.ToInt64(keyBlock, 16));
          byte[] dataBlockBytes = BitConverter.GetBytes(Convert.ToInt64(dataBlock, 16));
          desEncrypt.Mode = CipherMode.ECB;
          desEncrypt.Key = keyBlockBytes;
          ICryptoTransform transForm = desEncrypt.CreateEncryptor();
          MemoryStream enecryptedStream = new MemoryStream();
          CryptoStream cryptoStream = new CryptoStream(enecryptedStream, transForm, CryptoStreamMode.Write);
          cryptoStream.Write(dataBlockBytes, 0, dataBlockBytes.Length);
          cryptoStream.FlushFinalBlock();
          byte[] encryptedData = new byte[enecryptedStream.Length];
          enecryptedStream.Position = 0;
          enecryptedStream.Read(encryptedData, 0, encryptedData.Length);
          string enCryptedHex = BitConverter.ToString(encryptedData);
  
          return enCryptedHex.Replace("-",""); 
      }
  </code></pre>
  
  <p>What am i doing wrong?</p>
  
  <p><strong>UPDATED QUESTION</strong> 
  I have tested the above solution from CodeInChaos. 
  It does give me back a 64 bit result. But still there is something wrong.</p>
  
  <p>Here is my updated code.</p>
  
  <p>The keyblock value is abababababababab
  and the data block value is 215135734068666F.</p>
  
  <p>The resultant 64 bit result should be XOR'ed with the data block again.</p>
  
  <p>The final answer is suppose to be 414945DD33C97C47 but I get 
  288a08c01a57ed3d. </p>
  
  <p>Why does it not come out right? </p>
  
  <p>Here is the specifications in suppliers documentation for the encryption.</p>
  
  <p>Encryption is DEA in accordance with FIPS 46-3, single DES in ECB mode, using a single 64-
  bit DES Key with odd parity.</p>
  
  <pre><code>$      public static string DESEncrypt(string keyBlock,string dataBlock){
          DES desEncrypt = new DESCryptoServiceProvider();
          byte[] keyBlockBytes = BitConverter.GetBytes(Convert.ToInt64(keyBlock, 16));
          byte[] dataBlockBytes = BitConverter.GetBytes(Convert.ToInt64(dataBlock, 16));
          desEncrypt.Mode = CipherMode.ECB;
          desEncrypt.Key = keyBlockBytes;
          desEncrypt.Padding = PaddingMode.None;
          ICryptoTransform transForm = desEncrypt.CreateEncryptor();
          MemoryStream enecryptedStream = new MemoryStream();
          CryptoStream cryptoStream = new CryptoStream(enecryptedStream, transForm, CryptoStreamMode.Write);
          cryptoStream.Write(dataBlockBytes, 0, dataBlockBytes.Length);
          cryptoStream.FlushFinalBlock();
          byte[] encryptedData = enecryptedStream.ToArray();
          string enCryptedHex = BitConverter.ToString(encryptedData);
          enCryptedHex = enCryptedHex.Replace("-", "");
          long iDeaEncrypt = Convert.ToInt64(enCryptedHex, 16);
          long iDataBlock = Convert.ToInt64(dataBlock, 16);
          long decoderKey = iDeaEncrypt ^ iDataBlock;
          string decKeyHex = Convert.ToString(decoderKey, 16);
          return decKeyHex;
      }
  </code></pre>