Encrypt and Decrypt

 

 

The security argument has been always charming. Sending a "disguised" massage to someone who is able to, through some operations, interpret it is call cryptography.

 

Below I post a code that I found on the net and that I've updated a little. The script modify an input string and then the opposite operation. This is the code:

 

'**********************************************************

Dim temp, key  

'temp is the input string

'key is the key using to modify the input string for both operation (en/decrypt)

'**********************************************************

 

temp="Now is the time for all good men To come To the aid of their fellow countrymen."
key = "huasHIYhkasdho1"

 

temp = EnDecrypt(temp,key, "ENCRYPT")
WScript.Echo temp
temp = EnDecrypt(temp,key, "DECRYPT")
WScript.Echo temp

 

'This is the unique function that done both operations

Function EnDeCrypt(Str, key, strAction)
 Dim lenKey, KeyPos, LenStr, x, Newstr
 
 Newstr = "" 

 'Newstr is the result string after operations


 lenKey = Len(key)

 'the length of the key (number of chars)


 KeyPos = 1

 'char position of the key

 
 LenStr = Len(Str)
 'length of the input string (number of chars)

 


 Select Case strAction

 

 Case "ENCRYPT":


  For x = 1 To LenStr             


         Newstr = Newstr & chr(asc(Mid(str,x,1)) + Asc(Mid(key,KeyPos,1)))

         'Operation Analysis:

         ' - Mid(str, x, 1): function to return the char in the position x of the

         '                       input string

         ' - Mid(key, keyPos, 1): function to return the char in the position

         '                                KeyPos of the key

         ' asc Function: return the ASCII number of a char (es: A = 65)

         ' chr Function: traslate a number in the corresponding char (es: 65 = A)

         ' the operation done the sum of 2 numbers and translate the result

           ' in a char.


         KeyPos = keypos+1
         If KeyPos > lenKey Then KeyPos = 1

         'this if statement move the pointer (KeyPos) to the first char of the key


   Next  

 

 

 Case "DECRYPT":


  For x = 1 To LenStr   

 

         'this action make the same operation but at the contrary

         'infact it doesn't add but subtracts
         Newstr = Newstr & chr(asc(Mid(str,x,1)) - Asc(Mid(key,KeyPos,1)))
         KeyPos = keypos+1
         If KeyPos > lenKey Then KeyPos = 1


  Next  

 

 End Select

 

 EnDeCrypt = Newstr

End Function

 

'**********************************************************

 

Pag: <<    <    >