日期:2012-10-12 浏览次数:21195 次
它的基本原理是,需要有一个需要加密的
明文和一个随机生成的解密钥匙文件。然后使用这两个文件组合起来生成密文。
(明文) 组合 (密钥) = 加密后的密文
所以本文介绍的是生成密钥的代码。我们假设我们生成的密钥为512位长的密钥,它已经足够来加密一个文本字符了。代码如下:
KeyGeN.asp文件
<%
'******************************
' KeyGeN.asp
'******************************
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 "ERROR GENERATING KEY." & "<P>"
Response.Write Err.Number & "<BR>"
Response.Write Err.Description & "<BR>"
Else
Response.Write "KEY SUCCESSFULLY GENERATED."
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 ' Initialize random-number generator.
For I = 1 to iKeyLength
s = 255
k = Int(((upperbound - lowerbound) + 1) * Rnd + lowerbound)
strMyKey = strMyKey & Chr(k) & ""
next
KeyGeN = strMyKey
End Function
%>
在IIS下运行上面的KeyGeN.asp页面。你只需要如此做一次,他将把密钥写入文件c:\key.txt中 (如果你愿意的话,你也可以把这个文件放到另外一个更加安全的地方).然后你可以打开这个key.txt文件,它将包含512个ASCII码在35到96之间的字符.并且由于是随机生成的,所以每个人的私人密钥文件key.txt将是不一样的,下面是一个例子密钥文件:
IY/;$>=3)?^-+7M32#Q]VOII.Q=OFMC`:P7_B;LG'=I+@5%*+OP:F_=';'NSY`-^S.`AA=BJ3M0.WF#T5LGK(=/<:+C2K/^7AI$;PU'OME2+T8ND?W$C(J\,;631'M-LD5F%%1TF_&K2A-D-54[2P,#'*JU%6`0RF3CMF0(#T07U'FZ=>#,+.AW_/+']DIB;2DTIA57TT&-)O'/*F'M>H.XH5W^0Y*=71+5*^`^PKJ(=E/X#7A:?,S>R&T;+B#<:-*\@)X9F`_`%QA3Z95.?_T#1,$2#FWW5PBH^*<])A(S0@AVD8C^Q0R^T1D?(1+,YE71X+.*+U$:3XO^Q].KG&0N0];[LJ
下面再仔细分析一下上面的程序,我们发现其中的lowerbound和upperbound的数值其实就是你想使用来加密的ASCII字符范围。后面一篇文章中将介绍如何使用这个密钥来加密和解密一个字符串.
在第一部分,讨论了如何生成密钥,下面将介绍如何使用这个密钥来加密和解密一个字符串。
下面的代码就是能够同时实现这个功能的函数
Crypt.asp文件
<%
Dim g_Key
Const g_CryptThis = "Now is the time for
all good men to come to the aid of their country."
Const g_KeyLocation = "c:\key.txt"
g_Key = mid(ReadKeyFromFile(g_KeyLocation),1,Len(g_CryptThis))
Response.Write "<p>ORIGINAL STRING: " & g_CryptThis & "<p>"
Response.Write "<p>KEY VALUE: " & g_Key & "<p>"
Response.Write "<p>ENCRYPTED CYPHERTEXT: " & EnCrypt(g_CryptThis) & "<p>"
Response.Write "<p>DECRYPTED CYPHERTEXT: " & 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))
' *** uncomment below to encrypt with addition,
' iCryptChar = iStringChar + iKeyChar
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))
' *** uncomment below to decrypt with subtraction
' iDeCryptChar = iStringChar - iKeyChar
iDeCryptChar = iKeyChar Xor iStringChar
strDecrypted = strDecrypted & Chr(iDeCryptChar)
next
DeCrypt = strDecrypted
End Function