SQLserver儲存過程加密 解密

2021-10-01 01:34:31 字數 2377 閱讀 3673

sqlserver儲存過程加密、解密–加密儲存過程

–判斷表是否存在,如果存在就刪除

if object_id(n'dbo.test',n'u') is not null

drop table dbo.pwdtable

go

–建立表

–varbinary是乙個可以改變長度的二進位制資料。

–default(』』)如果某列沒有提供具體的值,那麼它提供了乙個預設值。

create table dbo.test

( id int identity(1,1) not null,

name nvarchar(max) not null,

pord_user varbinary(max) null,

pord_password varbinary(max) null,

remarks nvarchar(max) default('')

)go

–編輯儲存過程

–首先判斷儲存過程是否存在

if object_id(n'use_in',n'p') is not null

drop proc use_in

goif object_id(n'usp_de',n'p') is not null

drop proc usp_de

go

–建立加密儲存過程

create proc use_in

@en varchar(20),

@name nvarchar(max),

@pord_user nvarchar(max)='',

@pord_password nvarchar(max)='',

@remarks nvarchar(max)=''

as begin

insert test(name,pord_user,pord_password,remarks)

values

(@name,

encryptbypassphrase(@en,cast(@pord_user as nvarchar(max))),

encryptbypassphrase(@en,cast(@pord_password as nvarchar(max))),

@remarks

)end

go

–建立解密儲存過程

create proc usp_de

@de varchar(20)--,@pwd_user nvarchar(max) ----取消注釋可以按使用者名稱查詢

as begin

select

id,name,

cast(decryptbypassphrase(@de,pord_user) as nvarchar(max)),

cast(decryptbypassphrase(@de,pord_password) as nvarchar(max)),

remarks

from

test

--where ----取消注釋可以按使用者名稱查詢

-- pwd_user=encryptbypassphrase(@de,cast(@pwd_user as nvarchar(max)))

endgo

–允許將顯式值插入表的標識列中 on-允許 off-不允許

set identity_insert test off
–向表內插入加密資料

–i won』t tell you!為加密字串

–remarks欄位可以不寫、

exec use_in 'i won''t tell you!','喬丹','lin002','lin002','管理員'

exec use_in 'i won''t tell you!','李寧','lin001','lin001','客戶'

go

–查詢表資料

–查詢表內資料

----如果建立解密儲存過程的時候增加了@pwd_user變數,取消注釋、填寫使用者名稱到』'中,可以按使用者名稱查詢

Sql server 儲存過程加密

本方法可用於加密sql儲存過程 函式或者觸發器 使用 with encryption 選項 with encryption 子句對使用者隱藏儲存過程的文字 例子 if object id n pro encrypt test is not null drop procedure pro encryp...

儲存過程加密

返回老師管理下所有學生數 create procedure proc sum student teacher id int sum student int outputas select sum student count id count 是統計函式 from student where teac...

sql server儲存過程

建立表的語句 create table student sno int primary key,sname nvarchar 30 sgentle nvarchar 2 sage int,sbirth smalldatetime,sdept nvarchar 30 drop table studen...