<%
' Compiled by Lewis Edward Moten III
' 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
response.write("x("&x&")-pg("&pg&")-m("&m&")")
Loop
y = nMod((x * y), m)
response.write("======y("&y&")-<br>")
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)
dim str
response.write "pLngNumber:"&pLngNumber&"--"
response.write "pLngLength:"&pLngLength&"--"
str1=Hex(pLngNumber)
response.write "hex:"&str1&"--"
str2=String(pLngLength, "0")
response.write "string:"&str2&"--"
str=str1+str2
response.write "hex+string:"&str&"--"
str=right(str,pLngLength)
response.write "right:"&str&"--"
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,",")
dim str
If tIp = "" Then Exit Function End If
iEnc=Int(aKey(0))
iMod=Int(aKey(1))
For z = 1 To Len(tIp)
str=Mid(tIp, z, 1)
response.write "1:mid:"&str&" <br>"
str=Asc(str)
response.write "2:asc:"&str&" <br>"
str=CLng(str)
response.write "3:clng:"&str&" <br>"
str=Mult(str,ienc,imod)
response.write "4:mult:"&str&" <br>"
str=NumberToHex(str,8)
response.write "5:numbertohex:"&str&" <br>"
encSt =encSt &str
' encSt = encSt & NumberToHex(Mult(CLng(Asc(Mid(tIp, z, 1))), iEnc, iMod),8)
Next
response.write "6:encSt :"&encSt &" <br>"
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
set a=new TRSA
//取密钥
miyao=a.genKey()
//设置密钥
a.setKey(miyao)
ekey=a.KeyEnc
dkey=a.KeyDec
response.write("公钥:"&ekey&"--密钥:"&dkey&"<br>")
pwd="#"
miwen=a.Encrypt(pwd)
response.write("--密文:"&miwen&"<br>")
mingwen=a.Decrypt(miwen)
response.write("--明文:"&mingwen&"<br>")
%>
<script>
/*
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
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
'response.write("x("&x&")-pg("&pg&")-")
Loop
y = nMod((x * y), m)
'response.write("y("&y&")-")
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
*/
var KeyEnc="<%=a.KeyEnc %>";
function Encrypt(tIp){
var encSt, z;
var strMult;
var iEnc, iMod;
var aKey ;
aKey=KeyEnc.split(",");
var str;
if (tIp==""){return;}
iEnc=parseInt(aKey[0]);
iMod=parseInt(aKey[1]);
for (var z = 0 ;z< tIp.length;z++){
str=tIp.substring(z,z+1);
if (z==0) console.log ("1:mid:"+str+" <br>")
str=str.charCodeAt(0)
if (z==0) console.log ("1:asc:"+str+" <br>")
str=parseInt(str)
if (z==0) console.log ("1:clng:"+str+" <br>")
str=Mult(str,iEnc,iMod)
if (z==0) console.log ("1:mult:"+str+" <br>")
str=NumberToHex(str,8)
// if (z==0) console.log ("1:numbertohex:"+str+" <br>")
/* encSt = encSt & NumberToHex(Mult(CLng(Asc(Mid(tIp, z, 1))), iEnc, iMod),8)
*/
}
console.log ("6:encSt:"+str+" <br>")
return encSt;
}
function Mult( x, pg, m){
var y=1;
while (pg > 0){
while ((pg / 2) == parseInt(pg / 2)){
x = nMod((x * x), m);
pg = pg / 2;
console.log("x("+x+")-pg("+pg+")-m("+m+")")
}
y = nMod((x * y), m);
console.log("=========y("+y+")-")
pg = pg - 1;
}
return y;
}
function nMod(x, y){
if (y == 0) return;
return (x - (parseInt(x / y) * y));
}
function NumberToHex( pLngNumber,pLngLength){
var str;
console.log( "pLngNumber:"+pLngNumber+"--")
console.log( "pLngLength:"+pLngLength+"--")
str1=decimalToHexString(pLngNumber)
console.log( "hex:"+str1+"--")
var hex0="";
for (var i=0;i<pLngLength-pLngNumber.length;i++){
hex0+="0"+"";
}
/* str2=String(pLngLength, "0")
console.log( "string:"+str2+"--")
str=str1+str2
console.log( "hex+string:"+str+"--")
str=right(str,pLngLength)
console.log("right:"+str+"--")
// NumberToHex = Right(String(pLngLength, "0") & Hex(pLngNumber), pLngLength);
*/
return ""+hex0+str1;
}
function decimalToHexString(number)
{
if (number < 0)
{
number = 0xFFFFFFFF + number + 1;
}
return number.toString(16).toUpperCase();
}
Encrypt("<%=pwd%>");
</script>