<%
Dim g_KeyLocation, g_Key
g_KeyLocation = "C:\crypt\" & BuildFileName() & ".txt"
Const g_CryptThis = "Now is the time for all good men to come to the aid of their country."
Const g_KeyLen = 512
On Error Resume Next
strFullKey = KeyGeN(g_KeyLen)
Call WriteKeyToFile(strFullKey)
if Err <> 0 Then
Response.Write "ERROR GENERATING KEY" & ""
Response.Write Err.Number & "
"
Response.Write Err.Description & "
"
Else
Response.Write "KEY SUCCESSFULLY GENERATED"
End If
Sub WriteKeyToFile(MyKeyString)
Dim keyFile, fso, strFileName
strFileName = g_KeyLocation
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, upperbound
lowerbound = 35 ' 35
upperbound = 96 ' 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
Function BuildFileName()
Dim k, i, strFileName, lowerbound, upperbound
lowerbound = 97
upperbound = 122
Randomize
for i = 1 to 24
k = Int(((upperbound - lowerbound) + 1) * Rnd + lowerbound)
strFileName = strFileName & Chr(k) & ""
next
BuildFileName = strFileName
End Function
g_Key = mid(ReadKeyFromFile(g_KeyLocation),1,Len(g_CryptThis))
Response.Write "
ORIGINAL STRING: " & g_CryptThis & "
"
Response.Write "
KEY VALUE: " & g_Key & "
"
Response.Write "
ENCRYPTED CYPHERTEXT: " & EnCrypt(g_CryptThis) & "
"
Response.Write "
DECRYPTED CYPHERTEXT: " & DeCrypt(EnCrypt(g_CryptThis)) & "
"
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
%>