指定的填充模式对此算法无效 - c# - System.Security.Cryptography

时间:2017-10-02 20:43:12

标签: c# security encryption cryptography system

对c#来说很新,目前在解密长密码时遇到问题,错误为

  

指定密钥不是此算法的有效大小

我知道这与加密的密码位长度不相关,但不确定如何建议允许这些较长密码的方法。

这是我的加密和解密

  

" cipherKey":" 0123456789abcdef"," cipherVector":   " somereallycooliv"

using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;

namespace DataApi
{
public class Encryption
{
    private readonly IConfigurationService _configService;


    private const string _vector = "cipherVector";
    private const string _key = "cipherKey";

    public Encryption(IConfigurationService configService)
    {
        _configService = configService;
    }
    public string EncryptString(string text)
    {
        if(string.IsNullOrEmpty(text))
        {
            return "";
        }
        try
        {

      var key = Encoding.UTF8.GetBytes(_configService.Get(_key));
        byte[] IV = Encoding.ASCII.GetBytes(_configService.Get(_vector));

        using (var aesAlg = Aes.Create())
        {
            using (var encryptor = aesAlg.CreateEncryptor(key, IV))
            {
                using (var msEncrypt = new MemoryStream())
                {
                    using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    using (var swEncrypt = new StreamWriter(csEncrypt))
                    {
                        swEncrypt.Write(text);
                    }

                    var decryptedContent = msEncrypt.ToArray();

                    var result = new byte[IV.Length + decryptedContent.Length];

                    Buffer.BlockCopy(IV, 0, result, 0, IV.Length);
                    Buffer.BlockCopy(decryptedContent, 0, result, IV.Length, decryptedContent.Length);

                    return Convert.ToBase64String(result);
                }
            }
        }
        }
        catch(Exception e) {
             Loggifer.Error("Unable to encrypt string: "+text , e );

            throw e;
        }
    }

    public string DecryptString(string cipherText)
    {
        if(string.IsNullOrEmpty(cipherText))
        {
            return "";
        }
        try
        {
            var fullCipher = Convert.FromBase64String(cipherText);

            byte[] IV = Encoding.ASCII.GetBytes(_configService.Get(_vector));
            var cipher = new byte[16];

            Buffer.BlockCopy(fullCipher, 0, IV, 0, IV.Length);
            Buffer.BlockCopy(fullCipher, IV.Length, cipher, 0, IV.Length);
            var key = Encoding.UTF8.GetBytes(_configService.Get(_key));

            using (var aesAlg = Aes.Create())
            {
                using (var decryptor = aesAlg.CreateDecryptor(key, IV))
                {
                    string result;
                    using (var msDecrypt = new MemoryStream(cipher))
                    {
                        using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (var srDecrypt = new StreamReader(csDecrypt))
                            {
                                result = srDecrypt.ReadToEnd();
                            }
                        }
                    }

                    return result;
                }
            }
        }
        catch (Exception e)
        {
            Loggifer.Error("Unable to decrypt string: "+cipherText , e );
            throw e;
        }
    }

}
}

1 个答案:

答案 0 :(得分:18)

函数public string DecryptString(string cipherText)

需要进行两项更改
 var cipher = new byte[fullCipher.Length - IV.Length];

 Buffer.BlockCopy(fullCipher, IV.Length, cipher, 0, fullCipher.Length - IV.Length);