Source language: Translate to:

MD5 EnDecrypt Function

Questions and information about using VBScript and JavaScript in NeoBook functions

Moderator: Neosoft Support

MD5 EnDecrypt Function

Postby David de Argentina » Mon Sep 16, 2013 11:10 pm

Hi all,

I found here:

http://social.technet.microsoft.com/For ... n-vbscript

a VBScript function that encrypts any string and reverses the process when it is executes twice times.

After addapt it to NeoBook enviroment the code is:

(Save it into your VBScript Functions Folder as EnDeCrypt)

Code: Select all
{NeoBook Function}
Version=5.80
Language=VBScript
Param=[%1]|Text|Text to En/De Crypt
Param=[%2]|Text|Encrypt Key
Param=[%3]|Variable|Return Var
{End}

Dim sbox(255)
Dim key(255)
Dim Encryptkey
Dim EncryptStr
Dim Encryptedstr

EncryptStr = "[%1]"
Encryptkey = "[%2]"

Encryptedstr = EnDeCrypt(EncryptStr,Encryptkey)
publication.nbSetVar "[%3]", Encryptedstr


Sub RC4Initialize(strPwd)
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
':::  This routine called by EnDeCrypt function. Initializes the :::
':::  sbox and the key array)                                    :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

dim tempSwap
dim a
dim b

intLength = len(strPwd)
For a = 0 To 255
  key(a) = asc(mid(strpwd, (a mod intLength)+1, 1))
  sbox(a) = a
next

b = 0
For a = 0 To 255
  b = (b + sbox(a) + key(a)) Mod 256
  tempSwap = sbox(a)
  sbox(a) = sbox(b)
  sbox(b) = tempSwap
Next
End Sub

Function EnDeCrypt(plaintxt, psw)
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
':::  This routine does all the work. Call it both to ENcrypt    :::
':::  and to DEcrypt your data.                                  :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

dim temp
dim a
dim i
dim j
dim k
dim cipherby
dim cipher
i = 0
j = 0

RC4Initialize psw

For a = 1 To Len(plaintxt)
  i = (i + 1) Mod 256
  j = (j + sbox(i)) Mod 256
  temp = sbox(i)
  sbox(i) = sbox(j)
  sbox(j) = temp
  k = sbox((sbox(i) + sbox(j)) Mod 256)

  cipherby = Asc(Mid(plaintxt, a, 1)) Xor k
  cipher = cipher & Chr(cipherby)
Next
EnDeCrypt = cipher
End Function


The code to execute this function is the following:
(Put this code into a button Actions Properties)

Code: Select all
InputBox "EnDeCrypt" "Text to convert" "[String]"
Call "EnDeCrypt" "[String]" "ThisIsAVeryLongKey" "[String]"


Function must be improved, because when transform text to non-printable characters (specially character 13) produces an error. (text are separate in lines and does not stay enclosed between quotes)


Could any do the improvement ? (and publicate here ?)

Thanks in advance,
David de Argentina
User avatar
David de Argentina
 
Posts: 1553
Joined: Mon Apr 04, 2005 4:13 pm
Location: Buenos Aires, Argentina

Re: MD5 EnDecrypt Function

Postby Tony Kroos » Tue Sep 17, 2013 11:32 pm

this is not md5. It's a simplified non-strict RC4 cipher implementation and have to be improved.
Could any do the improvement ? (and publicate here ?)

can u please explain why u need cipher algos and what u want to achieve by using it, maybe we will find you a more appropriate method.
Also I would recommend this plugin http://www.neoplugins.com/?plugin/npciph
Tony Kroos
 
Posts: 402
Joined: Thu Oct 15, 2009 3:43 pm

Re: MD5 EnDecrypt Function

Postby David de Argentina » Wed Sep 18, 2013 12:42 am

Hi Tony,

I need keep secret some relevant data stored into registry.
Data should not be readeable, but it must be transformable to the original text in order to be evaluated.

I can do a plugin with some crypto stuff, but i'm trying to do some JScript / VBScript functions to keep light the final program...

Greetings from Buenos Aires,
David de Argentina
User avatar
David de Argentina
 
