28,391
社区成员
发帖
与我相关
我的任务
分享
CONST BASE_64_MAP="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Dim MapEnc(63)
Dim MapDec(127)
Dim i
For i=0 To 63
MapEnc(i)=Mid(BASE_64_MAP,i+1,1)
Next
For i=0 To 63
MapDec(Asc(MapEnc(i)))=i
Next
Set i=Nothing
Private Function DecToBin(intDecimal)
Dim strBinary,blnFlag
strBinary=""
blnFlag=True
Do While blnFlag
strBinary=Cstr(intDecimal AND &H01)&strBinary
intDecimal=intDecimal\2
If intDecimal=0 Then blnFlag=False
Loop
Set blnFlag=Nothing
strBinary=Right("00000000"&strBinary,8)
DecToBin=strBinary
Set strBinary=Nothing
End Function
Private Function BinToDec(strBin)
Dim intDec,i,j
intDec=0
j=Len(strBin)
For i=1 To j
intDec=intDec+2^(j-i)*CInt(Mid(strBin,i,1))
Next
Set i=Nothing
Set j=Nothing
BinToDec=intDec
Set intDec=Nothing
End Function
Private Function Bin24Encode(strBin24)
Dim strEncoder,strBin6
strEncoder=""
If (Len(strBin24)<=8) Then
strBin24=Left(strBin24&"00000000",8)
strBin6="00"&Mid(strBin24,1,6)
strEncoder=MapEnc(BinToDec(strBin6))
strBin6="00"&Mid(strBin24,7,2)&"0000"
strEncoder=strEncoder&MapEnc(BinToDec(strBin6))
strEncoder=strEncoder&"=="
Else If (Len(strBin24)<=16) Then
strBin24=Left(strBin24&"00000000",16)
strBin6="00"&Mid(strBin24,1,6)
strEncoder=MapEnc(BinToDec(strBin6))
strBin6="00"&Mid(strBin24,7,6)
strEncoder=strEncoder&MapEnc(BinToDec(strBin6))
strBin6="00"&Mid(strBin24,13,4)&"00"
strEncoder=strEncoder&MapEnc(BinToDec(strBin6))
strEncoder=strEncoder&"="
Else
strBin24=Left(strBin24&"00000000",24)
strBin6="00"&Mid(strBin24,1,6)
strEncoder=MapEnc(BinToDec(strBin6))
strBin6="00"&Mid(strBin24,7,6)
strEncoder=strEncoder&MapEnc(BinToDec(strBin6))
strBin6="00"&Mid(strBin24,13,6)
strEncoder=strEncoder&MapEnc(BinToDec(strBin6))
strBin6="00"&Mid(strBin24,19,6)
strEncoder=strEncoder&MapEnc(BinToDec(strBin6))
End If
End If
Set strBin6=Nothing
Bin24Encode=strEncoder
Set strEncoder=Nothing
End Function
Private Function HexToDec(strHex)
Dim intDec,i,j
j=Len(strHex)
intDec=0
For i=1 To j
Select Case Mid(strHex,i,1)
Case "0" intDec=intDex+16^(j-i)*0
Case "1" intDec=intDex+16^(j-i)*1
Case "2" intDec=intDex+16^(j-i)*2
Case "3" intDec=intDex+16^(j-i)*3
Case "4" intDec=intDex+16^(j-i)*4
Case "5" intDec=intDex+16^(j-i)*5
Case "6" intDec=intDex+16^(j-i)*6
Case "7" intDec=intDex+16^(j-i)*7
Case "8" intDec=intDex+16^(j-i)*8
Case "9" intDec=intDex+16^(j-i)*9
Case "A" intDec=intDex+16^(j-i)*10
Case "B" intDec=intDex+16^(j-i)*11
Case "C" intDec=intDex+16^(j-i)*12
Case "D" intDec=intDex+16^(j-i)*13
Case "E" intDec=intDex+16^(j-i)*14
Case "F" intDec=intDex+16^(j-i)*15
End Select
Next
Set i=Nothing
HexToDec=intDec
Set intDec=Nothing
End Function
Private Function HexToBin(strHex)
Dim strBin,i,j
j=Len(strHex)
strBin=""
For i=1 To j
Select Case Mid(strHex,i,1)
Case "0" strBin=strBin&"0000"
Case "1" strBin=strBin&"0001"
Case "2" strBin=strBin&"0010"
Case "3" strBin=strBin&"0011"
Case "4" strBin=strBin&"0100"
Case "5" strBin=strBin&"0101"
Case "6" strBin=strBin&"0110"
Case "7" strBin=strBin&"0111"
Case "8" strBin=strBin&"1000"
Case "9" strBin=strBin&"1001"
Case "A" strBin=strBin&"1010"
Case "B" strBin=strBin&"1011"
Case "C" strBin=strBin&"1100"
Case "D" strBin=strBin&"1101"
Case "E" strBin=strBin&"1110"
Case "F" strBin=strBin&"1111"
End Select
Next
Set i=Nothing
Set j=Nothing
HexToBin=strBin
Set strBin=Nothing
End Function
Private Function BinToHex(strBin)
Dim strHex,strBin4
strHex=""
Do While strBin<>""
strBin4=Mid(strBin,1,4)
If Len(strBin)>4 Then
strBin=Mid(strBin,5,Len(strBin)-4)
Else
strBin=""
End If
Select Case strBin4
Case "0000" strHex=strHex&"0"
Case "0001" strHex=strHex&"1"
Case "0010" strHex=strHex&"2"
Case "0011" strHex=strHex&"3"
Case "0100" strHex=strHex&"4"
Case "0101" strHex=strHex&"5"
Case "0110" strHex=strHex&"6"
Case "0111" strHex=strHex&"7"
Case "1000" strHex=strHex&"8"
Case "1001" strHex=strHex&"9"
Case "1010" strHex=strHex&"A"
Case "1011" strHex=strHex&"B"
Case "1100" strHex=strHex&"C"
Case "1101" strHex=strHex&"D"
Case "1110" strHex=strHex&"E"
Case "1111" strHex=strHex&"F"
End Select
Loop
Set strBin4=Nothing
BinToHex=strHex
Set strHex=Nothing
End Function
PUBLIC Function Encode(strText)
Dim strTemp24,strBinarySource,strCode,intAsc,strHex,i,j
strTemp24=""
strBinarySource=""
strCode=""
j=Clng(Len(strText))
For i=1 To j
intAsc=Asc(Mid(strText,i,1))
If intAsc>=0 AND intAsc<128 Then
strBinarySource=strBinarySource&DecToBin(intAsc)
Else
strHex=CStr(Hex(intAsc))
strHex=Right("0000"&strHex,4)
strBinarySource=strBinarySource & HexToBin(strHex)
End If
Next
Do While (strBinarySource<>"")
If Clng(Len(strBinarySource))>=24 Then
strTemp24=Mid(strBinarySource,1,24)
strBinarySource=Mid(strBinarySource,25,Len(strBinarySource)-24)
Else
strTemp24=Mid(strBinarySource,1,Len(strBinarySource))
strBinarySource=""
End If
strCode=strCode&Bin24Encode(strTemp24)
Loop
Set i=Nothing
Set j=Nothing
Set intAsc=Nothing
Set strTemp24=Nothing
Set strBinarySource=Nothing
Set i=Nothing
Encode=strCode
Set strCode=Nothing
End Function
PUBLIC Function Decode(strCode)
Dim i,j,strText,strBinarySource,strTemp8,intIndex
j=Clng(Len(strCode))
strText=""
strBinarySource=""
For i=1 To j
intIndex=MapDec(Asc(Mid(strCode,i,1)))
If Mid(strCode,i,1)<>"=" Then strBinarySource=strBinarySource&Right(DecToBin(intIndex),6)
Next
Do While (strBinarySource<>"")
If Len(strBinarySource)>8 Then
strTemp8=Mid(strBinarySource,1,8)
strBinarySource=Mid(strBinarySource,9,Len(strBinarySource)-8)
Else
If Len(strBinarySource)=8 Then strTemp8=Mid(strBinarySource,1,8)
strBinarySource=""
End If
If Mid(strTemp8,1,1)="0" Then
strText=strText&Chr(BinToDec(strTemp8))
Else
If Len(strBinarySource)>8 Then
strTemp8=strTemp8&Mid(strBinarySource,1,8)
strBinarySource=Mid(strBinarySource,9,Len(strBinarySource)-8)
strText=strText& Chr("&H"& BinToHex(strTemp8))
Else
If Len(strBinarySource)=8 Then
strTemp8=strTemp8&Mid(strBinarySource,1,8)
'response.write BinToHex(strTemp8)&"<br>"
strText=strText& Chr("&H" & BinToHex(strTemp8))
End If
strBinarySource=""
End If
End If
Loop
Set strBinarySource=Nothing
Set intIndex=Nothing
Set i=Nothing
Set j=Nothing
Decode=strText
Set strText=Nothing
End Function
OPTION EXPLICIT
const BASE_64_MAP_INIT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
dim newline
dim Base64EncMap(63)
dim Base64DecMap(127)
dim inp,hu,encode
call initCodecs '初始化
inp = "<WORD处理后的asp代码>" '将要保护的asp代码用WORD处理,然后填在此处
hu= base64Encode(inp) '调用函数base64Encode进行加密,得到密文hu
Response.Write(hu) '显示密文
PUBLIC SUB initCodecs() '初始化函数initCodecs
newline = "<P>" & chr(13) & chr(10)
dim max, idx
max = len(BASE_64_MAP_INIT)
for idx = 0 to max - 1
Base64EncMap(idx) = mid(BASE_64_MAP_INIT, idx + 1, 1)
next
for idx = 0 to max - 1
Base64DecMap(ASC(Base64EncMap(idx))) = idx
next
END SUB
PUBLIC FUNCTION base64Encode(plain) '加密函数base64Encode
if len(plain) = 0 then
base64Encode = ""
exit function
end if
dim ret, ndx, by3, first, second, third
by3 = (len(plain) \ 3) * 3
ndx = 1
do while ndx <= by3
first = asc(mid(plain, ndx+0, 1))
second = asc(mid(plain, ndx+1, 1))
third = asc(mid(plain, ndx+2, 1))
ret = ret & Base64EncMap( (first \ 4) AND 63 )
ret = ret & Base64EncMap( ((first * 16) AND 48) + ((second \ 16) AND 15 ) )
ret = ret & Base64EncMap( ((second * 4) AND 60) + ((third \ 64) AND 3 ) )
ret = ret & Base64EncMap( third AND 63)
ndx = ndx + 3
loop
if by3 < len(plain) then
first = asc(mid(plain, ndx+0, 1))
ret = ret & Base64EncMap( (first \ 4) AND 63 )
if (len(plain) MOD 3 ) = 2 then
second = asc(mid(plain, ndx+1, 1))
ret = ret & Base64EncMap( ((first * 16) AND 48) + ((second \ 16) AND 15 ) )
ret = ret & Base64EncMap( ((second * 4) AND 60) )
else
ret = ret & Base64EncMap( (first * 16) AND 48)
ret = ret '& "="
end if
ret = ret '& "="
end if
base64Encode = ret
END FUNCTION
Dim Hu,Hu2
'拷贝“欲保护asp代码”的密文 将之存放到Hu变量中
Hu=""
Hu2= base64Decode(hu) '还原要保护的ASP代码
execute(UnEncode(Hu2)) '还原单引号、回车换行,并执行原代码
'解密函数base64Decode
FUNCTION base64Decode(scrambled)
if len(scrambled) = 0 then
base64Decode = ""
exit function
end if
dim realLen
realLen = len(scrambled)
do while mid(scrambled, realLen, 1) = "="
realLen = realLen - 1
loop
dim ret, ndx, by4, first, second, third, fourth
ret = ""
by4 = (realLen \ 4) * 4
ndx = 1
do while ndx <= by4
first = Base64DecMap(asc(mid(scrambled, ndx+0, 1)))
second = Base64DecMap(asc(mid(scrambled, ndx+1, 1)))
third = Base64DecMap(asc(mid(scrambled, ndx+2, 1)))
fourth = Base64DecMap(asc(mid(scrambled, ndx+3, 1)))
ret = ret & chr( ((first * 4) AND 255) + ((second \ 16) AND 3))
ret = ret & chr( ((second * 16) AND 255) + ((third \ 4) AND 15))
ret = ret & chr( ((third * 64) AND 255) + (fourth AND 63))
ndx = ndx + 4
loop
if ndx < realLen then
first = Base64DecMap(asc(mid(scrambled, ndx+0, 1)))
second = Base64DecMap(asc(mid(scrambled, ndx+1, 1)))
ret = ret & chr( ((first * 4) AND 255) + ((second \ 16) AND 3))
if realLen MOD 4 = 3 then
third = Base64DecMap(asc(mid(scrambled,ndx+2,1)))
ret = ret & chr( ((second * 16) AND 255) + ((third \ 4) AND 15))
end if
end if
base64Decode = ret
END FUNCTION
'还原单引号、回车换行函数UnEncode
function UnEncode(cc)
for i = 1 to len(cc)
if mid(cc,i,1)<> "水" then
if mid(cc,i,1)="加" then
temp = """" & temp
else
temp = Mid(cc, i, 1) + temp
end if
else
temp=newline&temp
end if
next
UnEncode=temp
end function