【问题标题】:C# Encryption InformationC# 加密信息
【发布时间】:2022-01-07 05:54:05
【问题描述】:

我们正在使用以下代码来加密/解密文本以将一些敏感信息存储到我们的数据库中。

public static string Encrypt(string inputText)
{
    const string ENCRYPTION_KEY = "MY_KEY";
    byte[] SALT = Encoding.ASCII.GetBytes(ENCRYPTION_KEY.Length.ToString());

    System.Security.Cryptography.RijndaelManaged rijndaelCipher = null;
    byte[] plainText = null;
    System.Security.Cryptography.PasswordDeriveBytes SecretKey = null;

    try
    {
        rijndaelCipher = new System.Security.Cryptography.RijndaelManaged();
        plainText = Encoding.Unicode.GetBytes(inputText);
        SecretKey = new System.Security.Cryptography.PasswordDeriveBytes(ENCRYPTION_KEY, SALT);

        using (System.Security.Cryptography.ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16)))
        {
            using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
            {
                using (System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, encryptor, System.Security.Cryptography.CryptoStreamMode.Write))
                {
                    cryptoStream.Write(plainText, 0, plainText.Length);
                    cryptoStream.FlushFinalBlock();
                    return Convert.ToBase64String(memoryStream.ToArray());
                }
            }
        }
    }
    catch
    {
        throw;
    }
    finally
    {
        rijndaelCipher = null;
        plainText = null;
        plainText = null;
    }
}

public static string Decrypt(string inputText)
{
    string ENCRYPTION_KEY = "MY_KEY";
    byte[] SALT = Encoding.ASCII.GetBytes(ENCRYPTION_KEY.Length.ToString());

    System.Security.Cryptography.RijndaelManaged rijndaelCipher = null;
    byte[] encryptedData = null;
    byte[] plainText = null;

    try
    {
        rijndaelCipher = new System.Security.Cryptography.RijndaelManaged();
        encryptedData = Convert.FromBase64String(inputText);
        System.Security.Cryptography.PasswordDeriveBytes secretKey = new System.Security.Cryptography.PasswordDeriveBytes(ENCRYPTION_KEY, SALT);

        using (System.Security.Cryptography.ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16)))
        {
            using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(encryptedData))
            {
                using (System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, decryptor, System.Security.Cryptography.CryptoStreamMode.Read))
                {
                    plainText = new byte[encryptedData.Length];
                    int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
                    return Encoding.Unicode.GetString(plainText, 0, decryptedCount);
                }
            }
        }
    }
    catch
    {
        return "";
    }
    finally
    {
        rijndaelCipher = null;
        encryptedData = null;
        plainText = null;
    }
}

我不是编写此代码的原始开发人员,我需要编写一些与安全性相关的文档,因此想知道上述算法的确切名称。有人可以告诉我上述加密/解密文本的方法的确切名称是什么。如MD5、SHA256、AES等

我用谷歌搜索了很多,但无法找到正确的可靠答案。

谢谢。

【问题讨论】:

  • MD5 和 SHA256 不是加密算法。它们是散列算法。不同之处在于没有办法“解密”哈希。

标签: c# encryption cryptography md5 sha


【解决方案1】:

Rijndael 是赢得 AES 竞争的算法,但仅适用于具有 128 位 BlockSize 的版本。 Microsoft doc 声明 RijndaelManaged 类的默认值为 128,因此此代码使用带有 PKCS7 填充的 AES-256-CBC(密钥为 32 字节且未指定模式)。

但是,这段代码非常不安全:您应该使用 GCM 或 CBC/CTR 加校验和等模式,并且密钥永远不应从简单的硬编码 ascii 字符串派生,无论它有多长或多复杂,盐是它的简单副本。最后,IV 应该是随机的并沿密文保存,而不是从密钥派生,否则 ECB 模式常见的攻击也可以在这里应用。

PS:RijndaelManaged 被标记为已过时,应使用AesAesCryptoServiceProvider

【讨论】:

    猜你喜欢
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多