Encrypt.vbs

This script is a sample encryption/decryption algorithm. The algorithm uses a encryption key and a random seed. If you change the seed or the key, you will get a different encrypted string. You need to use the same key and seed for encryption and decryption.

‘******************************************************************************
‘** Script: encrypt.vbs
‘** Version: 1.0
‘** Created: 20-1-2009 22:28
‘** Author: Adriaan Westra
‘** E-mail:
‘**
‘** Purpose / Comments:
‘** Demonstrate a simple encryption algorithm.
‘**
‘** Doel / Commentaar:
‘** Demoscript voor een simpel encryptie algoritme.
‘**
‘**
‘** Changelog :
‘** date / time :
‘** 20-1-2009 22:28 : Initial version
‘**
‘******************************************************************************
‘******************************************************************************
‘** Constants for opening files
‘** Constanten voor het openen van files
Const ForWriting = 2
Const ForAppending = 8
Dim strEncrypted
Dim strKey
Dim intSeed
‘******************************************************************************
‘** This is the key that will be use for en/decrypting the text
strKey = “This is a very long key”
‘******************************************************************************
‘** Tis is the text that is going to be encrypted
strEncrypt = “This text will be encrypted.”
‘******************************************************************************
‘** This is the seed that is used for randpmizing the en/decryption
intSeed = 6
‘******************************************************************************
‘** Call the Encrypt function to encrypt the text
strEncryptedText = Encrypt(strEncrypt, strKey, intSeed)
‘******************************************************************************
‘** Store the encrypted text in a file for later use
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
strFile = “encrypted.txt”
Set objTxTFile = objFSO.OpenTextFile(strFile, ForWriting, True)
objTxTFile.Write strEncryptedText
objTxTFile.Close
‘******************************************************************************
‘** Get the encrypted text from file
Set objTxtFile = objFSO.OpenTextFile(strFile)
If Not objTxtFile.AtEndOfStream Then
strDecrypt = objTxtFile.ReadAll
End If
objTxTFile.Close
‘******************************************************************************
‘** Output the encrypted text to screen
wscript.echo “Encrypted text : ” & strDecrypt

‘******************************************************************************
‘** Output the decrypted text to screen
wscript.echo “Decrypted text : ” & Decrypt(strDecrypt, strKey, intSeed)

wscript.quit
‘******************************************************************************
‘** Function: String2Asc
‘** Version: 1.0
‘** Created: 20-1-2009 22:35
‘** Author: Adriaan Westra
‘** E-mail:
‘**
‘** Purpose / Comments:
‘**
‘** Transform a string in to an array with ascii values
‘**
‘** Change Log :
‘**
‘** 20-1-2009 22:36 : Initial Version
‘**
‘** Arguments :
‘**
‘** strIn : string to be converted
‘**
‘** Returns :
‘**
‘** an array with ascii values
‘**
‘******************************************************************************
Function String2Asc( strIn)
arrResult = Array()
ReDim arrResult( CInt( Len( strIn ) ) )
For intI = 0 to Len(strIn) – 1
arrResult( intI ) = Asc( Mid( strIn,intI + 1 ,1 ) )
Next
String2Asc = arrResult
End Function
‘******************************************************************************
‘** Function: Encrypt
‘** Version: 1.0
‘** Created: 20-1-2009 22:35
‘** Author: Adriaan Westra
‘** E-mail:
‘**
‘** Purpose / Comments:
‘**
‘** Encrypt a string in to an encrypted string
‘**
‘** Change Log :
‘**
‘** 20-1-2009 22:36 : Initial Version
‘**
‘** Arguments :
‘**
‘** strEncrypt : string to be encrypted
‘** strKey : string used as encryption key
‘** intSeed : integer to make the encryption random
‘**
‘** Returns :
‘**
‘** an encrypted string
‘**
‘******************************************************************************
Function Encrypt( strEncrypt, strKey, intSeed)
Rnd(-1)
Randomize intSeed
intRnd = Int( ( Len(strKey) – 1 + 1 ) * Rnd + 1 )

arrEncrypt = String2Asc(strEncrypt)
arrKey = String2Asc(strKey)

For intI = 0 to UBound( arrEncrypt ) – 1

intPointer = intI + intRnd
If intPointer > UBound(arrKey) Then
intPointer = intPointer – ((UBound(arrKey) + 1 ) * Int(intPointer / (UBound(arrKey) + 1)))
End If

intCalc = arrEncrypt(intI) + arrKey(intPointer)

If intCalc > 256 Then
intCalc = intCalc – 256
End If
strEncrypted = strEncrypted & Chr(intCalc)
Next
encrypt = strEncrypted
End Function
‘******************************************************************************
‘** Function: Decrypt
‘** Version: 1.0
‘** Created: 20-1-2009 22:35
‘** Author: Adriaan Westra
‘** E-mail:
‘**
‘** Purpose / Comments:
‘**
‘** Decrypt an encrypted string
‘**
‘** Change Log :
‘**
‘** 20-1-2009 22:36 : Initial Version
‘**
‘** Arguments :
‘**
‘** strDecrypt : string to be Decrypted
‘** strKey : string used as encryption key
‘** intSeed : integer used to make the encryption random
‘**
‘** Returns :
‘**
‘** A Decrypted string
‘**
‘******************************************************************************
Function Decrypt( strDecrypt, strKey, intSeed)
Rnd(-1)
Randomize intSeed
intRnd = Int( ( Len(strKey) – 1 + 1 ) * Rnd + 1 )

arrDecrypt = String2Asc(strDecrypt)
arrKey = String2Asc(strKey)

For intI = 0 to UBound( arrDecrypt ) – 1

intPointer = intI + intRnd
If intPointer > UBound(arrKey) Then
intPointer = intPointer – ((UBound(arrKey) + 1 ) * Int(intPointer / (UBound(arrKey) + 1)))
End If

intCalc = arrDecrypt(intI) – arrKey(intPointer)

If intCalc < 0 Then intCalc = intCalc + 256 End If strDecrypted = strDecrypted & Chr(intCalc) Next Decrypt = strDecrypted End Function

Geef een reactie