当前位置: 移动技术网 > IT编程>开发语言>Asp > 在asp中通过vbs类实现rsa加密与解密的代码

在asp中通过vbs类实现rsa加密与解密的代码

2017年12月12日  | 移动技术网IT编程  | 我要评论
在asp中通过vbs类实现rsa加密与解密,建议入精华 本文章有两文件组成 test.asp 测试演示文件 clsrsa.asp 实现rsa加密与解密的
在asp中通过vbs类实现rsa加密与解密,建议入精华

本文章有两文件组成
test.asp 测试演示文件
clsrsa.asp 实现rsa加密与解密的vbs类文件
下面是代码:

1. test.asp 

复制代码 代码如下:

<% 
rem 文章标题:在asp中通过vbs类实现rsa加密与解密 
rem 收集整理:yanek 
rem 联系:aspboy@263.net 

%> 
<%option explicit%> 
<!--#include file="clsrsa.asp"--> 
<% 

dim lngkeye 
dim lngkeyd 
dim lngkeyn 
dim strmessage 
dim objrsa 
if not request.form = "" then 

    lngkeye = request.form("keye") 
    lngkeyd = request.form("keyd") 
    lngkeyn = request.form("keyn") 
    strmessage = request.form("message") 

    set objrsa = new clsrsa 

    select case request.form("action") 
        case "generate keys" 
            call objrsa.genkey() 
            lngkeye = objrsa.publickey 
            lngkeyd = objrsa.privatekey 
            lngkeyn = objrsa.modulus 
        case "encrypt" 
            objrsa.publickey = lngkeye 
            objrsa.modulus = lngkeyn 
            strmessage = objrsa.encode(strmessage) 
        case "decrypt" 
            objrsa.privatekey = lngkeyd 
            objrsa.modulus = lngkeyn 
            strmessage = objrsa.decode(strmessage) 
    end select 

    set objrsa = nothing 

end if 
%> 
<html> 
    <head> 
        <title>rsa cipher demonstration</title> 
    </head> 
    <body> 
        <h1>rsa cipher demonstration</h1> 
        <p> 
            you will first need to generate your public/privage key-pair 
            before you can encrypt/decrypt messages. 
        </p> 
        <form method="post"> 
            <table> 
                <tr> 
                    <td>public key</td> 
                    <td><input name="keye" value="<%=server.htmlencode(lngkeye)%>"></td> 
                    <td rowspan="3"> 
                        <input type="submit" name="action" value="generate keys"> 
                    </td> 
                </tr> 
                <tr> 
                    <td>private key</td> 
                    <td><input name="keyd" value="<%=server.htmlencode(lngkeyd)%>"></td> 
                </tr> 
                <tr> 
                    <td>modulus</td> 
                    <td><input name="keyn" value="<%=server.htmlencode(lngkeyn)%>"></td> 
                </tr> 
                <tr> 
                    <td colspan="3"> 
                        test message:<br> 
                        <textarea name="message" cols="50" rows="7"><%=server.htmlencode(strmessage)%></textarea> 
                    </td> 
                </tr> 
                <tr> 
                    <td align="right" colspan="3"> 
                        <input type="submit" name="action" value="encrypt"> 
                        <input type="submit" name="action" value="decrypt"> 
                    </td> 
                </tr> 
            </table> 
        </form> 
            </body> 
</html> 
    


clsrsa.asp  

复制代码 代码如下:

<% 
rem 实现rsa加密与解密的vbs类文件 
rem 文章标题:在asp中通过vbs类实现rsa加密与解密 
rem 收集整理:yanek 
rem 联系:aspboy@263.net 

' rsa encryption class 

' .privatekey 
'        your personal private key.  keep this hidden. 

' .publickey 
'        key for others to encrypt data with. 

' .modulus 
'        used with both public and private keys when encrypting 
'        and decrypting data. 

' .genkey() 
'        creates public/private key set and modulus 

' .crypt(plngmessage, plngkey)   
'        encrypts/decrypts message and returns  
'        as a string. 

' .encode(pstrmessage) 
'        encrypts message and returns in double-hex format 

' .decode(pstrmessage) 
'        decrypts message from double-hex format and returns a string 

