aspnet core加密和解密字符串

时间:2017-05-01 22:05:07

标签: c# encryption asp.net-core

我正在尝试使用一些非常标准的算法来解密字符串。

public static string DecryptString(string cipherText)
{
    string keyString = string.Empty;

    // Check whether the environment variable exists.
    keyString = Environment.GetEnvironmentVariable("EncryptKey");

    if (keyString == null)
    {
        keyString = "E546C8DF278CD5931069B522E695D4F2";
    }

    var fullCipher = Convert.FromBase64String(cipherText);
    using (var aesAlg = Aes.Create())
    {
        byte[] iv = new byte[aesAlg.BlockSize / 8];
        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(keyString);

        string result;
        using (var decryptor = aesAlg.CreateDecryptor(key, iv))
        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;
    }
}

我一直收到错误:

  

System.Security.Cryptography.CryptographicException:指定的填充模式对此算法无效。

我尝试过多种方式

var iv = new byte[16];
var cipher = new byte[16];

var iv = aesAlg.IV;

此时我仍然收到错误。我做错了什么?

2 个答案:

答案 0 :(得分:0)

需要两项更改

                let u = d3.select('#el')
                .selectAll('path')
                .data(features);

                u.enter()
                .append('path')
                .attr('d', geoGenerator)

var cipher = new byte[fullCipher.Length - iv.Length];

答案 1 :(得分:-1)

public static string Decrypt(string cipherText)
  {
     string EncryptionKey  = string.Empty;

    // Check whether the environment variable exists.
    EncryptionKey = Environment.GetEnvironmentVariable("EncryptKey");

    if (EncryptionKey == null)
    {
        EncryptionKey = "E546C8DF278CD5931069B522E695D4F2";
    }
            byte[] cipherBytes;
            try
            {
                cipherBytes = Convert.FromBase64String(cipherText);
            }
            catch
            {
                return cipherText;
            }
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(cipherBytes, 0, cipherBytes.Length);
                        cs.Close();
                    }
                    cipherText = Encoding.Unicode.GetString(ms.ToArray());
                }
            }
            return cipherText;
        }

注意:-,您需要提供与用于加密字符串相同的密钥。