在 C# 中加密/解密数据的更快方法是什么?

Him*_*har 2 c# encryption

我使用了以下函数,但解密 1100 条联系我们表格的记录需要 50 秒到 1 分钟。所以我过滤一些东西大约需要1分钟的时间。因为我需要加密整个数据,所以我在 C# 中触发过滤器查询。

这是加密方法:

public static string Encrypt(string encryptString)
{
    if (string.IsNullOrEmpty(encryptString))
        return encryptString;

    string EncryptionKey = "---";
    byte[] clearBytes = Encoding.Unicode.GetBytes(encryptString);
    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.CreateEncryptor(), CryptoStreamMode.Write))
            {
                cs.Write(clearBytes, 0, clearBytes.Length);
                cs.Close();
            }
            encryptString = Convert.ToBase64String(ms.ToArray());
        }
    }
    return encryptString;
}
Run Code Online (Sandbox Code Playgroud)

这是我的解密方法:

public static string Decrypt(string cipherText)
{
    if (string.IsNullOrEmpty(cipherText))
        return cipherText;

    string EncryptionKey = "---";
    cipherText = cipherText.Replace(" ", "+");
    byte[] cipherBytes = Convert.FromBase64String(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;
}
Run Code Online (Sandbox Code Playgroud)

Raw*_*aew 6

重构您的代码,解密和加密 1000 个项目只需不到一秒的时间。

var watch = new Stopwatch();
watch.Start();
using (var service = new Cryptography("---"))
{
    var listEncrypt = new List<string>();
    var listPlain = new List<string>();
    for (int i = 0; i < 1000; i++)
    {
        var encypt = service.Encrypt(i.ToString());
        listEncrypt.Add(encypt);
    }

    for (int i = 0; i < 1000; i++)
    {
        var plain = service.Decrypt(listEncrypt[i]);
        listPlain.Add(plain);
    }
}
watch.Stop();
Console.WriteLine(watch.Elapsed.Milliseconds.ToString());
Console.Read();
Run Code Online (Sandbox Code Playgroud)

这个想法是只创建一个加密器实例来加密/解密列表

public class Cryptography : IDisposable
{
    private Aes Encryptor;

    public Cryptography(string key)
    {
        Encryptor = Aes.Create();
        var pdb = new Rfc2898DeriveBytes(key, 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);
    }

    public string Encrypt(string plainText)
    {
        if (string.IsNullOrEmpty(plainText))
            return plainText;

        var clearBytes = Encoding.Unicode.GetBytes(plainText);

        using (var ms = new MemoryStream())
        {
            using (var cs = new CryptoStream(ms, Encryptor.CreateEncryptor(), CryptoStreamMode.Write))
            {
                cs.Write(clearBytes, 0, clearBytes.Length);
            }
            plainText = Convert.ToBase64String(ms.ToArray());
        }

        return plainText;
    }

    public string Decrypt(string cipherText)
    {
        if (string.IsNullOrEmpty(cipherText))
            return cipherText;

        cipherText = cipherText.Replace(" ", "+");
        var cipherBytes = Convert.FromBase64String(cipherText);

        using (var ms = new MemoryStream())
        {
            using (var cs = new CryptoStream(ms, Encryptor.CreateDecryptor(), CryptoStreamMode.Write))
            {
                cs.Write(cipherBytes, 0, cipherBytes.Length);
            }
            cipherText = Encoding.Unicode.GetString(ms.ToArray());
        }
        return cipherText;
    }

    public void Dispose()
    {
        Encryptor.Dispose();
    }
}
Run Code Online (Sandbox Code Playgroud)