存储过程加密解密

Use master
Go
if object_ID(‘[sp_EncryptObject]‘) is not null
    Drop Procedure [sp_EncryptObject]
Go
create procedure sp_EncryptObject
(
    @Object sysname=‘All‘
)
as
/*
    当@Object=All的时候,对所有的函数,存储过程,视图和触发器进行加密
    调用方法:
    1. Execute sp_EncryptObject ‘All‘
    2. Execute sp_EncryptObject ‘ObjectName‘
*/
begin
    set nocount on

    if @Object <>‘All‘
    begin
        if not exists(select 1 from sys.objects a where a.object_id=object_id(@Object) And a.type in(‘P‘,‘V‘,‘TR‘,‘FN‘,‘IF‘,‘TF‘))
        begin
            --SQL Server 2008
            raiserror 50001 N‘无效的加密对象!加密对象必须是函数,存储过程,视图或触发器。‘

            --SQL Server 2012
            --throw 50001, N‘无效的加密对象!加密对象必须是函数,存储过程,视图或触发器。‘,1  

            return
        end

        if exists(select 1 from sys.sql_modules a where a.object_id=object_id(@Object) and a.definition is null)
        begin
            --SQL Server 2008
            raiserror 50001 N‘对象已经加密!‘

            --SQL Server 2012
            --throw 50001, N‘对象已经加密!‘,1
            return
        end
    end

    declare @sql nvarchar(max),@C1 nchar(1),@C2 nchar(1),@type nvarchar(50),@Replace nvarchar(50)
    set @C1=nchar(13)
    set @C2=nchar(10)

    declare cur_Object
        cursor for
            select object_name(a.object_id) As ObjectName,a.definition
                from sys.sql_modules a
                    inner join sys.objects b on b.object_id=a.object_id
                        and b.is_ms_shipped=0
                        and not exists(select 1
                                            from sys.extended_properties x
                                            where x.major_id=b.object_id
                                                and x.minor_id=0
                                                and x.class=1
                                                and x.name=‘microsoft_database_tools_support‘
                                        )
                where b.type in(‘P‘,‘V‘,‘TR‘,‘FN‘,‘IF‘,‘TF‘)
                    and (b.name=@Object or @Object=‘All‘)
                    and b.name <>‘sp_EncryptObject‘
                    and a.definition is not null
                order by Case
                            when b.type =‘V‘ then 1
                            when b.type =‘TR‘ then 2
                            when b.type in(‘FN‘,‘IF‘,‘TF‘) then 3
                            else 4 end,b.create_date,b.object_id

    open cur_Object
    fetch next from cur_Object into @Object,@sql
    while @@fetch_status=0
    begin

        Begin Try

            if objectproperty(object_id(@Object),‘ExecIsAfterTrigger‘)=0 set @Replace=‘As‘ ; else set @Replace=‘For ‘;

            if (patindex(‘%‘+@C1+@C2+@Replace+@C1+@C2+‘%‘,@sql)>0)
            begin
                set @sql=Replace(@sql,@C1+@C2+@Replace+@C1+@C2,@C1+@C2+‘With Encryption‘+@C1+@C2+@Replace+@C1+@C2)
            end
            else if(patindex(‘%‘+@C1+@Replace+@C1+‘%‘,@sql)>0)
            begin
                set @sql=Replace(@sql,@C1+@Replace+@C1,@C1+‘With Encryption‘+@C1+@Replace+@C1)
            end
            else if(patindex(‘%‘+@C2+@Replace+@C2+‘%‘,@sql)>0)
            begin
                set @sql=Replace(@sql,@C2+@Replace+@C2,@C2+‘With Encryption‘+@C2+@Replace+@C2)
            end
            else if(patindex(‘%‘+@C2+@Replace+@C1+‘%‘,@sql)>0)
            begin
                set @sql=Replace(@sql,@C2+@Replace+@C1,@C1+‘With Encryption‘+@C2+@Replace+@C1)
            end
            else if(patindex(‘%‘+@C1+@C2+@Replace+‘%‘,@sql)>0)
            begin
                set @sql=Replace(@sql,@C1+@C2+@Replace,@C1+@C2+‘With Encryption‘+@C1+@C2+@Replace)
            end
            else if(patindex(‘%‘+@C1+@Replace+‘%‘,@sql)>0)
            begin
                set @sql=Replace(@sql,@C1+@Replace,@C1+‘With Encryption‘+@C1+@Replace)
            end
            else if(patindex(‘%‘+@C2+@Replace+‘%‘,@sql)>0)
            begin
                set @sql=Replace(@sql,@C2+@Replace,@C2+‘With Encryption‘+@C2+@Replace)
            end

            set @type =
                case
                    when object_id(@Object,‘P‘)>0 then ‘Proc‘
                    when object_id(@Object,‘V‘)>0 then ‘View‘
                    when object_id(@Object,‘TR‘)>0  then ‘Trigger‘
                    when object_id(@Object,‘FN‘)>0 or object_id(@Object,‘IF‘)>0 or object_id(@Object,‘TF‘)>0 then ‘Function‘
                end
            set @sql=Replace(@sql,‘Create ‘+@type,‘Alter ‘+@type)

            Begin Transaction
            exec(@sql)
            print N‘已完成加密对象(‘+@type+‘):‘+@Object
            Commit Transaction

        End Try
        Begin Catch
            Declare @Error nvarchar(2047)
            Set @Error=‘Object: ‘+@Object+@C1+@C2+‘Error: ‘+Error_message()

            Rollback Transaction
            print @Error
            print @sql
        End Catch

        fetch next from cur_Object into @Object,@sql

    end

    close cur_Object
    deallocate cur_Object
