RSA算法原理
瀏覽量:3484
在SQL SERVER中實(shí)現(xiàn)RSA加密算法
一、RSA算法原理
RSA算法非常簡單,概述如下:
找兩素?cái)?shù)p和q
取n=p*q
取t=(p-1)*(q-1)
取任何一個數(shù)e,要求滿足e<t并且e與t互素(就是最大公因數(shù)為1)
取d*e%t==1
這樣最終得到三個數(shù): n d e
找兩素?cái)?shù)p和q
取n=p*q
取t=(p-1)*(q-1)
取任何一個數(shù)e,要求滿足e<t并且e與t互素(就是最大公因數(shù)為1)
取d*e%t==1
這樣最終得到三個數(shù): n d e
設(shè)消息為數(shù)M (M <n)
設(shè)c=(M**d)%n就得到了加密后的消息c
設(shè)m=(c**e)%n則 m == M,從而完成對c的解密。
注:**表示次方,上面兩式中的d和e可以互換。
設(shè)c=(M**d)%n就得到了加密后的消息c
設(shè)m=(c**e)%n則 m == M,從而完成對c的解密。
注:**表示次方,上面兩式中的d和e可以互換。
在對稱加密中:
n d兩個數(shù)構(gòu)成公鑰,可以告訴別人;
n e兩個數(shù)構(gòu)成私鑰,e自己保留,不讓任何人知道。
給別人發(fā)送的信息使用e加密,只要別人能用d解開就證明信息是由你發(fā)送的,構(gòu)成了簽名機(jī)制。
別人給你發(fā)送信息時使用d加密,這樣只有擁有e的你能夠?qū)ζ浣饷堋?/div>
n d兩個數(shù)構(gòu)成公鑰,可以告訴別人;
n e兩個數(shù)構(gòu)成私鑰,e自己保留,不讓任何人知道。
給別人發(fā)送的信息使用e加密,只要別人能用d解開就證明信息是由你發(fā)送的,構(gòu)成了簽名機(jī)制。
別人給你發(fā)送信息時使用d加密,這樣只有擁有e的你能夠?qū)ζ浣饷堋?/div>
rsa的安全性在于對于一個大數(shù)n,沒有有效的方法能夠?qū)⑵浞纸鈴亩谝阎猲 d的情況下無法獲得e;同樣在已知n e的情況下無法求得d。
以上內(nèi)容出自原文出處中國互聯(lián)網(wǎng),CRM,辦公OA,軟件開發(fā)易勢科技最專業(yè)
二、使用T-SQL實(shí)現(xiàn)RSA算法
--判斷是否為素?cái)?shù)
if object_id('f_pnumtest') is not null
drop function f_isPrimeNum
go
create function [dbo].[f_isPrimeNum]
(@p int)
returns bit
begin
declare @flg bit,@i int
select @flg=1, @i=2
while @i<sqrt(@p)
begin
if(@p%@i=0 )
begin
set @flg=0
break
end
set @i=@i+1
end
return @flg
end
--判斷兩個數(shù)是否互素,首先要選取兩個互素的數(shù)
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
--產(chǎn)生密鑰對
if object_id('p_createKey1') is not null
drop proc p_createKey1
go
create proc p_createKey1
@p int,@q int
as
begin
declare @n bigint,@t bigint,@flag int,@d int
if dbo.f_pnumtest(@p)=0
begin
print cast(@p as varchar)+'不是素?cái)?shù),請重新選擇數(shù)據(jù)'
return
end
if dbo.f_pnumtest(@q)=0
begin
print cast(@q as varchar)+'不是素?cái)?shù),請重新選擇數(shù)據(jù)'
return
end
print '請從下列數(shù)據(jù)中選擇其中一對,作為密鑰'
select @n=@p*@q,@t=(@p-1)*(@q-1)
declare @e int
set @e=2
while @e<@t
begin
if dbo.f_isNUmsPrime(@e,@t)=0
begin
set @d=2
while @d<@n
begin
if(@e*@d%@t=1)
print cast(@e as varchar)+space(5)+cast(@d as varchar)
set @d=@d+1
end
end
set @e=@e+1
end
end
/*加密函數(shù)說明,@key 為上一個存儲過程中選擇的密碼中的一個 ,@p ,@q 產(chǎn)生密鑰對時選擇的兩個數(shù)。獲取每一個字符的ascii值,然后進(jìn)行加密,產(chǎn)生2個字節(jié)的16位數(shù)據(jù)*/
if object_id('f_RSAEncry') is not null
drop function f_RSAEncry
go
create function f_RSAEncry
(@s varchar(100),@key int ,@p int ,@q int)
returns varchar(8000)
as
begin
declare @crypt varchar(8000)
set @crypt=''
while len(@s)>0
begin
declare @i int,@tmp varchar(10),@k2 int,@leftchar int
select @leftchar=ascii(left(@s,1)),@k2=@key,@i=1
while @k2>0
begin
set @i=(@leftchar*@i)%(@p*@q)
set @k2=@k2-1
end
set @tmp=''
select @tmp=case when @i%16 between 10 and 15 then char( @i%16+55) else cast(@i%16 as varchar) end +@tmp,@i=@i/16
from (select number from master.dbo.spt_values where type='p' and number<10 )K
order by number desc
set @crypt=@crypt+right(@tmp,4)
set @s=stuff(@s,1,1,'')
end
return @crypt
end
--解密:@key 為一個存儲過程中選擇的密碼對中另一個數(shù)字 ,@p ,@q 產(chǎn)生密鑰對時選擇的兩個數(shù)
if object_id('f_RSADecry') is not null
drop function f_RSADecry
go
create function f_RSADecry
(@s varchar(100),@key int ,@p int ,@q int)
returns varchar(8000)
as
begin
declare @crypt varchar(8000)
set @crypt=''
while len(@s)>0
begin
declare @i int
select @i=sum(data1)
from ( select case upper(substring(left(@s,4), number, 1)) when 'A' then 10
when 'B' then 11
when 'C' then 12
when 'D' then 13
when 'E' then 14
when 'F' then 15
else substring(left(@s,4), number, 1)
end* power(16, len(left(@s,4)) - number) data1
from (select number from master.dbo.spt_values where type='p')K
where number <= len(left(@s,4))
) L
declare @k2 int,@j int
select @k2=@key,@j=1
while @k2>0
begin
set @j=(@i*@j)%(@p*@q)
set @k2=@k2-1
end
set @crypt=@crypt+char(@j)
set @s=stuff(@s,1,4,'')
end
return @crypt
end
三、在SQL SERVER中的使用
三、在SQL SERVER中的使用
【測試】
if object_id('tb') is not null
drop table tb
go
create table tb(id int identity(1,1),col varchar(100))
go
insert into tb values(dbo.f_RSAEncry('RSA',63,47,59))
select * from tb
id col
1 069505EE02F3
select id,col=dbo.f_RSADecry(col,847,47,59)
from tb
id col
1 RSA
四、目前版本函數(shù)的缺點(diǎn)
1、目前只能對ascii符號進(jìn)行加密,對unicode尚不支持。
2、在選取的素?cái)?shù)都比較小,所以密鑰空間比較小,而實(shí)際應(yīng)用中選取的素?cái)?shù)都會非常的大,不容易破解。但是對于一些基礎(chǔ)的加密還能夠使用。
3、如果一次加密覺得安全性不夠的話,可以進(jìn)行重復(fù)加密(即進(jìn)行多次加密),兩次的密鑰最好不相同。
例如:insert into tb values(dbo.f_RSAEncry(dbo.f_RSAEncry('RSA',63,47,59),23,11,17))
那么解密的時候,按照加密的逆序進(jìn)行解密:
select id,col=dbo.f_RSADecry(dbo.f_RSADecry(col,7,11,17),847,47,59)
from tb
4、如果選取的數(shù)字比較大,那么在進(jìn)行加密的時候,生成的16進(jìn)制密文最好使用3個字節(jié)或者更多。
上一篇:HTML5最新標(biāo)簽
下一篇:調(diào)試的過程
0