RAS算法的介绍
1978年就出现了这种算法,它是第一个既能用于数据加密
也能用于数字签名的算法。它易于理解和操作,也很流行。算
法的名字以发明者的名字命名:Ron Rivest, AdiShamir 和
Leonard Adleman。但RSA的安全性一直未能得到理论上的证明。
RSA的安全性依赖于大数分解。公钥和私钥都是两个大素数
( 大于 100个十进制位)的函数。据猜测,从一个密钥和密文
推断出明文的难度等同于分解两个大素数的积。
密钥对的产生:选择两个大素数,p 和q 。计算:
n = p * q
然后随机选择加密密钥e,要求 e 和 ( p - 1 ) * ( q - 1 )
互质。最后,利用Euclid 算法计算解密密钥d, 满足
e * d = 1 ( mod ( p - 1 ) * ( q - 1 ) )
其中n和d也要互质。数e和
n是公钥,d是私钥。两个素数p和q不再需要,应该丢弃,不要让任
何人知道。 加密信息 m(二进制表示)时,首先把m分成等长数据
块 m1 ,m2,..., mi ,块长s,其中 2^s <= n, s 尽可能的大。对
应的密文是:
ci = mi^e ( mod n ) ( a )
解密时作如下计算:
mi = ci^d ( mod n ) ( b )
RSA 可用于数字签名,方案是用 ( a ) 式签名, ( b )
式验证。具体操作时考虑到安全性和 m信息量较大等因素,一般是先
作 HASH 运算。
RSA 的安全性。
RSA的安全性依赖于大数分解,但是否等同于大数分解一直未能得到理
论上的证明,因为没有证明破解RSA就一定需要作大数分解。假设存在
一种无须分解大数的算法,那它肯定可以修改成为大数分解算法。目前,
RSA的一些变种算法已被证明等价于大数分解。不管怎样,分解n是最显
然的攻击方法。现在,人们已能分解140多个十进制位的大素数。因此,
模数n必须选大一些,因具体适用情况而定。
RSA的速度:
由于进行的都是大数计算,使得RSA最快的情况也比DES慢上100倍,无论
是软件还是硬件实现。速度一直是RSA的缺陷。一般来说只用于少量数据
加密。
RSA的选择密文攻击:
RSA在选择密文攻击面前很脆弱。一般攻击者是将某一信息作一下伪装
(Blind),让拥有私钥的实体签署。然后,经过计算就可得到它所想要的信
息。实际上,攻击利用的都是同一个弱点,即存在这样一个事实:乘幂保
留了输入的乘法结构:
( XM )^d = X^d *M^d mod n
前面已经提到,这个固有的问题来自于公钥密码系统的最有用的特征
--每个人都能使用公钥。但从算法上无法解决这一问题,主要措施有
两条:一条是采用好的公钥协议,保证工作过程中实体不对其他实体
任意产生的信息解密,不对自己一无所知的信息签名;另一条是决不
对陌生人送来的随机文档签名,签名时首先使用One-Way HashFunction
对文档作HASH处理,或同时使用不同的签名算法。在中提到了几种不
同类型的攻击方法。
RSA的公共模数攻击。
若系统中共有一个模数,只是不同的人拥有不同的e和d,系统将是危险
的。最普遍的情况是同一信息用不同的公钥加密,这些公钥共模而且互
质,那末该信息无需私钥就可得到恢复。设P为信息明文,两个加密密钥
为e1和e2,公共模数是n,则:
C1 = P^e1 mod n
C2 = P^e2 mod n
密码分析者知道n、e1、e2、C1和C2,就能得到P。
因为e1和e2互质,故用Euclidean算法能找到r和s,满足:
r * e1 + s * e2 = 1
假设r为负数,需再用Euclidean算法计算C1^(-1),则
( C1^(-1) )^(-r) * C2^s = P mod n
另外,还有其它几种利用公共模数攻击的方法。总之,如果知道给定模数
的一对e和d,一是有利于攻击者分解模数,一是有利于攻击者计算出其它
成对的e’和d’,而无需分解模数。解决办法只有一个,那就是不要共享
模数n。
RSA的小指数攻击。 有一种提高
RSA速度的建议是使公钥e取较小的值,这样会使加密变得易于实现,速度
有所提高。但这样作是不安全的,对付办法就是e和d都取较大的值。
RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作。
RSA是被研究得最广泛的公钥算法,从提出到现在已近二十年,经历了各
种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一。
RSA的安全性依赖于大数的因子分解,但并没有从理论上证明破译RSA的难
度与大数分解难度等价。即RSA的重大缺陷是无法从理论上把握它的保密性
能如何,而且密码学界多数人士倾向于因子分解不是NPC问题。
RSA的缺点主要有:
A)产生密钥很麻烦,受到素数产生技术的限制,因而难以做到一次
一密。B)分组长度太大,为保证安全性,n 至少也要 600 bits
以上,使运算代价很高,尤其是速度较慢,较对称密码算法慢几个数量级;
且随着大数分解技术的发展,这个长度还在增加,不利于数据格式的标准化。
目前,SET(Secure Electronic Transaction)协议中要求CA采用2048比特长
的密钥,其他实体使用1024比特的密钥。
以下的为源码:
'************************************************
'* 欢迎光临品琳居 http://plindge.yeah.net *
'************************************************
' 经典加密算法在VB中的实现(1)- Rsa
' 我很快会把一个快速的64位RSA加密算法的例子放上主页
Public key(1 To 3) As Long
Private Const base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrst
uvwxyz0123456789+/"
Public Sub GenKey()
Dim d As Long, phi As Long, e As Long
Dim m As Long, x As Long, q As Long
Dim p As Long
Randomize
On Error GoTo top
top:
p = Rnd * 1000 \ 1
If IsPrime(p) = False Then GoTo top
Sel_q:
q = Rnd * 1000 \ 1
If IsPrime(q) = False Then GoTo Sel_q
n = p * q \ 1
phi = (p - 1) * (q - 1) \ 1
d = Rnd * n \ 1
If d = 0 Or n = 0 Or d = 1 Then GoTo top
e = Euler(phi, d)
If e = 0 Or e = 1 Then GoTo top
x = Mult(255, e, n)
If Not Mult(x, d, n) = 255 Then
DoEvents
GoTo top
ElseIf Mult(x, d, n) = 255 Then
key(1) = e
key(2) = d
key(3) = n
End If
End Sub
Private Function Euler(ByVal a As Long, ByVal b As Long) As Long
On Error GoTo error2
r1 = a: r = b
p1 = 0: p = 1
q1 = 2: q = 0
n = -1
Do Until r = 0
r2 = r1: r1 = r
p2 = p1: p1 = p
q2 = q1: q1 = q
n = n + 1
r = r2 Mod r1
c = r2 \ r1
p = (c * p1) + p2
q = (c * q1) + q2
Loop
s = (b * p1) - (a * q1)
If s > 0 Then
x = p1
Else
x = (0 - p1) + a
End If
Euler = x
Exit Function
error2:
Euler = 0
End Function
Private Function Mult(ByVal x As Long, ByVal p As Long, ByVal m As Lon
g) As Long
y = 1
On Error GoTo error1
Do While p > 0
Do While (p / 2) = (p \ 2)
x = (x * x) Mod m
p = p / 2
Loop
y = (x * y) Mod m
p = p - 1
Loop
Mult = y
Exit Function
error1:
y = 0
End Function
Private Function IsPrime(lngNumber As Long) As Boolean
Dim lngCount As Long
Dim lngSqr As Long
Dim x As Long
lngSqr = Sqr(lngNumber) ' get the int square root
If lngNumber < 2 Then
IsPrime = False
Exit Function
End If
lngCount = 2
IsPrime = True
If lngNumber Mod lngCount = 0& Then
IsPrime = False
Exit Function
End If
lngCount = 3
For x& = lngCount To lngSqr Step 2
If lngNumber Mod x& = 0 Then
IsPrime = False
Exit Function
End If
Next
End Function
Private Function Base64_Encode(DecryptedText As String) As String
Dim c1, c2, c3 As Integer
Dim w1 As Integer
Dim w2 As Integer
Dim w3 As Integer
Dim w4 As Integer
Dim n As Integer
Dim retry As String
For n = 1 To Len(DecryptedText) Step 3
c1 = Asc(Mid$(DecryptedText, n, 1))
c2 = Asc(Mid$(DecryptedText, n + 1, 1) + Chr$(0))
c3 = Asc(Mid$(DecryptedText, n + 2, 1) + Chr$(0))
w1 = Int(c1 / 4)
w2 = (c1 And 3) * 16 + Int(c2 / 16)
If Len(DecryptedText) >= n + 1 Then w3 = (c2 And 15) * 4 + Int(c
3 / 64) Else w3 = -1
If Len(DecryptedText) >= n + 2 Then w4 = c3 And 63 Else w4 = -1
retry = retry + mimeencode(w1) + mimeencode(w2) + mimeencode(w3)
+ mimeencode(w4)
Next
Base64_Encode = retry
End Function
Private Function Base64_Decode(a As String) As String
Dim w1 As Integer
Dim w2 As Integer
Dim w3 As Integer
Dim w4 As Integer
Dim n As Integer
Dim retry As String
For n = 1 To Len(a) Step 4
w1 = mimedecode(Mid$(a, n, 1))
w2 = mimedecode(Mid$(a, n + 1, 1))
w3 = mimedecode(Mid$(a, n + 2, 1))
w4 = mimedecode(Mid$(a, n + 3, 1))
If w2 >= 0 Then retry = retry + Chr$(((w1 * 4 + Int(w2 / 16)) An
d 255))
If w3 >= 0 Then retry = retry + Chr$(((w2 * 16 + Int(w3 / 4)) An
d 255))
If w4 >= 0 Then retry = retry + Chr$(((w3 * 64 + w4) And 255))
Next
Base64_Decode = retry
End Function
Private Function mimeencode(w As Integer) As String
If w >= 0 Then mimeencode = Mid$(base64, w + 1, 1) Else mimeencode
= ""
End Function
Private Function mimedecode(a As String) As Integer
If Len(a) = 0 Then mimedecode = -1: Exit Function
mimedecode = InStr(base64, a) - 1
End Function
Public Function Encode(ByVal Inp As String, ByVal e As Long, ByVal n A
s Long) As String
Dim s As String
s = ""
m = Inp
If m = "" Then Exit Function
s = Mult(CLng(Asc(Mid(m, 1, 1))), e, n)
For i = 2 To Len(m)
s = s & "+" & Mult(CLng(Asc(Mid(m, i, 1))), e, n)
Next i
Encode = Base64_Encode(s)
End Function
Public Function Decode(ByVal Inp As String, ByVal d As Long, ByVal n A
s Long) As String
St = ""
ind = Base64_Decode(Inp)
For i = 1 To Len(ind)
nxt = InStr(i, ind, "+")
If Not nxt = 0 Then
tok = Val(Mid(ind, i, nxt))
Else
tok = Val(Mid(ind, i))
End If
St = St + Chr(Mult(CLng(tok), d, n))
If Not nxt = 0 Then
i = nxt
Else
i = Len(ind)
End If
Next i
Decode = St
End Function
1978年就出现了这种算法,它是第一个既能用于数据加密
也能用于数字签名的算法。它易于理解和操作,也很流行。算
法的名字以发明者的名字命名:Ron Rivest, AdiShamir 和
Leonard Adleman。但RSA的安全性一直未能得到理论上的证明。
RSA的安全性依赖于大数分解。公钥和私钥都是两个大素数
( 大于 100个十进制位)的函数。据猜测,从一个密钥和密文
推断出明文的难度等同于分解两个大素数的积。
密钥对的产生:选择两个大素数,p 和q 。计算:
n = p * q
然后随机选择加密密钥e,要求 e 和 ( p - 1 ) * ( q - 1 )
互质。最后,利用Euclid 算法计算解密密钥d, 满足
e * d = 1 ( mod ( p - 1 ) * ( q - 1 ) )
其中n和d也要互质。数e和
n是公钥,d是私钥。两个素数p和q不再需要,应该丢弃,不要让任
何人知道。 加密信息 m(二进制表示)时,首先把m分成等长数据
块 m1 ,m2,..., mi ,块长s,其中 2^s <= n, s 尽可能的大。对
应的密文是:
ci = mi^e ( mod n ) ( a )
解密时作如下计算:
mi = ci^d ( mod n ) ( b )
RSA 可用于数字签名,方案是用 ( a ) 式签名, ( b )
式验证。具体操作时考虑到安全性和 m信息量较大等因素,一般是先
作 HASH 运算。
RSA 的安全性。
RSA的安全性依赖于大数分解,但是否等同于大数分解一直未能得到理
论上的证明,因为没有证明破解RSA就一定需要作大数分解。假设存在
一种无须分解大数的算法,那它肯定可以修改成为大数分解算法。目前,
RSA的一些变种算法已被证明等价于大数分解。不管怎样,分解n是最显
然的攻击方法。现在,人们已能分解140多个十进制位的大素数。因此,
模数n必须选大一些,因具体适用情况而定。
RSA的速度:
由于进行的都是大数计算,使得RSA最快的情况也比DES慢上100倍,无论
是软件还是硬件实现。速度一直是RSA的缺陷。一般来说只用于少量数据
加密。
RSA的选择密文攻击:
RSA在选择密文攻击面前很脆弱。一般攻击者是将某一信息作一下伪装
(Blind),让拥有私钥的实体签署。然后,经过计算就可得到它所想要的信
息。实际上,攻击利用的都是同一个弱点,即存在这样一个事实:乘幂保
留了输入的乘法结构:
( XM )^d = X^d *M^d mod n
前面已经提到,这个固有的问题来自于公钥密码系统的最有用的特征
--每个人都能使用公钥。但从算法上无法解决这一问题,主要措施有
两条:一条是采用好的公钥协议,保证工作过程中实体不对其他实体
任意产生的信息解密,不对自己一无所知的信息签名;另一条是决不
对陌生人送来的随机文档签名,签名时首先使用One-Way HashFunction
对文档作HASH处理,或同时使用不同的签名算法。在中提到了几种不
同类型的攻击方法。
RSA的公共模数攻击。
若系统中共有一个模数,只是不同的人拥有不同的e和d,系统将是危险
的。最普遍的情况是同一信息用不同的公钥加密,这些公钥共模而且互
质,那末该信息无需私钥就可得到恢复。设P为信息明文,两个加密密钥
为e1和e2,公共模数是n,则:
C1 = P^e1 mod n
C2 = P^e2 mod n
密码分析者知道n、e1、e2、C1和C2,就能得到P。
因为e1和e2互质,故用Euclidean算法能找到r和s,满足:
r * e1 + s * e2 = 1
假设r为负数,需再用Euclidean算法计算C1^(-1),则
( C1^(-1) )^(-r) * C2^s = P mod n
另外,还有其它几种利用公共模数攻击的方法。总之,如果知道给定模数
的一对e和d,一是有利于攻击者分解模数,一是有利于攻击者计算出其它
成对的e’和d’,而无需分解模数。解决办法只有一个,那就是不要共享
模数n。
RSA的小指数攻击。 有一种提高
RSA速度的建议是使公钥e取较小的值,这样会使加密变得易于实现,速度
有所提高。但这样作是不安全的,对付办法就是e和d都取较大的值。
RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作。
RSA是被研究得最广泛的公钥算法,从提出到现在已近二十年,经历了各
种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一。
RSA的安全性依赖于大数的因子分解,但并没有从理论上证明破译RSA的难
度与大数分解难度等价。即RSA的重大缺陷是无法从理论上把握它的保密性
能如何,而且密码学界多数人士倾向于因子分解不是NPC问题。
RSA的缺点主要有:
A)产生密钥很麻烦,受到素数产生技术的限制,因而难以做到一次
一密。B)分组长度太大,为保证安全性,n 至少也要 600 bits
以上,使运算代价很高,尤其是速度较慢,较对称密码算法慢几个数量级;
且随着大数分解技术的发展,这个长度还在增加,不利于数据格式的标准化。
目前,SET(Secure Electronic Transaction)协议中要求CA采用2048比特长
的密钥,其他实体使用1024比特的密钥。
以下的为源码:
'************************************************
'* 欢迎光临品琳居 http://plindge.yeah.net *
'************************************************
' 经典加密算法在VB中的实现(1)- Rsa
' 我很快会把一个快速的64位RSA加密算法的例子放上主页
Public key(1 To 3) As Long
Private Const base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrst
uvwxyz0123456789+/"
Public Sub GenKey()
Dim d As Long, phi As Long, e As Long
Dim m As Long, x As Long, q As Long
Dim p As Long
Randomize
On Error GoTo top
top:
p = Rnd * 1000 \ 1
If IsPrime(p) = False Then GoTo top
Sel_q:
q = Rnd * 1000 \ 1
If IsPrime(q) = False Then GoTo Sel_q
n = p * q \ 1
phi = (p - 1) * (q - 1) \ 1
d = Rnd * n \ 1
If d = 0 Or n = 0 Or d = 1 Then GoTo top
e = Euler(phi, d)
If e = 0 Or e = 1 Then GoTo top
x = Mult(255, e, n)
If Not Mult(x, d, n) = 255 Then
DoEvents
GoTo top
ElseIf Mult(x, d, n) = 255 Then
key(1) = e
key(2) = d
key(3) = n
End If
End Sub
Private Function Euler(ByVal a As Long, ByVal b As Long) As Long
On Error GoTo error2
r1 = a: r = b
p1 = 0: p = 1
q1 = 2: q = 0
n = -1
Do Until r = 0
r2 = r1: r1 = r
p2 = p1: p1 = p
q2 = q1: q1 = q
n = n + 1
r = r2 Mod r1
c = r2 \ r1
p = (c * p1) + p2
q = (c * q1) + q2
Loop
s = (b * p1) - (a * q1)
If s > 0 Then
x = p1
Else
x = (0 - p1) + a
End If
Euler = x
Exit Function
error2:
Euler = 0
End Function
Private Function Mult(ByVal x As Long, ByVal p As Long, ByVal m As Lon
g) As Long
y = 1
On Error GoTo error1
Do While p > 0
Do While (p / 2) = (p \ 2)
x = (x * x) Mod m
p = p / 2
Loop
y = (x * y) Mod m
p = p - 1
Loop
Mult = y
Exit Function
error1:
y = 0
End Function
Private Function IsPrime(lngNumber As Long) As Boolean
Dim lngCount As Long
Dim lngSqr As Long
Dim x As Long
lngSqr = Sqr(lngNumber) ' get the int square root
If lngNumber < 2 Then
IsPrime = False
Exit Function
End If
lngCount = 2
IsPrime = True
If lngNumber Mod lngCount = 0& Then
IsPrime = False
Exit Function
End If
lngCount = 3
For x& = lngCount To lngSqr Step 2
If lngNumber Mod x& = 0 Then
IsPrime = False
Exit Function
End If
Next
End Function
Private Function Base64_Encode(DecryptedText As String) As String
Dim c1, c2, c3 As Integer
Dim w1 As Integer
Dim w2 As Integer
Dim w3 As Integer
Dim w4 As Integer
Dim n As Integer
Dim retry As String
For n = 1 To Len(DecryptedText) Step 3
c1 = Asc(Mid$(DecryptedText, n, 1))
c2 = Asc(Mid$(DecryptedText, n + 1, 1) + Chr$(0))
c3 = Asc(Mid$(DecryptedText, n + 2, 1) + Chr$(0))
w1 = Int(c1 / 4)
w2 = (c1 And 3) * 16 + Int(c2 / 16)
If Len(DecryptedText) >= n + 1 Then w3 = (c2 And 15) * 4 + Int(c
3 / 64) Else w3 = -1
If Len(DecryptedText) >= n + 2 Then w4 = c3 And 63 Else w4 = -1
retry = retry + mimeencode(w1) + mimeencode(w2) + mimeencode(w3)
+ mimeencode(w4)
Next
Base64_Encode = retry
End Function
Private Function Base64_Decode(a As String) As String
Dim w1 As Integer
Dim w2 As Integer
Dim w3 As Integer
Dim w4 As Integer
Dim n As Integer
Dim retry As String
For n = 1 To Len(a) Step 4
w1 = mimedecode(Mid$(a, n, 1))
w2 = mimedecode(Mid$(a, n + 1, 1))
w3 = mimedecode(Mid$(a, n + 2, 1))
w4 = mimedecode(Mid$(a, n + 3, 1))
If w2 >= 0 Then retry = retry + Chr$(((w1 * 4 + Int(w2 / 16)) An
d 255))
If w3 >= 0 Then retry = retry + Chr$(((w2 * 16 + Int(w3 / 4)) An
d 255))
If w4 >= 0 Then retry = retry + Chr$(((w3 * 64 + w4) And 255))
Next
Base64_Decode = retry
End Function
Private Function mimeencode(w As Integer) As String
If w >= 0 Then mimeencode = Mid$(base64, w + 1, 1) Else mimeencode
= ""
End Function
Private Function mimedecode(a As String) As Integer
If Len(a) = 0 Then mimedecode = -1: Exit Function
mimedecode = InStr(base64, a) - 1
End Function
Public Function Encode(ByVal Inp As String, ByVal e As Long, ByVal n A
s Long) As String
Dim s As String
s = ""
m = Inp
If m = "" Then Exit Function
s = Mult(CLng(Asc(Mid(m, 1, 1))), e, n)
For i = 2 To Len(m)
s = s & "+" & Mult(CLng(Asc(Mid(m, i, 1))), e, n)
Next i
Encode = Base64_Encode(s)
End Function
Public Function Decode(ByVal Inp As String, ByVal d As Long, ByVal n A
s Long) As String
St = ""
ind = Base64_Decode(Inp)
For i = 1 To Len(ind)
nxt = InStr(i, ind, "+")
If Not nxt = 0 Then
tok = Val(Mid(ind, i, nxt))
Else
tok = Val(Mid(ind, i))
End If
St = St + Chr(Mult(CLng(tok), d, n))
If Not nxt = 0 Then
i = nxt
Else
i = Len(ind)
End If
Next i
Decode = St
End Function