Wednesday, 1 December 2010

VBS Encryption of text

This VB script can be used to encrypt and unencrypt data.


'
' file either contains Encrypt : userid : password
' or
' the result of the encryption
'

strFileName ="StoreSecure.dat"
strCryptMarker ="[ENCRYPT]"

Set objNet = WScript.CreateObject("WScript.Network")
cryptkey = "wscript.exe"

Const ForReading = 1
Const ForWriting = 2

Set fso = CreateObject("Scripting.FileSystemObject")

on error resume next
Set tfr = fso.OpenTextFile(strFileName,ForReading,False)
if err.number <> 0 then
wscript.echo "Error " & strFileName & " is missing - Unable to map directories "
wscript.quit 1
End if
on error goto 0

readText = tfr.readLine
splitText = Left(readText,Len(strCryptMarker))

if ucase(splitText) = strCryptMarker then
NewText = CryptVBS(mid(readText,Len(strCryptMarker) + 1),cryptkey)
close_it = tfr.close
Set tfw = fso.OpenTextFile(strFileName,ForWriting,True)
writeIt = tfw.write(NewText)
close_it = tfw.close
readText = NewText
End if

on error goto 0
NewReadText = CryptVBS(readText,cryptkey)

wscript.echo NewReadText


wscript.quit


'---------------------------------------
' Encryption function -
'---------------------------------------

Function CryptVBS(Text, Keynm)

keyptr=0
hold_it = ""
KeyLen = Len(Keynm)

For i = 1 To Len(Text)
KeyPtr = (KeyPtr + 1) Mod KeyLen
sTxtChr = Mid(Text, i, 1)
wTxtChr = Asc(stxtchr)
wKeyChr = Asc(Mid(Keynm, KeyPtr + 1, 1))
hold_it = hold_it & Chr(wTxtChr Xor wKeyChr)
Next

CryptVBS = hold_it
End Function

No comments:

Post a Comment