AES encrypt in c# decrypt in T-SQL

孤人 提交于 2019-12-23 01:34:12

问题


I have written the following code to decrypt some sensitive data, in most of the cases i need to query data using T-SQL where i can't decrypt the the data that is encrypted by this code. so my question is this how can i write a function in T-SQL that work the same way as like it work in C#, I will consume that in Stored procedures.

thanks in Advance

Encryption Function:

public static string Encrypt(string text)
    {
        if (string.IsNullOrEmpty(EncryptionKey))
            return string.Empty;
        if (string.IsNullOrEmpty(text))
            return string.Empty;
        var clearBytes = Encoding.Unicode.GetBytes(text);
        using (var encryptor = Aes.Create())
        {
            var 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 (var ms = new MemoryStream())
            {
                using (var cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(clearBytes, 0, clearBytes.Length);
                }
                text = Convert.ToBase64String(ms.ToArray());
            }
        }
        return text;
    }

Decryption Function:

public static string Decrypt(string text)
    {
        if (string.IsNullOrEmpty(EncryptionKey))
            return string.Empty;
        if (string.IsNullOrEmpty(text))
            return string.Empty;
        var cipherBytes = Convert.FromBase64String(text);
        using (var encryptor = Aes.Create())
        {
            var 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 (var ms = new MemoryStream())
            {
                using (var cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(cipherBytes, 0, cipherBytes.Length);
                }
                text = Encoding.Unicode.GetString(ms.ToArray());
            }
        }
        return text;
    }

回答1:


You can create a CLR UDF in SQL Server.

Refer following links for more info:

http://blog.sqlauthority.com/2008/10/19/sql-server-introduction-to-clr-simple-example-of-clr-stored-procedure/

https://msdn.microsoft.com/en-us/library/w2kae45k(v=vs.90).aspx



来源:https://stackoverflow.com/questions/29904035/aes-encrypt-in-c-sharp-decrypt-in-t-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!