考试辅导

名师推荐

试听名师的课 查看所有名师

在SQLSERVER中如何实现RSA加密算法 发布时间:2010-06-25 17:33 来源:互联网

  /*本次修改增加了unicode的支持,但是加密后依然显示为16进制数据,因为进行RSA加密后所得到的unicode编码是无法显示的,所以密文依然采用16进制数据显示。
  需要特别注意:如果要对中文进行加密,那么所选取的两个素数要比较大,两个素数的成绩最好要大于65536,即大于unicode的最大编码值
  在SQL SERVER中实现RSA加密算法(第二版)
  --判断是否为素数
  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
  --产生密钥对
  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
  /*加密函数说明,@key 为上一个存储过程中选择的密码中的一个 ,@p ,@q 产生密钥对时选择的两个数。获取每一个字符的unicode值,然后进行加密,产生3个字节的16位数据*/
  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

第一考试网友情提示:如果您遇到任何疑问,请登录第一考试网考试辅导频道或添加qq:,第一考试网以“为考友服务”为宗旨,秉承“快乐学习,轻松考试!”的理念,旨在为广大考友打造一个良好、温馨的学习与交流平台,欢迎持续关注。以上是小编为大家推荐的《在SQLSERVER中如何实现RSA加密算法》相关信息。

编辑推荐

计算机等级考试辅导:SQL中JOB的运行状态

计算机等级考试辅导:SQLSERVER2005的引用

三级:把数据导入不同的表空间

2009年三级信息管理技术辅导:战略数据规划

教你三种方法卸载Windows7SP1Beta