end

Go
exec sp_ms_marksystemobject ‘sp_EncryptObject‘ --标识为系统对象
go
Use master
Go
if object_ID(‘[sp_DecryptObject]‘) is not null
    Drop Procedure [sp_DecryptObject]
Go
create procedure sp_DecryptObject
(
    @Object sysname,    --要解密的对象名:函数,存储过程,视图或触发器
    @MaxLength int=4000 --评估内容的长度
)
as
set nocount on
/* 1. 解密 */

if not exists(select 1 from sys.objects a where a.object_id=object_id(@Object) And a.type in(‘P‘,‘V‘,‘TR‘,‘FN‘,‘IF‘,‘TF‘))
begin
    --SQL Server 2008
    raiserror 50001 N‘无效的对象!要解密的对象必须是函数,存储过程,视图或触发器。‘ 

    --SQL Server 2012
    --throw 50001, N‘无效的对象!要解密的对象必须是函数,存储过程,视图或触发器。‘,1
    return
end

if exists(select 1 from sys.sql_modules a where a.object_id=object_id(@Object) and a.definition is not null)
begin
    --SQL Server 2008
    raiserror 50001 N‘对象没有加密!‘ 

    --SQL Server 2012
    --throw 50001, N‘无效的对象!要解密的对象必须是函数,存储过程,视图或触发器。‘,1
    return
end

declare  @sql nvarchar(max)                --解密出来的SQL语句
        ,@imageval nvarchar(max)        --加密字符串
        ,@tmpStr nvarchar(max)            --临时SQL语句
        ,@tmpStr_imageval nvarchar(max) --临时SQL语句(加密后)
        ,@type char(2)                    --对象类型(‘P‘,‘V‘,‘TR‘,‘FN‘,‘IF‘,‘TF‘)
        ,@objectID int                    --对象ID
        ,@i int                            --While循环使用
        ,@Oject1 nvarchar(1000)

set @objectID=object_id(@Object)
set @type=(select a.type from sys.objects a where a.object_id=@objectID)

declare @Space4000 nchar(4000)
set @Space4000=replicate(‘-‘,4000)