class clsrsa 

    public privatekey 
    public publickey 
    public modulus 

    public sub genkey() 
        dim llngphi 
        dim q 
        dim p 

        randomize 

        do 
            do 

                ' 2 random primary numbers (0 to 1000) 
                do 
                    p = rnd * 1000 \ 1 
                loop while not isprime(p) 

                do 
                    q = rnd * 1000 \ 1 
                loop while not isprime(q) 

                 
                ' n = product of 2 primes 
                modulus = p * q \ 1 

                ' random decryptor (2 to n) 
                privatekey = rnd * (modulus - 2) \ 1 + 2 

                llngphi = (p - 1) * (q - 1) \ 1 
                publickey = euler(llngphi, privatekey) 

            loop while publickey = 0 or publickey = 1 

        ' loop if we can't crypt/decrypt a byte     
        loop while not testcrypt(255) 

    end sub 

    private function testcrypt(byref pbytdata) 
        dim lstrcrypted 
        lstrcrypted = crypt(pbytdata, publickey) 
        testcrypt = crypt(lstrcrypted, privatekey) = pbytdata 
    end function 

    private function euler(byref plngphi, byref plngkey) 

        dim llngr(3) 
        dim llngp(3) 
        dim llngq(3) 

        dim llngcounter 
        dim llngresult 

        euler = 0 

        llngr(1) = plngphi: llngr(0) = plngkey 
        llngp(1) = 0: llngp(0) = 1 
        llngq(1) = 2: llngq(0) = 0 

        llngcounter = -1 

        do until llngr(0) = 0 

            llngr(2) = llngr(1): llngr(1) = llngr(0) 
            llngp(2) = llngp(1): llngp(1) = llngp(0) 
            llngq(2) = llngq(1): llngq(1) = llngq(0) 

            llngcounter = llngcounter + 1 

            llngr(0) = llngr(2) mod llngr(1) 
            llngp(0) = ((llngr(2)\llngr(1)) * llngp(1)) + llngp(2) 
            llngq(0) = ((llngr(2)\llngr(1)) * llngq(1)) + llngq(2) 

        loop 

        llngresult = (plngkey * llngp(1)) - (plngphi * llngq(1)) 

        if llngresult > 0 then 
            euler = llngp(1) 
        else 
            euler = abs(llngp(1)) + plngphi 
        end if 

    end function 

    public function crypt(plngmessage, plngkey) 
        on error resume next 
        dim llngmod 
        dim llngresult 
        dim llngindex 
        if plngkey mod 2 = 0 then 
            llngresult = 1 
            for llngindex = 1 to plngkey / 2 
                llngmod = (plngmessage ^ 2) mod modulus 
                ' mod may error on key generation 
                llngresult = (llngmod * llngresult) mod modulus  
                if err then exit function 
            next 
        else 
            llngresult = plngmessage 
            for llngindex = 1 to plngkey / 2 
                llngmod = (plngmessage ^ 2) mod modulus 
                on error resume next 
                ' mod may error on key generation 
                llngresult = (llngmod * llngresult) mod modulus 
                if err then exit function 
            next 
        end if 
        crypt = llngresult 
    end function 

    private function isprime(byref plngnumber) 
        dim llngsquare 
        dim llngindex 
        isprime = false 
        if plngnumber < 2 then exit function 
        if plngnumber mod 2 = 0 then exit function 
        llngsquare = sqr(plngnumber) 
        for llngindex = 3 to llngsquare step 2 
            if plngnumber mod llngindex = 0 then exit function 
        next 
        isprime = true 
    end function 

    public function encode(byval pstrmessage) 
        dim llngindex 
        dim llngmaxindex 
        dim lbytascii 
        dim llngencrypted 
        llngmaxindex = len(pstrmessage) 
        if llngmaxindex = 0 then exit function 
        for llngindex = 1 to llngmaxindex 
            lbytascii = asc(mid(pstrmessage, llngindex, 1)) 
            llngencrypted = crypt(lbytascii, publickey) 
            encode = encode & numbertohex(llngencrypted, 4) 
        next 
    end function 

    public function decode(byval pstrmessage) 
        dim lbytascii 
        dim llngindex 
        dim llngmaxindex 
        dim llngencrypteddata 
        decode = "" 
        llngmaxindex = len(pstrmessage) 
        for llngindex = 1 to llngmaxindex step 4 
            llngencrypteddata = hextonumber(mid(pstrmessage, llngindex, 4)) 
            lbytascii = crypt(llngencrypteddata, privatekey) 
            decode = decode & chr(lbytascii) 
        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 

end class 
%> 
演示地址:http://www.cnaspol.com/myrsa/test.asp

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网