求助:ASP可逆加密算法

Jesses41 2007-07-24 05:45:20
如题,能加密解密包含中文,数字,符号,英文的字符串~!

谢谢了~!
...全文
290 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxasp 2007-07-27
  • 打赏
  • 举报
回复
帖子长度限制,只好发个精简的!
<%
Class clsCookieX
Private ErrMsg,bHasErr
Private sBASE_64_CHARACTERS
Private Sub Class_Initialize()
ErrMsg="":bHasErr=False
sBASE_64_CHARACTERS = "aT@zpMme5DHWQ4ER37dvgPYxu9JcB1ULw6sKihCljIqnSyk2VfOr8GFX0A!tNZob"
sBASE_64_CHARACTERS = strUnicode2Ansi(sBASE_64_CHARACTERS)
End Sub
Private Sub Class_Terminate()
End Sub
Private Sub AddErr(s)
ErrMsg=ErrMsg&"Cookie操作出错信息 - "&s&HTML_BR
bHasErr=True
End Sub
Public Function GetErr()
GetErr=ErrMsg
End Function
Public Sub ErrClear()
ErrMsg=""
bHasErr=False
End Sub
Public Property Get HasErr()
HasErr=bHasErr
End Property
Public Property Let Item(k,v)
Response.Cookies(encode(k))=encode(v)
End Property
Public Default Property Get Item(k)
Item=decode(Request.Cookies(encode(k)))
End Property
Public Function Remove(k)
k=encode(k)
Response.Cookies(k)=""
Response.Cookies(k).Expires=DateAdd("d",-1,Now)
End Function
Public Property Let Expires(k,v)
Response.Cookies(encode(k)).Expires=v
End Property
Public Function Exists(k)
k=encode(k)
If Len(Request.Cookies(k))=0 Then
Exists=False
Else
Exists=True
End If
End Function
Function encode(v)
encode=strAnsi2Unicode(Base64encode(strUnicode2Ansi(v)))
End Function
Function decode(v)
decode=strAnsi2Unicode(Base64decode(strUnicode2Ansi(v)))
End Function
Function strUnicodeLen(asContents)
Dim len1, k, i, Asc1, asContents1
asContents1 = "a" & asContents
len1 = Len(asContents1)
k = 0
For i = 1 To len1
Asc1 = Asc(Mid(asContents1, i, 1))
If Asc1 < 0 Then Asc1 = 65536 + Asc1
If Asc1 > 255 Then
k = k + 2
Else
k = k + 1
End If
Next
strUnicodeLen = k - 1
End Function
Function strUnicode2Ansi(asContents)
Dim len1, k, i, Asc1, varchar, varasc, varhex, varlow, varhigh
strUnicode2Ansi = ""
len1 = Len(asContents)
For i = 1 To len1
varchar = Mid(asContents, i, 1)
varasc = Asc(varchar)
If varasc < 0 Then varasc = varasc + 65536
If varasc > 255 Then
varhex = Hex(varasc)
varlow = Left(varhex, 2)
varhigh = Right(varhex, 2)
strUnicode2Ansi = strUnicode2Ansi & ChrB("&H" & varlow) & ChrB("&H" & varhigh)
Else
strUnicode2Ansi = strUnicode2Ansi & ChrB(varasc)
End If
Next
End Function
Function strAnsi2Unicode(asContents)
Dim len1, k, i, Asc1, varchar, varasc, varhex, varlow, varhigh
strAnsi2Unicode = ""
len1 = LenB(asContents)
If len1 = 0 Then Exit Function
For i = 1 To len1
varchar = MidB(asContents, i, 1)
varasc = AscB(varchar)
If varasc > 127 Then
strAnsi2Unicode = strAnsi2Unicode & Chr(AscW(MidB(asContents, i + 1, 1) & varchar))
i = i + 1
Else
strAnsi2Unicode = strAnsi2Unicode & Chr(varasc)
End If
Next
End Function
Function Base64encode(asContents)
Dim lnPosition
Dim lsResult
Dim Char1
Dim Char2
Dim Char3
Dim Char4
Dim Byte1
Dim Byte2
Dim Byte3
Dim SaveBits1
Dim SaveBits2
Dim lsGroupBinary
Dim lsGroup64
Dim M3, len1, len2
len1 = LenB(asContents)
If len1 < 1 Then
Base64encode = ""
Exit Function
End If
M3 = len1 Mod 3
If M3 > 0 Then asContents = asContents & String(3 - M3, ChrB(0))
If M3 > 0 Then
len1 = len1 + (3 - M3)
len2 = len1 - 3
Else
len2 = len1
End If
lsResult = ""
For lnPosition = 1 To len2 Step 3
lsGroup64 = ""
lsGroupBinary = MidB(asContents, lnPosition, 3)
Byte1 = AscB(MidB(lsGroupBinary, 1, 1)): SaveBits1 = Byte1 And 3
Byte2 = AscB(MidB(lsGroupBinary, 2, 1)): SaveBits2 = Byte2 And 15
Byte3 = AscB(MidB(lsGroupBinary, 3, 1))
Char1 = MidB(sBASE_64_CHARACTERS, ((Byte1 And 252) \ 4) + 1, 1)
Char2 = MidB(sBASE_64_CHARACTERS, (((Byte2 And 240) \ 16) Or (SaveBits1 * 16) And &HFF) + 1, 1)
Char3 = MidB(sBASE_64_CHARACTERS, (((Byte3 And 192) \ 64) Or (SaveBits2 * 4) And &HFF) + 1, 1)
Char4 = MidB(sBASE_64_CHARACTERS, (Byte3 And 63) + 1, 1)
lsGroup64 = Char1 & Char2 & Char3 & Char4
lsResult = lsResult & lsGroup64
Next
If M3 > 0 Then
lsGroup64 = ""
lsGroupBinary = MidB(asContents, len2 + 1, 3)
Byte1 = AscB(MidB(lsGroupBinary, 1, 1)): SaveBits1 = Byte1 And 3
Byte2 = AscB(MidB(lsGroupBinary, 2, 1)): SaveBits2 = Byte2 And 15
Byte3 = AscB(MidB(lsGroupBinary, 3, 1))
Char1 = MidB(sBASE_64_CHARACTERS, ((Byte1 And 252) \ 4) + 1, 1)
Char2 = MidB(sBASE_64_CHARACTERS, (((Byte2 And 240) \ 16) Or (SaveBits1 * 16) And &HFF) + 1, 1)
Char3 = MidB(sBASE_64_CHARACTERS, (((Byte3 And 192) \ 64) Or (SaveBits2 * 4) And &HFF) + 1, 1)
If M3 = 1 Then
lsGroup64 = Char1 & Char2 & ChrB(39) & ChrB(39)
Else
lsGroup64 = Char1 & Char2 & Char3 & ChrB(39)
End If
lsResult = lsResult & lsGroup64
End If
Base64encode = lsResult
End Function
Function Base64decode(asContents)
Dim lsResult
Dim lnPosition
Dim lsGroup64, lsGroupBinary
Dim Char1, Char2, Char3, Char4
Dim Byte1, Byte2, Byte3
Dim M4, len1, len2
len1 = LenB(asContents)
M4 = len1 Mod 4
If len1 < 1 Or M4 > 0 Then
Base64decode = ""
Exit Function
End If
If MidB(asContents, len1, 1) = ChrB(39) Then M4 = 3
If MidB(asContents, len1 - 1, 1) = ChrB(39) Then M4 = 2
If M4 = 0 Then
len2 = len1
Else
len2 = len1 - 4
End If
For lnPosition = 1 To len2 Step 4
lsGroupBinary = ""
lsGroup64 = MidB(asContents, lnPosition, 4)
Char1 = InStrB(sBASE_64_CHARACTERS, MidB(lsGroup64, 1, 1)) - 1
Char2 = InStrB(sBASE_64_CHARACTERS, MidB(lsGroup64, 2, 1)) - 1
Char3 = InStrB(sBASE_64_CHARACTERS, MidB(lsGroup64, 3, 1)) - 1
Char4 = InStrB(sBASE_64_CHARACTERS, MidB(lsGroup64, 4, 1)) - 1
Byte1 = ChrB(((Char2 And 48) \ 16) Or (Char1 * 4) And &HFF)
Byte2 = lsGroupBinary & ChrB(((Char3 And 60) \ 4) Or (Char2 * 16) And &HFF)
Byte3 = ChrB((((Char3 And 3) * 64) And &HFF) Or (Char4 And 63))
lsGroupBinary = Byte1 & Byte2 & Byte3
lsResult = lsResult & lsGroupBinary
Next
If M4 > 0 Then
lsGroupBinary = ""
lsGroup64 = MidB(asContents, len2 + 1, M4) & ChrB(65)
If M4 = 2 Then
lsGroup64 = lsGroup64 & ChrB(65)
End If
Char1 = InStrB(sBASE_64_CHARACTERS, MidB(lsGroup64, 1, 1)) - 1
Char2 = InStrB(sBASE_64_CHARACTERS, MidB(lsGroup64, 2, 1)) - 1
Char3 = InStrB(sBASE_64_CHARACTERS, MidB(lsGroup64, 3, 1)) - 1
Char4 = InStrB(sBASE_64_CHARACTERS, MidB(lsGroup64, 4, 1)) - 1
Byte1 = ChrB(((Char2 And 48) \ 16) Or (Char1 * 4) And &HFF)
Byte2 = lsGroupBinary & ChrB(((Char3 And 60) \ 4) Or (Char2 * 16) And &HFF)
Byte3 = ChrB((((Char3 And 3) * 64) And &HFF) Or (Char4 And 63))
If M4 = 2 Then
lsGroupBinary = Byte1
ElseIf M4 = 3 Then
lsGroupBinary = Byte1 & Byte2
End If
lsResult = lsResult & lsGroupBinary
End If
Base64decode = lsResult
End Function
End Class
Dim startime
startime=Timer
Response.Write "<pre>"
Dim c
Set c=New clsCookieX
c("hellow")="简体中文abc123我在!"
c("hellow1")="简体中文abc123我"
c("一二三四")="壹仟贰佰叁拾肆"
For i=1 To 10
Response.Write c("t"&i)
Next
Response.Write c("hellow")
Response.Write c("hellow1")
Response.Write "<hr>"
Response.Write Server.HTMLEncode(Request.ServerVariables("ALL_RAW"))
c.Remove("hellow")
c.Remove("hellow1")
Response.Write c.Exists("hellow1")
Response.Write FormatNumber((Timer-startime)*1000,3)
Response.Write "</pre>"
%>
dingmin 2007-07-27
  • 打赏
  • 举报
