This is my implementation of the
Vigenere cipher. Mine shifts a bit more of the ASCII character set than just the alphabet to cater for code as the plain text. It substitutes the CR character with an uncommon printable character to make the encrypted text to be able to be put into a string variable.
The key is 5 characters
ROT13 generated from the vbscript
RND Function which actually isn't random unless you use the randomize statement (This was my attempt to confuse.)
With this particular cipher, if the key is the the same length as the plain text, then the encrypted text is truly unbreakable, that is if you don't use the same key twice and of course don't save the key in the same script.
As TNO demonstrated, cracking is simple really if you know vbscript. It sort of proves no matter how hard to you try to hide the key and obfuscate your code you can't encrypt vbscript if the key is embedded in the same code.
Here is the "decomplicated" code I promised.
'Vigenere Encryption
'Read from a plain text vbs file and create an encrypted string that can be pasted into code.
'Enter path to plain text script file
strPT=inputbox("Enter path to plain text file", "Vigenere Encryption")
'Enter key
strKey=inputbox("Enter key", "Vigenere Encryption", "ABCDE")
if instr(strKey, " ") then
msgbox "Sorry"&vbcrlf&"Error don't use spaces in key"
wscript.quit
end if
'Read plain text script file
Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strPT)
strFile = objFile.ReadAll
objFile.close
'Encrypt
dim akey()
redim akey(len(strKey))
for a=1 to len(strKey)
akey(a)=asc(mid(strKey,a,1))-65
next
s=-1
y=len(strFile)
for z=1 to y
w=mid(strFile,z,1)
u=asc(w)
s=s+1
if u>31 and u<128 then
t=u+akey((s mod len(strKey))+1)
if t>127 then t=t-127+31
strET=strET&chr(t)
else
strET=strET&chr(u)
end if
next
strET=replace(strET, chr(10), chr(165))
strET=replace(strET, chr(13), chr(167))
strET=replace(strET, chr(34), chr(169))
'Save as another file
objFSO.OpenTextFile(strPT&"-Vigenere encrypted with "&strKey&".txt", 8, True).WriteLine strET
Copy the encrypted text the above script generates into the strET variable of the following script.
'Vigenere Decryption
'Decrypt an encrypted script and execute
'Your Encrypted script
strET="mtiesx!$Vgrjrw$it©gics{sxee$"
'Enter key
strKey=inputbox("Enter key", "Vigenere Decryption", "ABCDE")
'Decrypt
dim akey()
redim akey(len(strKey))
for a=1 to len(strKey)
akey(a)=asc(mid(strKey,a,1))-65
next
s=-1
y=len(strET)
for z=1 to y
w=mid(strET,z,1)
u=asc(w)
s=s+1
if u=169 then u=34
if u>31 and u<128 then
t=u-akey((s mod len(strKey))+1)
if t<32 then t=t+127-31
strPT=strPT&chr(t)
else
strPT=strPT&chr(u)
end if
next
strPT=replace(strPT, chr(165), chr(10))
strPT=replace(strPT, chr(167), chr(13))
'Execute
on error resume next
executeglobal strPT
if err<>0 then msgbox "Try a different Key"
For a more hefty example that uses RC4 and some other tricks see this one:-
http://www.visualbasicscript.com/fb.aspx?m=74744
<message edited by TomRiddle on Sunday, August 02, 2009 11:12 PM>