【问题标题】:aspnet core encrypting and decrypting a stringaspnet核心加密和解密字符串
【发布时间】:2017-05-01 22:05:07
【问题描述】:

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

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;

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

【问题讨论】:

  • 消息是怎么加密的,什么语言和实现如PHP mcrypt
  • [@w2olves] 你解决了吗?
  • @Eris 这不是重复的 1) 这是 AES,那不是。 2)如果我们定义密钥长度为 15,那么我们得到了密钥大小异常,但是 16 我们得到了其他异常(这个填充的东西,问题是关于什么的)
  • 通过更改cipher 的值并更改此链接中提到的第二个Buffer.BlockCopy 语句可解决此问题:stackoverflow.com/a/46541503/4745542

标签: c# encryption asp.net-core


【解决方案1】:

需要进行两次更改

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

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

【讨论】:

    【解决方案2】:
    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;
            }
    

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

    【讨论】:

    • 这是一段复制粘贴到整个网络和本网站的代码块。提及您在哪里找到它,以及为什么它是执行 OP 想要的一个不错的选择。
    • @CodeCaster 我正在使用此代码。我没有从任何地方复制。我从多个地方添加此代码。
    猜你喜欢
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    • 2018-08-26
    相关资源
    最近更新 更多