<noscript id="eassg"><table id="eassg"></table></noscript>
  • <strike id="eassg"><s id="eassg"></s></strike>
  • <ul id="eassg"></ul>
    注冊|登錄

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

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

    Latest technology最新技術(shù)

    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
    設(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)成了簽名機(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中的使用
    【測試】 
    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é)或者更多。

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

    国产91精品久久久久久久| 国产成人精品免费视频大| 国产精品自在拍一区二区不卡| 日韩免费高清一级毛片| 国产精品久久无码一区二区三区网| 91freevideos精品| 无码国内精品人妻少妇蜜桃视频| 国产在线精品二区| 9久久这里只有精品国产| 亚洲国产精品视频| 日韩在线播放中文字幕| 日韩综合无码一区二区| 国产在热线精品视频| 国产精品视频公开费视频| 国产精品无码一区二区三区不卡| 国内精品久久久久影院亚洲| 国产精品香蕉在线一区| 国产福利91精品一区二区三区| 亚洲精品网站在线观看你懂的| 99ee6热久久免费精品6| 99在线观看精品免费99| 99精品国产高清自在线看超| 久久99精品一久久久久久| 久久精品.com| 久久国产精品久久久久久 | 在线私拍国产福利精品| 国产精品无打码在线播放| www.亚洲精品| 国产99久久久国产精品~~牛| 国产精品无码一区二区三级| 精品午夜福利1000在线观看| 国产成人精品日本亚洲专区| 亚洲欧洲久久久精品| 亚洲电影日韩精品| 国产精品 码ls字幕影视| 国产高清国内精品福利99久久| 国产精品成人久久久久三级午夜电影| 亚洲人午夜射精精品日韩| 国产成人久久久精品二区三区| 亚洲精品无码专区2| 国产精品高清在线观看|