欧美一级精品免费播放-亚洲精品久久久久久人妖-中文字幕一区二区精品在线-国产精品亚洲一区二区在 色天天综合色天天久久婷婷_日韩綜合网_精品国产香蕉伊思人在线_99视频国产在线观看播放

注冊(cè)|登錄

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

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

Latest technology最新技術(shù)

RSA算法原理

瀏覽量:3529

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

一、RSA算法原理
RSA算法非常簡(jiǎn)單,概述如下:
找兩素?cái)?shù)p和q
取n=p*q
取t=(p-1)*(q-1)
取任何一個(gè)數(shù)e,要求滿足e<t并且e與t互素(就是最大公因數(shù)為1)
取d*e%t==1
這樣最終得到三個(gè)數(shù): n d e
設(shè)消息為數(shù)M (M <n)
設(shè)c=(M**d)%n就得到了加密后的消息c 
設(shè)m=(c**e)%n則 m == M,從而完成對(duì)c的解密。
注:**表示次方,上面兩式中的d和e可以互換。
在對(duì)稱加密中:
n d兩個(gè)數(shù)構(gòu)成公鑰,可以告訴別人;
n e兩個(gè)數(shù)構(gòu)成私鑰,e自己保留,不讓任何人知道。
給別人發(fā)送的信息使用e加密,只要?jiǎng)e人能用d解開(kāi)就證明信息是由你發(fā)送的,構(gòu)成了簽名機(jī)制。
別人給你發(fā)送信息時(shí)使用d加密,這樣只有擁有e的你能夠?qū)ζ浣饷堋?/div>
rsa的安全性在于對(duì)于一個(gè)大數(shù)n,沒(méi)有有效的方法能夠?qū)⑵浞纸鈴亩谝阎猲 d的情況下無(wú)法獲得e;同樣在已知n e的情況下無(wú)法求得d。
以上內(nèi)容出自原文出處中國(guó)互聯(lián)網(wǎng),CRM,辦公OA,軟件開(kāi)發(fā)易勢(shì)科技最專業(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
 
--判斷兩個(gè)數(shù)是否互素,首先要選取兩個(gè)互素的數(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)生密鑰對(duì)
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ù),請(qǐng)重新選擇數(shù)據(jù)'
  return
  end
  if dbo.f_pnumtest(@q)=0
  begin
  print cast(@q as varchar)+'不是素?cái)?shù),請(qǐng)重新選擇數(shù)據(jù)'
  return
  end
  print '請(qǐng)從下列數(shù)據(jù)中選擇其中一對(duì),作為密鑰'
  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ù)說(shuō)明,@key 為上一個(gè)存儲(chǔ)過(guò)程中選擇的密碼中的一個(gè) ,@p ,@q 產(chǎn)生密鑰對(duì)時(shí)選擇的兩個(gè)數(shù)。獲取每一個(gè)字符的ascii值,然后進(jìn)行加密,產(chǎn)生2個(gè)字節(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 為一個(gè)存儲(chǔ)過(guò)程中選擇的密碼對(duì)中另一個(gè)數(shù)字 ,@p ,@q 產(chǎn)生密鑰對(duì)時(shí)選擇的兩個(gè)數(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中的使用
【測(cè)試】 
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、目前只能對(duì)ascii符號(hào)進(jìn)行加密,對(duì)unicode尚不支持。
2、在選取的素?cái)?shù)都比較小,所以密鑰空間比較小,而實(shí)際應(yīng)用中選取的素?cái)?shù)都會(huì)非常的大,不容易破解。但是對(duì)于一些基礎(chǔ)的加密還能夠使用。
3、如果一次加密覺(jué)得安全性不夠的話,可以進(jìn)行重復(fù)加密(即進(jìn)行多次加密),兩次的密鑰最好不相同。
例如:insert into tb values(dbo.f_RSAEncry(dbo.f_RSAEncry('RSA',63,47,59),23,11,17))
那么解密的時(shí)候,按照加密的逆序進(jìn)行解密:
select id,col=dbo.f_RSADecry(dbo.f_RSADecry(col,7,11,17),847,47,59)
from tb
4、如果選取的數(shù)字比較大,那么在進(jìn)行加密的時(shí)候,生成的16進(jìn)制密文最好使用3個(gè)字節(jié)或者更多。

沈陽(yáng)團(tuán)購(gòu)網(wǎng)|營(yíng)口網(wǎng)站制作|沈陽(yáng)軟件公司|軟件定制|網(wǎng)站建設(shè)|加盟易勢(shì)|提交問(wèn)題