/***************************************************

作者:herowang(让你望见影子的墙)

日期:2010.1.1

注:   转载请保留此信息

更多内容,请访问我的博客:blog.csdn.NET/herowang

****************************************************/

一、RSA算法原理

RSA算法非常简单,概述如下:

找两素数p和q

取n=p*q

取t=(p-1)*(q-1)

取任何一个数e,要求满足e<t并且e与t互素(就是最大公因数为)

取d*e%t==1

这样最终得到三个数:n   d   e

设消息为数M (M <n)

设c=(M**d)%n就得到了加密后的消息c

设m=(c**e)%n则m == M,从而完成对c的解密。

注:**表示次方,上面两式中的d和e可以互换。

在对称加密中:

n d两个数构成公钥,可以告诉别人;

n e两个数构成私钥,e自己保留,不让任何人知道。

给别人发送的信息使用e加密,只要别人能用d解开就证明信息是由你发送的,构成了签名机制。

别人给你发送信息时使用d加密,这样只有拥有e的你能够对其解密。

rsa的安全性在于对于一个大数n,没有有效的方法能够将其分解从而在已知n d的情况下无法获得e;同样在已知n e的情况下无法求得d。

以上内容出自原文出处http://www.xfocus.net/articles/200503/778.html

二、使用T-SQL实现RSA算法

--判断是否为素数

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

--判断两个数是否互素,首先要选取两个互素的数

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

--产生密钥对

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)+'不是素数,请重新选择数据'

return

end

if dbo.f_pnumtest(@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 产生密钥对时选择的两个数。获取每一个字符的ascii值,然后进行加密,产生个字节的位数据*/

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 为一个存储过程中选择的密码对中另一个数字,@p ,@q 产生密钥对时选择的两个数

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

*/

四、目前版本函数的缺点

1、目前只能对ascii符号进行加密,对unicode尚不支持。

2、在选取的素数都比较小,所以密钥空间比较小,而实际应用中选取的素数都会非常的大,不容易破解。但是对于一些基础的加密还能够使用。

3、如果一次加密觉得安全性不够的话,可以进行重复加密(即进行多次加密),两次的密钥最好不相同。

例如: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、如果选取的数字比较大,那么在进行加密的时候,生成的进制密文最好使用个字节或者更多。

在SQL SERVER中实现RSA加解密函数(第一版)的更多相关文章

  1. 在SQL SERVER中实现RSA加解密函数(第二版)

    /*************************************************** 作者:herowang(让你望见影子的墙) 日期:2010.1.5 注: 转载请保留此信息 更 ...

  2. C# 中使用 RSA加解密算法

    一.什么是RSA RSA公开密钥密码体制.所谓的公开密钥密码体制就是使用不同的加密密钥与解密密钥,是一种“由已知加密密钥推导出解密密钥在计算上是不可行的”密码体制. 在公开密钥密码体制中,加密密钥(即 ...

  3. SQL Server 中截取字符串常用的函数

    SQL Server 中截取字符串常用的函数: 1.LEFT ( character_expression , integer_expression ) 函数说明:LEFT ( '源字符串' , '要 ...

  4. SQL Server中的LEFT、RIGHT函数

    SQL Server中的LEFT.RIGHT函数. LEFT:返回字符串中从左边开始指定个数字符. LEFT(character_expression,integer_expression); RIG ...

  5. Java中的RSA加解密工具类:RSAUtils

    本人手写已测试,大家可以参考使用 package com.mirana.frame.utils.encrypt; import com.mirana.frame.utils.log.LogUtils; ...

  6. Lua 中的 RSA 加解密实现

    记得之前,部门某款游戏陆陆续续收到一些玩家反馈,抱怨在登录游戏时会等待很久.初步排查后基本断定可能是此游戏的登录服务器程序某块代码有问题,于是即安排了服务器同事作排查分析但一直无果. 之后我时间有了空 ...

  7. 实现SQL Server中的切割字符串SplitString函数,返回Table

    有时我们要用到批量操作时都会对字符串进行拆分,可是SQL Server中却没有自带Split函数,所以要自己来实现了. -- ===================================== ...

  8. sql server中截取字符串的常用函数

    我们如果要在sql server中,使用截取字符串的方法要怎样使用呢? sql server提供了3个常用截取字符串方法,LEFT().RIGHT().SUBSTRING() /****** Sql ...

  9. 实现SQL Server中的切割字符串SplitString函数

    有时我们要用到批量操作时都会对字符串进行拆分,可是SQL Server中却没有自带Split函数,所以要自己来实现了.没什么好说的,需要的朋友直接拿去用吧 SET ANSI_NULLS ON GO S ...

随机推荐

  1. svn清理失败且路径显示乱码

    1.下载 sqlite数据库工具,sqlite3.exe下载地址:sqlite官网http://www.sqlite.org/download.html,我这里是windows操作系统,因此下载 Pr ...

  2. '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp error

    '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp error 异常现象 ### Cause: java.sql.SQ ...

  3. 【CityHunter】游戏流程设计及技术要点

    目前CityHunter的核心玩法和主要的技术实现点已经全部到位,但是本人并未真正设计过整个游戏系统,所以只能循规蹈矩的先从最基本的流程图开始规划. 以上的流程是打开游戏直到控制台,接下来,是从控制台 ...

  4. top命令

    TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终止该程序为止.比较准确的说,top命令提供了实时的对系统处理器的状态监视.它将显示系统中C ...

  5. MQTT开发笔记之《MQTT Server》

    MQTT SERVER 性能测试报告 : http://w3yyb.sinaapp.com/archives/1601各个MQTT SERVER功能列表: http://blog.lenix.xyz/ ...

  6. kail linux安装软件提示“无法定位软件包”解决方法

    主要是更新源的问题,我安装ibus-pinyin老是提示"无法定位软件包" 最后我用的是163的源: # 源 deb http://mirrors.163.com/debian w ...

  7. Node.js入门笔记(5):案例两则

    案例分析:前端自动化 1. 实现一个自动创建前端项目文件的js 通过node.js自动创建前端项目目录,包括js目录,js目录css目录,index.html和对应的内容. 初步的代码如下: var ...

  8. [Head First设计模式]生活中学设计模式——迭代器模式

    系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...

  9. js高级群的一些整理6月

    https://github.com/the5fire/backbonejs-learning-note/blob/master/chapters/01-hello-backbonejs.rst Ba ...

  10. 前端进阶试题css(来自js高级前端开发---豪情)既然被发现了HOHO,那我就置顶了嘿嘿!觉得自己技术OK的可以把这套题目做完哦,然后加入高级前端的社区咯

    http://www.cnblogs.com/jikey/p/4426105.html js高级前端开发加群方法(此群很难进,里面纯技术,严禁广告,水群) 完整题目做完发邮箱(jikeytang@16 ...