注冊|登錄

聯(lián)系電話:024-31891684  13390130939
沈陽軟件公司--沈陽軟件定制

沈陽軟件開發(fā)_沈陽軟件公司_沈陽軟件定制/軟件/最新技術(shù)

Latest technology最新技術(shù)

RSA算法原理

瀏覽量:3489

在SQL SERVER中實現(xiàn)RSA加密算法

一、RSA算法原理
RSA算法非常簡單,概述如下:
找兩素數(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可以互換。
在對稱加密中:
n d兩個數(shù)構(gòu)成公鑰,可以告訴別人;
n e兩個數(shù)構(gòu)成私鑰,e自己保留,不讓任何人知道。
給別人發(fā)送的信息使用e加密,只要別人能用d解開就證明信息是由你發(fā)送的,構(gòu)成了簽名機制。
別人給你發(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實現(xiàn)RSA算法
  --判斷是否為素數(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)+'不是素數(shù),請重新選擇數(shù)據(jù)'
  return
  end
  if dbo.f_pnumtest(@q)=0
  begin
  print cast(@q as varchar)+'不是素數(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值,然后進行加密,產(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中的使用
【測試】 
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ù)的缺點
1、目前只能對ascii符號進行加密,對unicode尚不支持。
2、在選取的素數(shù)都比較小,所以密鑰空間比較小,而實際應(yīng)用中選取的素數(shù)都會非常的大,不容易破解。但是對于一些基礎(chǔ)的加密還能夠使用。
3、如果一次加密覺得安全性不夠的話,可以進行重復(fù)加密(即進行多次加密),兩次的密鑰最好不相同。
例如:insert into tb values(dbo.f_RSAEncry(dbo.f_RSAEncry('RSA',63,47,59),23,11,17))
那么解密的時候,按照加密的逆序進行解密:
select id,col=dbo.f_RSADecry(dbo.f_RSADecry(col,7,11,17),847,47,59)
from tb
4、如果選取的數(shù)字比較大,那么在進行加密的時候,生成的16進制密文最好使用3個字節(jié)或者更多。

沈陽團購網(wǎng)|營口網(wǎng)站制作|沈陽軟件公司|軟件定制|網(wǎng)站建設(shè)|加盟易勢|提交問題

主站蜘蛛池模板: www夜插内射视频网站| 亚洲国产精品日韩在线观看 | 女人张开腿让男人桶视频免费大全| 久久精品*5在热| 欧美人与牲动交xxxxbbbb| 伊人色院成人蜜桃视频| 老司机福利在线播放| 国产后入清纯学生妹| 69堂国产成人精品视频不卡| 国模精品视频一区二区三区| а天堂中文最新一区二区三区| 无遮挡一级毛片性视频不卡| 九九视频在线观看6| 欧美日韩一卡二卡| 亚洲综合五月天| 精品96在线观看影院| 回复术士的重来人生第一季樱花动漫| 麻豆国产尤物av尤物在线观看| 国产精品久免费的黄网站| 91普通话国产对白在线| 天天躁夜夜躁狠狠躁2021| 一级毛片aaaaaa免费看| 扒开双腿疯狂进出爽爽动态图| 久久精品噜噜噜成人av| 最近免费中文字幕大全高清10| 亚洲国产精彩中文乱码av| 波多野结衣中文丝袜字幕| 免费a级在线观看播放| 精品国产免费一区二区三区| 国产99视频精品草莓免视看| 韩国免费人成在线观看网站| 国产成人精品免费视频动漫| xxxx日本在线| 国产精品毛片大码女人| 91精品国产综合久久久久久| 在线观看国产一区二区三区 | 欧美性猛交xxxx乱大交丰满| 亚洲激情中文字幕| 激情图片小说区| 人人妻人人狠人人爽| 男女激情边摸边做边吃奶在线观看|