如何实现“AES/CFB/NoPadding"WinRT C# 中的加密/解密? [英] How to implement "AES/CFB/NoPadding" Encryption/Decryption in WinRT C#?

查看:60
本文介绍了如何实现“AES/CFB/NoPadding"WinRT C# 中的加密/解密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们现在正在将现有代码移植到Windows 8,从服务器下载的数据文件是在AES/CFB/NoPadding"中加密的,我们无法更改数据加密模式,因为该数据文件被很多人使用现有客户.

We are porting the existing code to Windows 8 now, the data file downloaded from the server is encrypted in "AES/CFB/NoPadding", and we cannot change the data encryption mode because the data file is used by a lot of existing clients.

在 WinRT 中,[SymmetricAlgorithmNames][1] 不包含匹配AES/CFB/NoPadding"的名称.从 [SymmetricKeyAlgorithmProvider][2] 文档中,NoPadding 算法不包含CFB"

  • 无填充:
  • DES_CBC
  • DES_ECB
  • 3DES_CBC
  • 3DES_ECB
  • RC2_CBC
  • RC2_ECB
  • AES_CBC
  • AES_ECB
  • In WinRT , the [SymmetricAlgorithmNames][1] doesn't contains a name to match "AES/CFB/NoPadding". And from the [SymmetricKeyAlgorithmProvider][2] document, the NoPadding algorithms don't contain "CFB"

    • No padding:
    • DES_CBC
    • DES_ECB
    • 3DES_CBC
    • 3DES_ECB
    • RC2_CBC
    • RC2_ECB
    • AES_CBC
    • AES_ECB
    • 推荐答案

      一种选择是使用 BouncyCastle 库,它们确实支持 Aes/Ofb/NoPadding.

      One option is to use BouncyCastle library, they do support Aes/Ofb/NoPadding.

      另一种选择是使用 c# 框架中的 System.Security.Cryptography.Aes 并使用 PaddingMode.Zeros 进行加密,然后将加密数据截断为与输入数据相同的长度(丢弃加密文本末尾的填充数据).解密时,将填充(任何垃圾或零)附加到密文以与块长度对齐,设置 PaddingMode.None,解密,最后丢弃填充的字节(它们将是伪造的).像这样:

      Another option is to use System.Security.Cryptography.Aes from c# framework and encrypt using PaddingMode.Zeros, then truncate encrypted data to the same length as input data (discard padded data at the end of encrypted text). When decrypting, append padding (any garbage or zeros) to cipertext to align to the block length, set PaddingMode.None, decrypt, throw away padded bytes at the end (they will be bogus). Something like this:

              var aes2 = Aes.Create();
              aes2.KeySize = 128;
              aes2.BlockSize = 128;
              aes2.Mode = CipherMode.CFB;
              aes2.Padding = PaddingMode.Zeros;
              var aes2Data = new byte[buff.Length];
              // strip padded data
              Buffer.BlockCopy(aes2.CreateEncryptor().TransformFinalBlock(buff, 0, buff.Length), 0,
                  aes2Data, 0, buff.Length);
              Console.WriteLine("Aes2 size: {0}", aes2Data.Length);
      
      
              // validate aes2
              var size = aes2Data.Length;
              var pad = aes2Data.Length % 16;
              if (pad != 0)
              {
                  var tmp = new byte[aes2Data.Length + 16 - pad];
                  Buffer.BlockCopy(aes2Data, 0, tmp, 0, aes2Data.Length);
                  aes2Data = tmp;
              }
      
              var aes2Decr = Aes.Create();
              aes2Decr.Padding = PaddingMode.None;
              aes2Decr.Key = aes2.Key;
              aes2Decr.Mode = aes2.Mode;
              aes2Decr.IV = aes2.IV;
              var data2 = aes2Decr.CreateDecryptor().TransformFinalBlock(aes2Data, 0, aes2Data.Length);
              valid = data2.Take(size).SequenceEqual(buff);
              if (!valid)
                  throw new ApplicationException("Invalid data");
      

      这篇关于如何实现“AES/CFB/NoPadding"WinRT C# 中的加密/解密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