DES ECB C#加密/解密

时间:2012-05-28 11:35:33

标签: c# encryption des ecb

我对先前加密的数据进行解密存在问题。我正在使用具有三个不同密钥的顺序加密 - 解密 - 加密来获得三重效果。加密函数正常工作(返回8字节数组),但解密函数返回空数组。

    public static byte[] EncryptDES(byte[] clearData, byte[] key) 
    { 
        DES desEncrypt = new DESCryptoServiceProvider(); 
        desEncrypt.Mode = CipherMode.ECB; 
        desEncrypt.Key = key; 
        ICryptoTransform transForm = desEncrypt.CreateEncryptor(); 
        MemoryStream encryptedStream = new MemoryStream(); 
        CryptoStream cryptoStream = new CryptoStream(encryptedStream, transForm, CryptoStreamMode.Write); 
        cryptoStream.Write(clearData, 0, clearData.Length); 
        byte [] encryptedData = encryptedStream.ToArray(); 
        return encryptedData;
    }

    public static byte[] DecryptDES(byte[] clearData, byte[] key)
    {
        DES desDecrypt = new DESCryptoServiceProvider();
        desDecrypt.Mode = CipherMode.ECB;
        desDecrypt.Key = key;
        ICryptoTransform transForm = desDecrypt.CreateDecryptor();
        MemoryStream decryptedStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(decryptedStream, transForm, CryptoStreamMode.Write);
        cryptoStream.Write(clearData, 0, clearData.Length);
        byte[] encryptedData = decryptedStream.ToArray();
        return encryptedData;
    }

    public static byte[] Encrypt3DES(byte[] clearData, byte[] key0, byte[] key1, byte[] key2) 
    {
        byte[] encryptedData1 = new byte[clearData.Length];
        byte[] encryptedData2 = new byte[clearData.Length];
        byte[] encryptedData3 = new byte[clearData.Length];
        encryptedData1 = DESCrypto.EncryptDES(clearData    , key0);
        encryptedData2 = DESCrypto.DecryptDES(encryptedData1, key1);
        encryptedData3 = DESCrypto.EncryptDES(encryptedData2, key2);
        return encryptedData3;
    } 

我做错了什么?

2 个答案:

答案 0 :(得分:0)

框架中已经存在

TripleDES,但我想您希望将自己的实现用于教育目的。

你做的事情比必要的更复杂。由于您使用的是流,为什么不将它们全部链接起来:

public static byte[] TripleDESEncrypt(byte[] plainText, byte[] key1, byte[] key2, byte[] key3)
{
  var des = DES.Create();
  des.Mode = CipherMode.ECB;

  des.Padding = PaddingMode.None;
  des.Key = key3;
  var encryptor1 = des.CreateEncryptor();

  des.Key = key2;
  var decryptor = des.CreateDecryptor();

  des.Padding = PaddingMode.PKCS7;
  des.Key = key1;
  var encryptor2 = des.CreateEncryptor();

  byte[] result;
  using (var ms = new MemoryStream())
  {
    using (var cs1 = new CryptoStream(ms, encryptor1, CryptoStreamMode.Write))
    using (var cs2 = new CryptoStream(cs1, decryptor, CryptoStreamMode.Write))
    using (var cs3 = new CryptoStream(cs2, encryptor2, CryptoStreamMode.Write))
      cs3.Write(plainText, 0, plainText.Length);

    result = ms.ToArray();
  }


  return result;
}

public static byte[] TripleDESDecrypt(byte[] cipherText, byte[] key1, byte[] key2, byte[] key3)
{
  var des = DES.Create();
  des.Mode = CipherMode.ECB;

  des.Padding = PaddingMode.PKCS7;
  des.Key = key1;
  var decryptor1 = des.CreateDecryptor();

  des.Padding = PaddingMode.None;
  des.Key = key2;
  var encryptor = des.CreateEncryptor();

  des.Key = key3;
  var decryptor2 = des.CreateDecryptor();

  byte[] result;
  using (var ms = new MemoryStream())
  {
    using (var cs1 = new CryptoStream(ms, decryptor1, CryptoStreamMode.Write))
    using (var cs2 = new CryptoStream(cs1, encryptor, CryptoStreamMode.Write))
    using (var cs3 = new CryptoStream(cs2, decryptor2, CryptoStreamMode.Write))
      cs3.Write(cipherText, 0, cipherText.Length);

    result = ms.ToArray();
  }


  return result;
}

记下using块的用法以及填充如何应用于不同的流。

框架TripleDES比上述代码快2.5倍。

public static byte[] TripleDESEncryptFramework(byte[] plainText, byte[] key)
{
  var tdes = TripleDES.Create();
  tdes.Mode = CipherMode.ECB;
  tdes.Padding = PaddingMode.PKCS7;
  tdes.Key = key;

  var encryptor = tdes.CreateEncryptor();

  byte[] result;
  using (var ms = new MemoryStream())
  {
    using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
      cs.Write(plainText, 0, plainText.Length);

    result = ms.ToArray();
  }

  return result;
}

如果要比较两种不同加密方法的结果,则需要记住TripleDES的24位密钥实际上是放在一个数组中的3个密钥:

[  key1  ][  key2  ][  key3  ]
==============================
[            key             ]

答案 1 :(得分:0)

只需要cryptoStream.FlushFinalBlock()。它的代码效果很好:

   //ENCRYPT
   public static byte[] EncryptDES(byte[] clearData, byte[] key) 
   { 
       DES desEncrypt = new DESCryptoServiceProvider(); 
       desEncrypt.Mode = CipherMode.ECB; 
       desEncrypt.Key = key; 
       ICryptoTransform transForm = desEncrypt.CreateEncryptor(); 
       MemoryStream encryptedStream = new MemoryStream(); 
       CryptoStream cryptoStream = new CryptoStream(encryptedStream, transForm, CryptoStreamMode.Write); 
       cryptoStream.Write(clearData, 0, clearData.Length);
       cryptoStream.FlushFinalBlock();
       return encryptedStream.ToArray();
   }

   //DECRYPT
   public static byte[] DecryptDES(byte[] clearData, byte[] key)
   {
       DES desDecrypt = new DESCryptoServiceProvider();
       desDecrypt.Mode = CipherMode.ECB;
       desDecrypt.Key = key;
       ICryptoTransform transForm = desDecrypt.CreateDecryptor();
       MemoryStream decryptedStream = new MemoryStream();
       CryptoStream cryptoStream = new CryptoStream(decryptedStream, transForm, CryptoStreamMode.Write);
       cryptoStream.Write(clearData, 0, clearData.Length);
       cryptoStream.FlushFinalBlock();
       return decryptedStream.ToArray();
   }