/*
@tmpStr 会构造下面的SQL语句
-------------------------------------------------------------------------------
alter trigger Tr_Name on Table_Name with encryption for update as return /**/
alter proc Proc_Name with encryption  as select 1 as col /**/
alter view View_Name with encryption as select 1 as col /**/
alter function Fn_Name() returns int with encryption as begin return(0) end/**/
*/
set @Oject1=quotename(object_schema_name(@objectID))+‘.‘+quotename(@Object)
set @tmpStr=
        case
            when @type =‘P ‘ then N‘Alter Procedure ‘+@Oject1+‘ with encryption as select 1 as column1 ‘
            when @type =‘V ‘ then N‘Alter View ‘+@Oject1+‘ with encryption as select 1 as column1 ‘
            when @type =‘FN‘ then N‘Alter Function ‘+@Oject1+‘() returns int with encryption as begin return(0) end ‘
            when @type =‘IF‘ then N‘Alter Function ‘+@Oject1+‘() returns table with encryption as return(Select a.name from sys.types a) ‘
            when @type =‘TF‘ then N‘Alter Function ‘+@Oject1+‘() returns @t table(name nvarchar(50)) with encryption as begin return end ‘
            else ‘Alter Trigger ‘+@Oject1+‘on ‘+quotename(object_schema_name(@objectID))+‘.‘+(select Top(1) quotename(object_name(parent_id)) from sys.triggers a where a.object_id=@objectID)+‘ with encryption for update as return ‘
        end        

set @tmpStr=@tmpStr+‘/*‘+@Space4000
set @i=0
while @i < (ceiling(@MaxLength*1.0/4000)-1)
begin
    set @tmpStr=@tmpStr+ @Space4000
    Set @i=@i+1
end
set @tmpStr=@tmpStr+‘*/‘

------------
set @imageval =(select top(1) a.imageval from sys.sysobjvalues a where a.objid=@objectID and a.valclass=1)

begin tran
exec(@tmpStr)
set @tmpStr_imageval =(select top(1) a.imageval from sys.sysobjvalues a where a.objid=@objectID and a.valclass=1)

rollback tran

-------------
set @tmpStr=stuff(@tmpStr,1,5,‘create‘)
set @sql=‘‘
set @i=1
while @i<= (datalength(@imageval)/2)
begin
    set @sql=@sql+isnull(nchar(unicode(substring(@tmpStr,@i,1)) ^ unicode(substring(@tmpStr_imageval,@i,1))^unicode(substring(@imageval,@i,1)) ),‘‘)
    Set @i+=1
end

/* 2. 列印 */

declare @patindex int
while @sql>‘‘
begin

    set @patindex=patindex(‘%‘+char(13)+char(10)+‘%‘,@sql)
    if @patindex >0
    begin
        print substring(@sql,1,@patindex-1)
        set @sql=stuff(@sql,1,@patindex+1,‘‘)
    end
    else
    begin
        set @patindex=patindex(‘%‘+char(13)+‘%‘,@sql)
        if @patindex >0
        begin
            print substring(@sql,1,@patindex-1)
            set @sql=stuff(@sql,1,@patindex,‘‘)
        end
        else
        begin
            set @patindex=patindex(‘%‘+char(10)+‘%‘,@sql)
            if @patindex >0
            begin
                print substring(@sql,1,@patindex-1)
                set @sql=stuff(@sql,1,@patindex,‘‘)
            end
            else
            begin
                print @sql
                set @sql=‘‘
            end
        end
    end

end

Go
exec sp_ms_marksystemobject ‘sp_DecryptObject‘ --标识为系统对象
go
时间: 2024-10-10 17:34:28

存储过程加密解密的相关文章

SqlServer存储过程加密与解密

