ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

MSSQL 调用 CLR 加密/解密

2022-07-28 11:34:42  阅读:242  来源: 互联网

标签:SqlString text 解密 key new using byte MSSQL CLR


使用 Transact-SQL开启CLR

sp_configure 'show advanced options',1
RECONFIGURE

sp_configure 'clr enabled',1
RECONFIGURE

 

DECLARE @hash AS BINARY(64) = (SELECT HASHBYTES('SHA2_512', (SELECT * FROM OPENROWSET (BULK 'C:\clrDES.dll', SINGLE_BLOB) AS [Data])))

EXEC sp_add_trusted_assembly @hash

 

编写CLR集成托管DLL

using System;
using System.IO;
using System.Text;
using System.Data;
using System.Data.SqlTypes;
using System.Security.Cryptography;
using Microsoft.SqlServer.Server;

public class DES
{
    [SqlFunction(IsDeterministic = true, IsPrecise = true)]
    public static SqlString DESEncrypt(SqlString text, SqlString key)
    {
        if (text.IsNull || key.IsNull || key.Value.Length < 8)
            return null;
        return (SqlString)_DESEncrypt(Encoding.Default.GetBytes((string)text),
            Encoding.Default.GetBytes((string)key),
            new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF });
    }

    private static string _DESEncrypt(byte[] text, byte[] key, byte[] iv)
    {
        string entext;
        using (MemoryStream mstream = new MemoryStream())
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            CryptoStream estream = new CryptoStream(mstream, des.CreateEncryptor(key, iv), CryptoStreamMode.Write);
            try
            {
                estream.Write(text, 0, text.Length);
                estream.FlushFinalBlock();
                entext = Convert.ToBase64String(mstream.ToArray());
            }
            finally
            {
                estream.Close();
                des.Clear();
            }
        }
        return entext;
    }

    [SqlFunction(IsDeterministic = true, IsPrecise = true)]
    public static SqlString DESDecrypt(SqlString text, SqlString key)
    {
        if (text.IsNull || key.IsNull || key.Value.Length < 8)
            return null;
        return (SqlString)_DESDecrypt(Convert.FromBase64String((string)text),
            Encoding.Default.GetBytes((string)key),
            new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF });
    }

    private static string _DESDecrypt(byte[] text, byte[] key, byte[] iv)
    {
        string detext;
        using (MemoryStream mstream = new MemoryStream())
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            CryptoStream estream = new CryptoStream(mstream, des.CreateDecryptor(key, iv), CryptoStreamMode.Write);
            try
            {
                estream.Write(text, 0, text.Length);
                estream.FlushFinalBlock();
                detext = Encoding.Default.GetString(mstream.ToArray());
            }
            finally
            {
                estream.Close();
                des.Clear();
            }
        }
        return detext;
    }
}

 

调用示例

select dbo.des_encrypt(N'hello world', N'88888888');
select dbo.des_decrypt(N'+GeLDT6kAxZlm2pnFX8X4w==',N'88888888');

 

标签:SqlString,text,解密,key,new,using,byte,MSSQL,CLR
来源: https://www.cnblogs.com/microsoft-zh/p/16527972.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有