2014-02-25 180 views
2

我在与生成KCV在C#.NET计算KCV与DES密钥

在参考this答案DES密钥出了问题,KCV DES密钥“ABCDEF”“ D5D44F“(因为只考虑前3个字节),它是由包含64位”0“的块生成的。

http://www.emvlab.org/keyshares/?combined=ABCDEF&combined_kcv=&one=&one_kcv=&two=&two_kcv=&three=&three_kcv=&numcomp=three&parity=ignore&action=Split

所以我确信我应该得到D5D44F的KCV:要确认这是正确的价值,我也是用这个工具,这给了我同样的结果进行检查。

现在,当我尝试在C#.NET中实现它时,我完全得到了一个不同的值。我结束了“7217BF”

这里是我的代码:

using System; 
using System.IO; 
using System.Security.Cryptography; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine(GetKCVDES()); 
      Console.ReadLine(); 
     } 

     public static string GetKCVDES() 
     { 
      byte[] k = new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }; 
      byte[] i = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 
      byte[] d = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 

      DES des = new DESCryptoServiceProvider(); 
      MemoryStream memoryStream = new MemoryStream(); 
      CryptoStream cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(k, i), CryptoStreamMode.Write); 
      StreamWriter writer = new StreamWriter(cryptoStream); 

      writer.Write(d); 
      writer.Flush(); 
      cryptoStream.FlushFinalBlock(); 
      writer.Flush(); 
      return ByteArrayToString(memoryStream.GetBuffer()).Remove(6); 
     } 

     public static string ByteArrayToString(byte[] ba) 
     { 
      StringBuilder hex = new StringBuilder(ba.Length * 2); 
      foreach (byte b in ba) 
       hex.AppendFormat("{0:x2}", b); 
      return hex.ToString().ToUpper(); 
     } 
    } 
} 

的ByteArrayToString功能只是字节数组到一个十六进制字符串转换。在传递之前,我还检查了Byte数组,以确保ByteArrayToString方法未提供不同的输出。

我也试着明确强制DES CSP的ECB和CBC模式,但是我得到了相同的结果。

有没有我用DES CSP错误地实现的东西?

+0

你有没有试过用'TransformBlock'您已经创建了一个ECB模式下使用'CreateEncryptor'加密器实例后?这里不需要所有这些流。 –

+0

谢谢owlstead,我似乎已经从你的建议中解决了它。我还没有实现TransformBlock函数,但重构删除其中一个流似乎已经修复它。现在发布答案。 – quinnoth

回答

0

感谢owlstead的评论,我从函数中删除了StreamWriter(开始摆脱流),它现在给出了预期的输出。

我还没有完全理解这些Stream对象是如何工作的,所以我不知道它现在的工作原理的真正原因,但是通过对GetKCVDES函数的更新,我得到了我想要的KCV得到(我还简化了它更多地与使用语句位):

public static string GetKCVDES() 
{ 
    byte[] key = new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }; 
    byte[] iv = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 
    byte[] data = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 

    DES des = new DESCryptoServiceProvider(); 

    using (MemoryStream memoryStream = new MemoryStream()) 
    { 
     using (CryptoStream cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(key, iv), CryptoStreamMode.Write)) 
     { 
      cryptoStream.Write(data, 0, data.Length); 
      cryptoStream.FlushFinalBlock(); 
      return ByteArrayToString(memoryStream.ToArray()).Remove(6); 
     } 
    } 
}