国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > ASP > 正文

在asp中通過vbs類實現rsa加密與解密

2024-05-04 11:06:18
字體:
來源:轉載
供稿:網友
本文章有兩文件組成
test.asp 測試演示文件
clsrsa.asp 實現rsa加密與解密的vbs類文件
下面是代碼:

1. test.asp

<%
rem 文章標題:在asp中通過vbs類實現rsa加密與解密
rem 收集整理:yanek
rem 聯系:[email protected]

%>
<%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 聯系:[email protected]

' 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 testcry


|||pt(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
%>




發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清新县| 屏南县| 嘉祥县| 湾仔区| 仙桃市| 仙游县| 新泰市| 广汉市| 鄂托克前旗| 禄劝| 平安县| 西吉县| 彩票| 金乡县| 磐安县| 潞城市| 象山县| 武清区| 西宁市| 杭州市| 丹江口市| 什邡市| 遂宁市| 周口市| 新兴县| 巴林右旗| 启东市| 丽水市| 佛学| 隆安县| 平湖市| 新营市| 岳普湖县| 吴桥县| 贵港市| 温宿县| 富阳市| 原阳县| 溆浦县| 兖州市| 依兰县|