RSA加密解密算法 asp源码

时间:2009-08-28 13:10:00 

它是第一个既能用于数据加密也能用于数字签名的算法。它易于理解和操作,也很流行。算法的名字以发明者的名字命名:Ron Rivest, Adi Shamir 和Leonard Adleman。但RSA的安全性一直未能得到理论上的证明。

它经历了各种攻击,至今未被完全攻破。

<%
' Compiled by Lewis Edward Moten III
' lewis@moten.com
' http://www.lewismoten.com
' Wednesday, May 09, 2001 05:42 PM GMT +5
' RSA Encryption Class
'
' .KeyEnc
'  Key for others to encrypt data with.
'
' .KeyDec
'  Your personal private key.  Keep this hidden.
'
' .KeyMod
'  Used with both public and private keys when encrypting and decrypting data.
'
' .KeyGen
'  Used to generate both public and private keys for encrypting and decrypting data.
'
' .Encode(pStrMessage)
'  Encrypts message and returns in numeric format
'
' .Decode(pStrMessage)
'  Decrypts message and returns a string
'
Class TRSA
    Public KeyEnc
    Public KeyDec
    Private Function Mult(ByVal x, ByVal pg, ByVal m)
        dim y : y=1
 
        Do While pg > 0
         Do While (pg / 2) = Int((pg / 2))
             x = nMod((x * x), m)
             pg = pg / 2
         Loop
         y = nMod((x * y), m)
         pg = pg - 1
        Loop
        Mult = y
    End Function
    Private Function nMod(x, y)
        nMod = 0
        if y = 0 then Exit Function End If
        nMod = x - (Int(x / y) * y)
    End Function
    Private Function Euler(E3, PHI3)
     'genetates D from (E and PHI) using the Euler algorithm
     On Error Resume Next
     Dim u1, u2, u3, v1, v2, v3, q
     Dim t1, t2, t3, z, vv, inverse
     u1 = 1
     u2 = 0
     u3 = PHI3
     v1 = 0
     v2 = 1
     v3 = E3
     Do Until (v3 = 0)
         q = Int(u3 / v3)
         t1 = u1 - q * v1: t2 = u2 - q * v2: t3 = u3 - q * v3
         u1 = v1: u2 = v2: u3 = v3
         v1 = t1: v2 = t2: v3 = t3
         z = 1
     Loop
     If (u2 < 0) Then
         inverse = u2 + PHI3
     Else
         inverse = u2
     End If
     Euler = inverse
    End Function
    Private Function GCD(nPHI)
     On Error Resume Next
     Dim nE, y
     Const N_UP = 99999999 'set upper limit of random number For E
     Const N_LW = 10000000 'set lower limit of random number For E
     Randomize
     nE = Int((N_UP - N_LW + 1) * Rnd + N_LW)
  Do
      x = nPHI Mod nE
      y = x Mod nE
      If y <> 0 And IsPrime(nE) Then
          GCD = nE
          Exit Function
      Else
          nE = nE + 1
      End If
  Loop
    End Function
    Private Function IsPrime(lngNumber)
        On Error Resume Next
        Dim lngCount, ngSqr
        Dim x
        lngSqr = Int(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 NumberToHex(ByRef pLngNumber, ByRef pLngLength)
        NumberToHex = Right(String(pLngLength, "0") & Hex(pLngNumber), pLngLength)
    End Function
    Private Function HexToNumber(ByRef pStrHex)
        HexToNumber = CLng("&h" & pStrHex)
    End Function
    Public Function Encrypt(ByVal tIp)
        Dim encSt, z
        Dim strMult
        Dim iEnc, iMod
        Dim aKey : aKey=Split(KeyEnc,",")
        If tIp = "" Then Exit Function End If
        iEnc=Int(aKey(0))
        iMod=Int(aKey(1))
        For z = 1 To Len(tIp)
            encSt = encSt & NumberToHex(Mult(CLng(Asc(Mid(tIp, z, 1))), iEnc, iMod),8)
        Next
 
        Encrypt = encSt
    End Function
    Public Function Decrypt(ByVal tIp)
        Dim decSt, z
        Dim iDec, iMod
        Dim aKey : aKey=Split(KeyDec,",")
        if Len(tIp) Mod 8 <> 0 then Exit Function End If
       iDec=Int(aKey(0))
       iMod=Int(aKey(1))
 
        For z = 1 To Len(tIp) Step 8
            decSt = decSt + Chr(Mult(HexToNumber(Mid(tIp, z, 8)), iDec, iMod))
        Next
 
        Decrypt = decSt
    End Function
    Public Function genKey()
        'Generates the keys for E, D and N
        Dim E, D, N, p, q
        Const PQ_UP = 9999 'set upper limit of random number
        Const PQ_LW = 3170 'set lower limit of random number
        Const KEY_LOWER_LIMIT  = 10000000 'set For 64bit minimum
        p = 0: q = 0
        Randomize
        Do Until D > KEY_LOWER_LIMIT 'makes sure keys are 64bit minimum
            Do Until IsPrime(p) And IsPrime(q) ' make sure q and q are primes
                p = clng((PQ_UP - PQ_LW + 1) * Rnd + PQ_LW)
                q = clng((PQ_UP - PQ_LW + 1) * Rnd + PQ_LW)
             Loop
            N = clng(p * q)
            PHI = (p - 1) * (q - 1)
            E = clng(GCD(PHI))
            D = clng(Euler(E, PHI))
        Loop
        KeyEnc = E & "," & N
        KeyDec = D & "," & N
        genKey=E & "," & D & "," & N
    End Function
    Public Function setKey(ByVal a_sKey)
        Dim aKeys : aKeys=Split(a_sKey,",")
        setKey=false
        KeyEnc=null
        KeyDec=null
        If UBound(aKeys)<2 Then Exit Function End If
        KeyEnc=aKeys(0) & "," & aKeys(2)
        KeyDec=aKeys(1) & "," & aKeys(2)
        setKey=true
    End Function
End Class
%><%
' Compiled by Lewis Edward Moten III
' lewis@moten.com
' http://www.lewismoten.com
' Wednesday, May 09, 2001 05:42 PM GMT +5
' RSA Encryption Class
'
' .KeyEnc
'  Key for others to encrypt data with.
'
' .KeyDec
'  Your personal private key.  Keep this hidden.
'
' .KeyMod
'  Used with both public and private keys when encrypting and decrypting data.
'
' .KeyGen
'  Used to generate both public and private keys for encrypting and decrypting data.
'
' .Encode(pStrMessage)
'  Encrypts message and returns in numeric format
'
' .Decode(pStrMessage)
'  Decrypts message and returns a string
'
Class TRSA
    Public KeyEnc
    Public KeyDec
    Private Function Mult(ByVal x, ByVal pg, ByVal m)
        dim y : y=1
 
        Do While pg > 0
         Do While (pg / 2) = Int((pg / 2))
             x = nMod((x * x), m)
             pg = pg / 2
         Loop
         y = nMod((x * y), m)
         pg = pg - 1
        Loop
        Mult = y
    End Function
    Private Function nMod(x, y)
        nMod = 0
        if y = 0 then Exit Function End If
        nMod = x - (Int(x / y) * y)
    End Function
    Private Function Euler(E3, PHI3)
     'genetates D from (E and PHI) using the Euler algorithm
     On Error Resume Next
     Dim u1, u2, u3, v1, v2, v3, q
     Dim t1, t2, t3, z, vv, inverse
     u1 = 1
     u2 = 0
     u3 = PHI3
     v1 = 0
     v2 = 1
     v3 = E3
     Do Until (v3 = 0)
         q = Int(u3 / v3)
         t1 = u1 - q * v1: t2 = u2 - q * v2: t3 = u3 - q * v3
         u1 = v1: u2 = v2: u3 = v3
         v1 = t1: v2 = t2: v3 = t3
         z = 1
     Loop
     If (u2 < 0) Then
         inverse = u2 + PHI3
     Else
         inverse = u2
     End If
     Euler = inverse
    End Function
    Private Function GCD(nPHI)
     On Error Resume Next
     Dim nE, y
     Const N_UP = 99999999 'set upper limit of random number For E
     Const N_LW = 10000000 'set lower limit of random number For E
     Randomize
     nE = Int((N_UP - N_LW + 1) * Rnd + N_LW)
  Do
      x = nPHI Mod nE
      y = x Mod nE
      If y <> 0 And IsPrime(nE) Then
          GCD = nE
          Exit Function
      Else
          nE = nE + 1
      End If
  Loop
    End Function
    Private Function IsPrime(lngNumber)
        On Error Resume Next
        Dim lngCount, ngSqr
        Dim x
        lngSqr = Int(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 NumberToHex(ByRef pLngNumber, ByRef pLngLength)
        NumberToHex = Right(String(pLngLength, "0") & Hex(pLngNumber), pLngLength)
    End Function
    Private Function HexToNumber(ByRef pStrHex)
        HexToNumber = CLng("&h" & pStrHex)
    End Function
    Public Function Encrypt(ByVal tIp)
        Dim encSt, z
        Dim strMult
        Dim iEnc, iMod
        Dim aKey : aKey=Split(KeyEnc,",")
        If tIp = "" Then Exit Function End If
        iEnc=Int(aKey(0))
        iMod=Int(aKey(1))
        For z = 1 To Len(tIp)
            encSt = encSt & NumberToHex(Mult(CLng(Asc(Mid(tIp, z, 1))), iEnc, iMod),8)
        Next
 
        Encrypt = encSt
    End Function
    Public Function Decrypt(ByVal tIp)
        Dim decSt, z
        Dim iDec, iMod
        Dim aKey : aKey=Split(KeyDec,",")
        if Len(tIp) Mod 8 <> 0 then Exit Function End If
       iDec=Int(aKey(0))
       iMod=Int(aKey(1))
 
        For z = 1 To Len(tIp) Step 8
            decSt = decSt + Chr(Mult(HexToNumber(Mid(tIp, z, 8)), iDec, iMod))
        Next
 
        Decrypt = decSt
    End Function
    Public Function genKey()
        'Generates the keys for E, D and N
        Dim E, D, N, p, q
        Const PQ_UP = 9999 'set upper limit of random number
        Const PQ_LW = 3170 'set lower limit of random number
        Const KEY_LOWER_LIMIT  = 10000000 'set For 64bit minimum
        p = 0: q = 0
        Randomize
        Do Until D > KEY_LOWER_LIMIT 'makes sure keys are 64bit minimum
            Do Until IsPrime(p) And IsPrime(q) ' make sure q and q are primes
                p = clng((PQ_UP - PQ_LW + 1) * Rnd + PQ_LW)
                q = clng((PQ_UP - PQ_LW + 1) * Rnd + PQ_LW)
             Loop
            N = clng(p * q)
            PHI = (p - 1) * (q - 1)
            E = clng(GCD(PHI))
            D = clng(Euler(E, PHI))
        Loop
        KeyEnc = E & "," & N
        KeyDec = D & "," & N
        genKey=E & "," & D & "," & N
    End Function
    Public Function setKey(ByVal a_sKey)
        Dim aKeys : aKeys=Split(a_sKey,",")
        setKey=false
        KeyEnc=null
        KeyDec=null
        If UBound(aKeys)<2 Then Exit Function End If
        KeyEnc=aKeys(0) & "," & aKeys(2)
        KeyDec=aKeys(1) & "," & aKeys(2)
        setKey=true
    End Function
End Class
%>

标签:rsa,加密,算法,解密
0
投稿

猜你喜欢

  • javascript 跨浏览器的事件系统

    2010-07-28 19:34:00
  • xWin之JS版

    2009-09-12 18:45:00
  • python定时关机小脚本

    2022-09-24 23:38:21
  • OpenCV使用KNN完成OCR手写体识别

    2022-02-17 12:16:21
  • python去除删除数据中\\u0000\\u0001等unicode字符串的代码

    2023-09-04 09:15:51
  • Python if else语句对缩进的要求

    2021-03-07 03:32:12
  • Python使用socket模块实现简单tcp通信

    2021-04-20 14:54:14
  • 搞定MySQL数据库中文模糊检索问题

    2007-09-17 12:36:00
  • Mysql 插入中文及中文查询 (修改+调试)

    2024-01-21 23:52:42
  • pandas:get_dummies()与pd.factorize()的用法及区别说明

    2023-07-10 22:17:32
  • asp连接SQL和Access数据代码(asp里的随机函数)

    2013-06-01 20:26:06
  • IE下的firebug方法

    2009-07-29 18:50:00
  • ASP 使用Filter函数来检索数组

    2011-04-30 16:49:00
  • SQL server分页的4种方法示例(很全面)

    2024-01-27 15:09:47
  • 浅谈如何使用Python控制手机(一)

    2023-03-11 14:18:47
  • 详解Vue2.0里过滤器容易踩到的坑

    2024-05-09 10:40:46
  • 数据库主键的故事

    2008-05-31 07:50:00
  • Python二叉搜索树与双向链表转换算法示例

    2023-07-17 22:49:42
  • JQuery的Ajax请求实现局部刷新的简单实例

    2024-05-02 17:05:17
  • 阿里巴巴技术文章分享 Javascript继承机制的实现

    2024-04-30 09:59:16
  • asp之家 网络编程 m.aspxhome.com