当前位置:编程学习 > asp >>

ASP字符串加密解密代码实例

答案:

 

<<html>
<head>
<title>对字符串加密
</title>
</head>
<body>
<%
Const g_CryptThis = "下面对本句话进行加密了!"
Const g_KeyLocation = "c:\key.txt"
g_Key = mid(ReadKeyFromFile(g_KeyLocation),1,Len(g_CryptThis))
Response.Write "<p>原文: " & g_CryptThis & "<p>"
Response.Write "<p>密钥: " & g_Key & "<p>"
Response.Write "<p>加密: " & EnCrypt(g_CryptThis) & "<p>"
Response.Write "<p>解密: " & DeCrypt(EnCrypt(g_CryptThis)) & "<p>"
Function EnCrypt(strCryptThis)
Dim strChar, iKeyChar, iStringChar, I
for I = 1 to Len(strCryptThis)
iKeyChar = Asc(mid(g_Key,I,1))
iStringChar = Asc(mid(strCryptThis,I,1))
iCryptChar = iKeyChar Xor iStringChar
strEncrypted = strEncrypted & Chr(iCryptChar)
next
EnCrypt = strEncrypted
End Function
Function DeCrypt(strEncrypted)
Dim strChar, iKeyChar, iStringChar, I
for I = 1 to Len(strEncrypted)
iKeyChar = (Asc(mid(g_Key,I,1)))
iStringChar = Asc(mid(strEncrypted,I,1))
iDeCryptChar = iKeyChar Xor iStringChar
strDecrypted = strDecrypted & Chr(iDeCryptChar)
next
DeCrypt = strDecrypted
End Function
Function ReadKeyFromFile(strFileName)
Dim keyFile, fso, f
set fso = Server.CreateObject("Scripting.FileSystemObject")
set f = fso.GetFile(strFileName)
set ts = f.OpenAsTextStream(1, -2)
Do While not ts.AtEndOfStream
keyFile = keyFile & ts.ReadLine
Loop
ReadKeyFromFile = keyFile
End Function
%>
</body>
</html>%
Const g_KeyLocation = "C:\key.txt"
Const g_KeyLen = 512
On Error Resume Next
Call WriteKeyToFile(KeyGeN(g_KeyLen),g_KeyLocation)
if Err <> 0 Then
Response.Write "生成密钥失败." & "<P>"
Response.Write Err.Number & "<BR>"
Response.Write Err.Description & "<BR>"
Else
Response.Write "生成密钥成功."
End If
Sub WriteKeyToFile(MyKeyString,strFileName)
Dim keyFile, fso
set fso = Server.CreateObject("scripting.FileSystemObject")
set keyFile = fso.CreateTextFile(strFileName, true)
keyFile.WriteLine(MyKeyString)
keyFile.Close
End Sub
Function KeyGeN(iKeyLength)
Dim k, iCount, strMyKey
lowerbound = 35
upperbound = 96
Randomize
for I = 1 to iKeyLength
s = 255
k = Int(((upperbound - lowerbound) + 1) * Rnd + lowerbound)
strMyKey = strMyKey & Chr(k) & ""
next
KeyGeN = strMyKey
End Function
%>

上一个:ASP查看网络设置代码实例
下一个:ASP满天星实例

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,