尝试使用.net核心中的BouncyCastle解密256个块大小的字符串。获取无效的填充计数错误

时间:2019-01-21 17:21:17

标签: encryption .net-core bouncycastle rijndael rijndaelmanaged

我正在尝试使用.net核心中的Bouncycastle解密256个块大小的字符串,并使用Rijndael在.net框架中对其进行加密。

我按照https://github.com/dotnet/corefx/issues/12064代码进行了解密。

加密代码

public string Encrypt(string clearText, string key)
{
   var salt = GetRandomBytes(16);
   var plainTextBytes = Encoding.UTF8.GetBytes(clearText);

  using (var rijAlg = InitRijndael(key, salt))
  {
  using (var memoryStream = new MemoryStream())
  {
   using (var cryptoStream = new CryptoStream(memoryStream, rijAlg.CreateEncryptor(), CryptoStreamMode.Write))
   {

          cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
   }

              return Convert.ToBase64String(salt) + ":" +                      
              Convert.ToBase64String(rijAlg.IV) + ":" +
              Convert.ToBase64String(memoryStream.ToArray()) + 
              ":" + HashPassword(clearText, salt, 11);
      }
 }
}   

private static Rijndael InitRijndael(string key, byte[] salt, byte[] iv = null)
    {
        //var salt = new byte[] { 226, 201, 66, 187, 1, 7, 255, 149, 161, 67 };
        var algorithm = Rijndael.Create();
        if (iv != null)
        {
                            //Decryption
            algorithm.BlockSize = iv.Length * 8;
            algorithm.IV = iv;
        }
        else
        {
                            //Encryption
            algorithm.BlockSize = 256; // 256-bit blocks
            algorithm.GenerateIV();
        }

        try
        {
            using (var rfc2898DeriveBytes = new Rfc2898DeriveBytes(key, salt))
            {
                algorithm.Key = rfc2898DeriveBytes.GetBytes(32); // 256-bit

            }
        }
        catch
        {
            algorithm.Dispose();
            throw;
        }

        return algorithm;
    }

尝试在.net核心中解密的解密代码:

public string Decrypt(string cipherText, string Key)
    {

        var tokens = cipherText.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
        if (tokens.Length != 4)
        {
            throw new ArgumentException("Invalid Cipher Text", "cipherText");
        }

        var salt = Convert.FromBase64String(tokens[0]);
        var iv = Convert.FromBase64String(tokens[1]);
        var bytes = Convert.FromBase64String(tokens[2]);
        var bytesHash = tokens[3];

        using (var password = new Rfc2898DeriveBytes(Key, salt))
        {
            var keyBytes = password.GetBytes(Keysize / 8);
            var engine = new RijndaelEngine(256);
            var blockCipher = new CbcBlockCipher(engine);
            var cipher = new PaddedBufferedBlockCipher(blockCipher, new Pkcs7Padding());
            var keyParam = new KeyParameter(keyBytes);
            var keyParamWithIV = new ParametersWithIV(keyParam, iv, 0, 32);

            cipher.Init(false, keyParamWithIV);
            var comparisonBytes = new byte[cipher.GetOutputSize(bytes.Length)];
            var length = cipher.ProcessBytes(bytes, comparisonBytes, 0);
            var finalBytes = cipher.DoFinal(comparisonBytes, 0, length);
            var final = Encoding.UTF8.GetString(finalBytes);
        }
}

我在执行代码Chipper.DoFinal时收到无效的PadCount错误。

0 个答案:

没有答案