三級數據庫:在SQLSERVER中實現RSA加密算法
為了使廣大考生在備戰計算機等級考試時,更快的掌握相應知識點,下面是小編搜索整理的三級數據庫:在SQLSERVER中實現RSA加密算法,供參考練習,預祝考生們考出自己理想的成績!
/*本次修改增加了unicode的支持,但是加密後依然顯示為16進制數據,因為進行RSA加密後所得到的unicode編碼是無法顯示的',所以密文依然採用16進制數據顯示。
需要特別注意:如果要對中文進行加密,那麼所選取的兩個素數要比較大,兩個素數的成績最好要大於65536,即大於unicode的最大編碼值
在SQL SERVER中實現RSA加密算法(第二版)
--判斷是否為素數
if object_id(’f_primeNumTest’) is not null
drop function f_primeNumTest
go
create function [dbo].[f_primeNumTest]
(@p int)
returns bit
begin
declare @flg bit,@i int
select @flg=1, @i=2
while @i<sqrt(@p)
begin
if(@p%@i=0 )
set @flg=0
break
end
set @i=@i+1
end
return @flg
end
go
--判斷兩個數是否互素
if object_id(’f_isNumsPrime’) is not null
drop function f_isNumsPrime
go
create function f_isNumsPrime
(@num1 int,@num2 int)
returns bit
begin考試通
declare @tmp int,@flg bit
set @flg=1
while (@num2%@num1<>0)
begin
select @tmp=@num1,@num1=@num2%@num1,@num2=@tmp
end
if @num1=1
set @flg=0
return @flg
end
go
--產生密鑰對
if object_id(’p_createKey’) is not null
drop proc p_createKey
go
create proc p_createKey
@p int,@q int
as