★ 加密存储过程 ★: IF EXISTS (SELECT name FROM sysobjects WHERE name = 'encrypt_this' AND type = 'P')   DROP PROCEDURE encrypt_this GO USE pubs GO CREATE PROCEDURE encrypt_this WITH ENCRYPTION  ---添加WITH ENCRYPTION即可 AS SELECT * FROM authorsGO  --查看存储过程文本: 

AJAX+REA实现前后台数据交互的加密解密

AJAX+REA实现前后台数据交互的加密解密 1.创建js文件Encryption.js /**  * 加密解密  */ /** RSA加密用 生成key */ function bodyRSA(){ /** 1024位的key参数写130,2014位的key参数写260 */ setMaxDigits(130); /** ajax 调用后台方法,取回公钥 */ var keyR ;     $.ajax({      url: "/GHGL/Key/pk",//请求后台的url,本例

加密解密过程

1,首先来说说有关加密解密有关的信息 信息安全标准NIST(National Institute of Standards and Technology)美国国家标准与技术研究院 openssl有CIA C:保密性: 数据保密性 隐私性 A:完整性: 数据完整性 系统完整性 I:可用性 真实性:一个实体是真实的,可被验证的. 可追溯性:一旦被攻击,能够追溯攻击源在哪 2,OSI规定的X.800 1)安全攻击: 被动攻击:窃听 主动攻击:1,伪装 2,重播 3,消息修改 4,拒绝攻击等 2)安全服

加密解密

在现代密码学中,加密方法大致可分为对称密钥加密(对称加密)和公开密钥加密(非对称加密). 一. 对称加密(Symmetric-key algorithm,或对等加密: Reciprocal cipher ) 对称加密,即加密和解密使用同一个密钥,或者知道一方密钥能够轻易计算出另一方密钥.其解密(decryption)算法等同于加密算法,也就是说,要还原对等加密的密文,套用加密同样的算法即可得到明文. 对称加密的速度比非对称加密快很多,在很多场合都需要对称加密. 对称加密又可分为分组密码(分组加密

C/C++使用openssl进行摘要和加密解密(md5, sha256, des, rsa)

openssl里面有很多用于摘要哈希.加密解密的算法,方便集成于工程项目,被广泛应用于网络报文中的安全传输和认证.下面以md5,sha256,des,rsa几个典型的api简单使用作为例子. 算法介绍 md5:https://en.wikipedia.org/wiki/MD5 sha256:https://en.wikipedia.org/wiki/SHA-2 des: https://en.wikipedia.org/wiki/Data_Encryption_Standard rsa: htt

c#中base64加密解密

using System; using System.Text; namespace Common { /// <summary> /// 实现Base64加密解密 /// </summary> public sealed class Base64 { /// <summary> /// Base64加密 /// </summary> /// <param name="codeName">加密采用的编码方式</param

C#DES加密解密字符串

1.添加引用 using System.Security.Cryptography; using System.IO; 2.添加默认密匙向量 //默认密钥向量 private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; 3.添加加密解密类 /// <summary> /// DES加密字符串 /// </summary> /// <param name="encry

Discuz论坛写出的php加密解密处理类(代码+使用方法)

PHP加密解密也是常有的事,最近在弄相关的东西,发现discuz论坛里的PHP加密解密处理类代码,感觉挺不错,在用的时候,要参考Discuz论坛的passport相关函数,后面我会附上使用方法,先把类代码帖上来: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 <?php /*

C#加密解密(DES,AES,Base64,md5,SHA256,RSA,RC4)

一:异或^简单加解密(数字类型) 1:原理: 异或用于比较两个二进制数的相应位,在执行按位"异或"运算时,如果两个二进制数的相应位都为1或者都为0,则返回0;如果两个二进制数的相应位其中一个为1另一个为0,则返回1. //对数字加密 int P_int_Num, P_int_Key;//定义两个值类型变量 string Encryptstr = (P_int_Num ^ P_int_Key).ToString();//加密数值 //对数字解密 int P_int_Key, P_int_