Posts: 1553
Joined: Mon Apr 04, 2005 4:13 pm
Location: Buenos Aires, Argentina

Re: MD5 EnDecrypt Function

Postby Tony Kroos » Wed Sep 18, 2013 5:11 pm

David de Argentina wrote:Function must be improved, because when transform text to non-printable characters (specially character 13) produces an error. (text are separate in lines and does not stay enclosed between quotes)
Could any do the improvement ? (and publicate here ?)

give me some examples (pm maybe) of your text data with all possible special characters used
Tony Kroos
 
Posts: 402
Joined: Thu Oct 15, 2009 3:43 pm

Re: MD5 EnDecrypt Function

Postby David de Argentina » Wed Sep 18, 2013 5:42 pm

Nothing special,

the [HDSerialNum], a delimiter, and a date.

I found this post:

viewtopic.php?f=22&t=19232

And works fine for me.

Thanks Tony,
David de Argentina
User avatar
David de Argentina
 
Posts: 1553
Joined: Mon Apr 04, 2005 4:13 pm
Location: Buenos Aires, Argentina

Re: MD5 EnDecrypt Function

Postby Tony Kroos » Thu Sep 19, 2013 8:32 pm

Well... again, regarding to [HDSerialNum]... it's not a hd serial number.

note that it is a volume serial number (which is changed after drive re-format, os reinstall, etc...) given by OS and not the one that is hard-coded by hardware manufacturer (that S/N u can see printed on the HD label). Basically, it is okay to use [HDSerialNum] in simple security schemes but if u code some serious s#it :wink: , keep in mind that [HDSerialNum] can be changed so your security will fail.

I guess [HDSerialNum] uses WMI to get it's [value] (as a most simple way), so if someone wants to know details how to obtain real hard-coded s/n u may check this article.

After reading that, we may know that due to some "compatibility bugs" WMI is returning a "corrupt" device serial number (Hex-encoded and byte-flipped). Other non-WMI stuff works as intended.
But we can still use it (unflip->Hex->String):

Code: Select all
{NeoBook Function}
Version=5,80
Language=VBScript
{End}
Dim objWMIService, colItems, objItem, x
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\.\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_DiskDrive")

    For Each objItem In colItems
        x = x + 1
        NbSetVar "[HD_Caption"&x&"]", objItem.Caption
        NbSetVar "[HD_DevID"&x&"]", objItem.DeviceID
        NbSetVar "[HD_PartNum"&x&"]", objItem.Partitions
        NbSetVar "[HD_Size"&x&"]", objItem.Size
        NbSetVar "[HD_Index"&x&"]", objItem.Index
        NbSetVar "[HD_SerialNum"&x&"]", UnflipHex2String(objItem.SerialNumber)
    Next

Function UnflipHex2String(strH)
    Dim i, j, uf, ufs
    For i = 1 To Len(strH) Step 4
        uf = uf & Mid(strH, i + 2, 2)
        uf = uf & Mid(strH, i, 2)
    Next
    For i = 1 To Len(uf) Step 2
        j = Mid(uf, i, 2)
        ufs = ufs & Chr(CLng("&h" & j))
    Next
    UnflipHex2String = ufs
End Function


Checked it for my WD Caviar blue hdd :
Data returned by Windows WMI (Win32_DiskDrive class -> SerialNumber property): 2020202057202d44435759413855393137333632
Hex data to string: W -DCWYA8U917362 (obviously junk)
Unflipped hex data to string: WD-WCAYU8193726 (that's better, hehe...)

so, it looks like we may use WMI and VBScript to obtain device manufacturer's serial numbers in native format.

Function returns an array of variables (just in case there is more than one device), [HD_Index] - index of device starting with 0 (first device)
Also u may want to determine on which device your program is started (by it's drive letter or partition number), just to determine s/n for this device only, check this example
Also I believe it can be implemented in native neobook global variables.

Will post about cipher script later today...
Tony Kroos
 
Posts: 402
Joined: Thu Oct 15, 2009 3:43 pm


Return to NeoBook Functions - VBScript & JavaScript

Who is online

Users browsing this forum: No registered users and 1 guest