加密/解密适用于Aes Zero填充但不适用于pkcs7 C#

时间:2017-05-25 08:51:24

标签: c# windows encryption cryptography aes

我正在尝试在UWP应用程序中加密我的数据,然后将其发送到Windows服务,在那里它将被解密。 Data是一个xml字符串,我用UTF8编码,关键是SHA256Hashed字符串。这里使用的IV是 -

  private static byte[] IV = new byte[] { 0x7b, 0x70, 0xb8, 0xf4, 0x85, 0xa7, 0x61, 0x68, 0x44, 0xff, 0xe4, 0xc8, 0x95, 0xcb, 0xbd, 0xea};

以下是UWP应用程序的加密代码。

public static byte[] EncryptDatawithAesCbcPkcs7(string data, byte[] key,byte[] iv)
{
    using (var aes = Aes.Create())
    {
        aes.Padding = PaddingMode.PKCS7;
        aes.BlockSize = 128;
        aes.KeySize = 256;
        aes.Mode = CipherMode.CBC;
        aes.IV = iv == null ? IV : iv;
        aes.Key = key;

        var dataBytes = Encoding.UTF8.GetBytes(data);
        var encryptor = aes.CreateEncryptor();

        try
        {
            byte[] buf = encryptor.TransformFinalBlock(Encoding.UTF8.GetBytes(data), 0, data.Length);
            return buf;
        }
        catch (Exception ex)
        {
            logger.Exception(ex, "error while encrypting");
            return null;
        }
    }
}

这里的数据是xml - >

<Request>
  <id>98</id>
  <callerId>d8ef93cc-03f9-45ef-ba13-b6546ce79792_h2zj8xccfsc8c</callerId>
  <ts>25-05-2017 09:06:40</ts>
</Request>

调用方法是 -

var secKeyWithKeyLength32 = Getsha256Key("A7R0OFOz91iv6heOWh");
    var requestData = CryptoHelper.EncryptDatawithAesCbcPkcs7(request.XmlString, secKeyWithKeyLength32, null);

密钥的SHA256来自此方法 -

    private byte[] Getsha256Key(string str)
    {
        using(var sha256 = SHA256.Create())
        {
            return sha256.ComputeHash(Encoding.UTF8.GetBytes(str));
        }
    }

对于解密,在Windows服务上,我使用下面的代码。

    public static string DecryptDataWithAesCbcPkcs7(byte[] data, byte[] key, byte[] iv = null)
    {
        if (null == data || data.Length == 0 || null == key || key.Length != 32)
        {
            _logger.Error2(_logComponent, "DecryptData needs a valid string and key");
            return null;
        }

        using (AesManaged aes = new AesManaged())
        {
            aes.Padding = PaddingMode.PKCS7;
            aes.BlockSize = 128;
            aes.KeySize = 256;
            aes.Mode = System.Security.Cryptography.CipherMode.CBC;
            aes.IV = iv == null ? IV : iv;
            aes.Key = key;

            try
            {
                // Create a decrytor to perform the stream transform.
                using (var decryptor = aes.CreateDecryptor())
                {
                    byte[] buf = decryptor.TransformFinalBlock(data, 0, data.Length);
                    return Encoding.UTF8.GetString(buf);
                }
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                 _logger.Exception2(_logComponent, ex, ex.Message);
                 return null;
            }
        }
    }

在此处调用代码 -

 byte[] buffer = new byte[MAX_LENGTH];
                    _secureServer.Read(buffer, 0, buffer.Count());

                    var sha256Key = Getsha256Key("A7R0OFOz91iv6heOWh");
                    var requestData = CryptoHelper.DecryptDataWithAesCbcPkcs7(buffer, sha256Key, null);

_secureServer这里是一个NamedPipeStream Getsha256Key与上面的代码相同

这会抛出错误 - “填充无效且无法删除”。相同的代码,填充零工作。我用pkcs7尝试了很多东西,但没有一个正在工作。

0 个答案:

没有答案