回复
rsa.asp
<%

Class clsRSA

Public PrivateKey
Public PublicKey
Public Modulus



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



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



test.asp
<!--#INCLUDE FILE="RSA.asp"-->
<%
function Encryptstr(Message)
Dim LngKeyE
Dim LngKeyD
Dim LngKeyN
Dim StrMessage
Dim ObjRSA


LngKeyE = "32823"
LngKeyD = "20643"
LngKeyN = "29893"
StrMessage = Message

Set ObjRSA = New clsRSA


ObjRSA.PublicKey = LngKeyE
ObjRSA.Modulus = LngKeyN
Encryptstr = ObjRSA.Encode(StrMessage)
Set ObjRSA = Nothing
end function




function decryptstr(Message)
Dim LngKeyE
Dim LngKeyD
Dim LngKeyN
Dim StrMessage
Dim ObjRSA


LngKeyE = "32823"
LngKeyD = "20643"
LngKeyN = "29893"
StrMessage = Message

Set ObjRSA = New clsRSA

ObjRSA.PrivateKey =LngKeyD
ObjRSA.Modulus=LngKeyN
decryptstr=ObjRSA.Decode(StrMessage)
Set ObjRSA = Nothing
end function



dim last,first
first="sohu"
Response.Write "加密前为:"&first
last=Encryptstr(first)
Response.Write "加密后为"&last
Response.Write "解密后为" &decryptstr(last)

%>


~~~~~~~~~~~~~~~~~~在ASP中实现加密与解密,加密方法:根据RSA
ihweb 2007-07-27
  • 打赏
  • 举报
回复
可逆,方法很多。移位,疑惑,base64,都行。
Jesses41 2007-07-27
  • 打赏
  • 举报
回复
具体点哈,我菜
UPUP
最好能给出代码哈
小逗狗 2007-07-24
  • 打赏
  • 举报
回复
你可以使用异或加密算法

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