在SQL SERVER中實現RSA加解密函數

基本含義:RSA公開密鑰密碼體制。所謂的公開密鑰密碼體制就是使用不一樣的加密密鑰與解密密鑰,是一種「由已知加密密鑰推導出解密密鑰在計算上是不可行的」密碼體制。算法

在SQL SERVER中實現RSA加密算法sql

--判斷是否爲素數函數

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  )

     begin

        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

--產生密鑰對spa

if object_id('p_createKey') is not null

  drop proc p_createKey

go

create proc p_createKey

@p int,@q int

as

begin

    declare @n bigint,@t bigint,@flag int,@d int

    if dbo.f_primeNumTest(@p)=0

    begin

       print cast(@p as varchar)+'不是素數,請從新選擇數據'

       return

    end

    if dbo.f_primeNumTest(@q)=0

    begin

       print cast(@q as varchar)+'不是素數,請從新選擇數據'

       return

  end

  print '請從下列數據中選擇其中一對,做爲密鑰'

    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

--加密函數.net

@key 爲上一個存儲過程當中選擇的密碼中的一個,@p ,@q 產生密鑰對時選擇的兩個數。獲取每個字符的unicode值,而後進行加密,產生個字節的位數據code

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 nvarchar(4000)

as

begin

   declare @crypt varchar(8000)

     set @crypt=''

   while len(@s)>0

   begin

              declare @i bigint,@tmp varchar(10),@k2 int,@leftchar int

              select @leftchar=unicode(left(@s,1)),@k2=@key/2,@i=1

              while @k2>0

              begin

                     set @i=(cast(power(@leftchar,2) as bigint)*@i)%(@p*@q)

                     set @k2=@k2-1

              end 

              set @i=(@leftchar*@i)%(@p*@q)   

              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,6)

              set @s=stuff(@s,1,1,'')

  end

  return @crypt

end

--解密ip

@key 爲一個存儲過程當中選擇的密碼對中另外一個數字,@p ,@q 產生密鑰對時選擇的兩個數unicode

if object_id('f_RSADecry') is not null

  drop function f_RSADecry

go

create function f_RSADecry

 (@s nvarchar(4000),@key int ,@p int ,@q int)

returns nvarchar(4000)

as

begin

  declare @crypt varchar(8000)

  set @crypt=''

  while len(@s)>0

    begin

       declare @leftchar bigint

       select @leftchar=sum(data1)

       from (select case upper(substring(left(@s,6), 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,6), number, 1)

                end* power(16, len(left(@s,6)) - number) data1

         from (select number from master.dbo.spt_values where type='p')K

         where number <= len(left(@s,6))

     ) L

    declare @k2 int,@j bigint

       select @k2=@key/2,@j=1

    while @k2>0

       begin

      set @j=(cast(power(@leftchar,2)as bigint)*@j)%(@p*@q)

      set @k2=@k2-1

    end

    set @j=(@leftchar*@j)%(@p*@q)

    set @crypt=@crypt+nchar(@j)

    set @s=stuff(@s,1,6,'')

    end

  return @crypt

end

使用方法:get

一、先使用p_createkey生成一對密鑰,參數爲兩個參數

二、調用相應進行加密、解密

編寫觸發器,使兩表有同步加密的效果

USE [test]

GO

/****** Object:  Trigger [dbo].[trig_insert]    Script Date: 07/24/2018 14:33:43 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

--建立insert觸發器

ALTER trigger [dbo].[trig_insert]

on [dbo].[tt]

after insert,update,delete

as

begin

    if object_id(N'ttt',N'U') is null

        create table ttt(id int,name varchar(50),description varchar(50));

    truncate table ttt;

insert into ttt(id,name,description) select id,name,dbo.f_RSAEncry(description,779,1163,59) from tt

end

--建立解密視圖

create view ttt_view  with encryption as select id,name,dbo.f_RSADecry(description,35039,1163,59) description from ttt;
相關文章
相關標籤/搜索