【问题标题】:How to do AES128 decryption in C#?如何在 C# 中进行 AES128 解密?
【发布时间】:2020-08-15 10:53:56
【问题描述】:

我有一个函数尝试在 C# 中解密一个字节数组。

解密函数:

string Decrypt(byte[] cipherText, byte[] Key, byte[] IV)
    {
        string plaintext = null;
        // Create AesManaged    
        using (AesManaged aes = new AesManaged())
        {
            aes.Mode = CipherMode.ECB;
            aes.Padding = PaddingMode.None;
            // Create a decryptor    
            ICryptoTransform decryptor = aes.CreateDecryptor(Key, IV);
            // Create the streams used for decryption.    
            using (MemoryStream ms = new MemoryStream(cipherText))
            {
                // Create crypto stream    
                using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                {
                    // Read crypto stream    
                    using (StreamReader reader = new StreamReader(cs))
                        plaintext = reader.ReadToEnd();
                }
            }
        }
        return plaintext;
    }

以下是我如何实现该功能:

byte[] app_key = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
byte[] data    = { 0xB6 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x73 ,0x69 ,0x2D ,0x01 ,0x00 ,0x01 ,0x18 ,0x4F ,0x84 ,0xE8 };
var sts = Decrypt(data, app_key, new byte[16]);
Console.WriteLine(BitConverter.ToString(Encoding.ASCII.GetBytes(sts)));

但是输出是 3F-3F-2F-31-3F-3F-3F-5E-61-2A-21-42-3F-2B 这是错误的!!!
正确的输出是 d4-f7-2f-31-a3-a8-d4-a3-5e-61-2a-21-42-88-2b-dc。

在线解密链接:http://aes.online-domain-tools.com/run/?inputType=frm-text&text=B6-00-00-00-00-00-73-69-2D-01-00-01-18-4F-84-E8&text_type=hex&function=aes&mode=ecb&key=01010101010101010101010101010101&key_type=hex&do=form-submit&decrypt=do

我在这里做错了什么?还有其他AES128解密功能吗?

谢谢...

【问题讨论】:

  • 你在 .NET 中处理一个 Unicode 字符串,而不是原始字节 - 这将把所有东西都扔掉。
  • 上一条评论中已经确定了该问题。只要二进制明文可以用 UTF8 解码,您的代码就可以工作。但发布的明文d4f7... 并非如此。因此,您必须替换 StreamReader(默认使用 UTF8 编码),例如带有MemoryStream msout(因此不应用编码),可以很容易地用cs.CopyTo(msout)填充并转换为带有msout.ToArray()的byte[](如果需要,可以从那里转换为字符串,例如带有BitConverter.ToString(...)) .

标签: c# .net encryption aes node-crypto


【解决方案1】:

试试这个代码;这是我的代码的一部分

AES 解密

public string AESDecrypt(string cryptText){

   // Seed and construct the transformation used for decrypting
   AESCryptoAlgorithm.GenerateIV();
   AESCryptoAlgorithm.GenerateIV();
   byte[] iv = AESCryptoAlgorithm.IV;
   int ivSize = iv.Length;
   ICryptoTransform decryptor = AESCryptoAlgorithm.CreateDecryptor(AESKey, iv);

   // The crypt text is expected to be encoded in base64 format, decode it...
   byte[] cryptBytes = Convert.FromBase64String(cryptText);
   byte[] cryptBytes = Convert.FromBase64String(cryptText);
   byte[] clearBytes = decryptor.TransformFinalBlock(cryptBytes, 0, cryptBytes.Length);

   return Encoding.ASCII.GetString(clearBytes, ivSize, clearBytes.Length - ivSize).TrimEnd('\0');
}

AES算法

protected Rijndael AESCryptoAlgorithm {
   get
   {
      if(_AESCryptoAlogrithm == null){
            _AESCryptoAlogrithm = Rijndael.Create();
            _AESCryptoAlogrithm.BlockSize = 128;
            _AESCryptoAlogrithm.Mode = CipherMode.CBC;
            _AESCryptoAlogrithm.Padding = PaddingMode.Zeros;
      }

      return _AESCryptoAlogrithm;
   }
}

十六进制解码

public static byte[] HexDecode(string hex){
   int len = hex.Length;
   byte[] bytes = new byte[len / 2];
   for (int i = 0; i < len; i += 2)
        bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
   return bytes;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-07
    • 2013-03-01
    • 2020-04-07
    • 1970-01-01
    • 2011-12-10
    • 2011-03-12
    • 1970-01-01
    • 2020-03-29
    相关资源
    最近更新 更多