Encryption 如何从C#加密和解密SQL Server 2008 r2中的数据?

Encryption 如何从C#加密和解密SQL Server 2008 r2中的数据?,encryption,sql-server-2008-r2,cryptography,aes,Encryption,Sql Server 2008 R2,Cryptography,Aes,嗨,我能够对指定的字符串进行加密和解密,并保存到数据库中。但我想搜索这些数据,但不可能在数据中搜索加密列。这是有办法的。前面我使用的是AES,下面的方法用于加密 private string Encrypt(string Text) { string EncryptionKey = "abcdef@123"; byte[] clearBytes = Encoding.Unicode.GetBytes(Text); using (Aes

嗨,我能够对指定的字符串进行加密和解密,并保存到数据库中。但我想搜索这些数据,但不可能在数据中搜索加密列。这是有办法的。前面我使用的是AES,下面的方法用于加密

  private string Encrypt(string Text)
    {
        string EncryptionKey = "abcdef@123";
        byte[] clearBytes = Encoding.Unicode.GetBytes(Text);
        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();
                }
                Text = Convert.ToBase64String(ms.ToArray());
            }
        }
        return Text;
    }
下面的方法用于解密

     private string Decrypt(string Text)
    {
        string EncryptionKey = "abcdef@123";
        byte[] cipherBytes = Convert.FromBase64String(Text);
        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();
                }
                Text = Encoding.Unicode.GetString(ms.ToArray());
            }
        }
        return Text;
    }

有人能帮我吗?,

我不相信有,这是加密的一部分

如果字符串具有搜索的某些特征,例如嵌入其中的名称,则可以计算名称的SOUNDEX值并将其存储在单独的列中。然后,您可以基于名称的soundex值进行搜索,这将减少返回的行数,然后对每个行进行解密和搜索。至少你不用把整张桌子都拉下来。 当然,这样做可能是一个安全漏洞


干杯-

我认为,您需要创建SQL CLR数据库项目,并使用

解密逻辑和SQL查询中的调用。您将能够搜索加密数据