WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Encryption

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Stronger Encryption Example.wbt

Keywords: 	 encryption

IntControl(39,0,0,0,0)
IntControl(40,0,0,0,0)
AddExtender("WWHUG34I.DLL")
#DefineFunction fsoerr(errors)
   Return(1)
#EndFunction
#DefineFunction BinaryReadEx_C(buffer,binoffset,path,fileoffset,chunksize)
   IntControl(73,3,0,"fsoerr",0)
   If StrSub(path,1,2)!="\\" Then path="\\?\":path
      Else path="\\?\UNC\":StrSub(path,3,-1)
   bytesread=BinaryReadEx(buffer,binoffset,path,fileoffset,chunksize)
   If bytesread==0 && chunksize==0 Then bytesread="zerolength"
   Return bytesread
#EndFunction
#DefineFunction BinaryWriteEx_C(buffer,binoffset,path,fileoffset,chunksize)
   IntControl(73,3,0,"fsoerr",0)
   If StrSub(path,1,2)!="\\" Then path="\\?\":path
      Else path="\\?\UNC\":StrSub(path,3,-1)
   byteswritten=BinaryWriteEx(buffer,binoffset,path,fileoffset,chunksize)
   If byteswritten==0 && chunksize==0 Then byteswritten="zerolength"
   Return byteswritten
#EndFunction


IntControl (4, 1, 0, 0, 0);                  file list box must return a directory name

;set the stage
blocksize=1000000
blocklen=1000021
testfile=""
outfile=""
offset=0
encrypt="CryptEncrypt"
decrypt="CryptDecrypt"
final=@TRUE
loop=0
fileoffset=0

CryptoFormat=`WWWDLGED,5.0`;                  create dialog box
CryptoCaption=`File Encryption`
CryptoX=66
CryptoY=58
CryptoWidth=149
CryptoHeight=43
CryptoNumControls=2
Crypto01=`4,16,64,DEFAULT,PUSHBUTTON,DEFAULT,"Encrypt a file",1`
Crypto02=`78,16,64,DEFAULT,PUSHBUTTON,DEFAULT,"Decrypt a File",2`

ButtonPushed=Dialog("Crypto")
If IsDefined(param1)==@YES
   If param1=="/d" Then Debug(@ON)
EndIf
;get user input
:files
testfile=AskFilename("Crypto", "", "All Files|*.*|Text Files|*.txt", "", 1)
outfile=AskLine("Crypto","Enter an output file name?","output.txt")
outfile=FileFullname(outfile)
If StrUpper(testfile)==StrUpper(FileFullname(outfile))
   Message("Can't Do That!!","Output file name must be different than Input file name")
   Goto files
EndIf

If FileExist(outfile)==@TRUE
   If AskYesNo("Warning!","%outfile% already exists!%@crlf%Overwrite it?")==@YES
      If FileDelete(outfile)==@FALSE
         Message("Error","File: %outfile% could not be overwritten.%@crlf%Please choose another!")
         Goto files
      EndIf
   Else
      Goto files
   EndIf
EndIf

:getpassword
hpass=AskPassword("Encryption","Please enter a password to use with encryption key")
If hpass==""
   If AskYesNo("Wow","Did you mean to leave the password blank")==@NO
      Goto getpassword
   EndIf
EndIf



:dllstuff
bbcrypt=BinaryAlloc(5000)
BinaryEodSet(bbcrypt,5000)
BinaryPoke4(bbcrypt,0,0)

i=0
dll=StrCat(DirWindows(1),"advapi32.dll")
func="CryptAcquireContextW"
provname="Microsoft Enhanced RSA and AES Cryptographic Provider"
rc=0
errors=@FALSE
While rc==0 && errors==@FALSE
   contname="PCOSContainer":i
   rc=DllCall(dll,long:func,lpbinary:bbcrypt,lpwstr:contname,lpwstr:provname,long:24,long:8|40)
   If rc==0
      If DllLastError()!=-2146893809 Then errors=@TRUE
      BinaryPoke4(bbcrypt,0,0)
      i=i+1
   EndIf
EndWhile

If rc <>0
   hcrypt=BinaryPeek4(bbcrypt,0)
   BinaryFree(bbcrypt)
Else
   Message("Error","Could not acquire a cryptgraphic context or system parameter is invalid")
   Exit
EndIf

bbhashobj=BinaryAlloc(32)
BinaryEodSet(bbhashobj,32)
hashfunc="CryptCreateHash"
MD5=32771
rc=DllCall(dll,long:hashfunc,long:hcrypt,long:md5,long:0,long:0,lpbinary:bbhashobj)
hobj=BinaryPeek4(bbhashobj,0)
BinaryFree(bbhashobj)
len=StrLen(hpass)
bbpass=BinaryAlloc(len)
BinaryEodSet(bbpass,len)
BinaryPokeStr(bbpass,0,hpass)
passfunc="CryptHashData"
rc=DllCall(dll,long:passfunc,long:hobj,lpbinary:bbpass,long:len,long:0)
If rc==0
   Message("Error","Could not create hash object..Exiting")
   Exit
EndIf
BinaryFree(bbpass)
keyfunc="CryptDeriveKey"
en_alg=26128 ;aes_256
bbprivatekey=BinaryAlloc(100)
BinaryEodSet(bbprivatekey,100)
rc=DllCall(dll,long:keyfunc,long:hcrypt,long:en_alg,long:hobj,long:16|1,lpbinary:bbprivatekey)
If rc<>0
   privatekey=BinaryPeek4(bbprivatekey,0)
   BinaryFree(bbprivatekey)
   killhash=DllCall(dll,long:"CryptDestroyHash",long:hobj)
Else
   Message("Error","Encryption Key Failed!.. Exiting")
   Exit
EndIf

keystore=BinaryAlloc(blocklen)
bbkeylen=BinaryAlloc(32)
BinaryPoke4(bbkeylen,0,0)
bbdatalen=BinaryAlloc(32)
BinaryPoke4(bbdatalen,0,0)
bbdata=BinaryAlloc(blocklen)

If buttonpushed==1
   keyfunc="CryptGenKey"
   en_alg=26128 ;aes_256
;   en_alg=26114 ;rc2
   bbpublickey=BinaryAlloc(3000)
   BinaryEodSet(bbpublickey,3000)
   rc=DllCall(dll,long:keyfunc,long:hcrypt,long:en_alg,long:16|1,lpbinary:bbpublickey)
   If rc<>0
      publickey=BinaryPeek4(bbpublickey,0)
      BinaryFree(bbpublickey)
   Else
      Message("Error","Encryption Key Failed!.. Exiting")
      Exit
   EndIf
   keyfunc="CryptExportKey"
   rc=DllCall(dll,long:keyfunc,long:publickey,long:0,word:8,word:0,lpNull,lpbinary:bbkeylen)
   newlen=BinaryPeek4(bbkeylen,0)
   keyblocksize=newlen
   rc=DllCall(dll,long:keyfunc,long:publickey,long:0,word:8,word:0,lpbinary:keystore,lpbinary:bbkeylen)
   origkeyblocksize=BinaryPeek4(bbkeylen,0)
   keyblocksize=blocksize
   BinaryPoke4(bbkeylen,0,keyblocksize)
   BinaryEodSet(keystore,keyblocksize)
   If rc<>0
      rc=DllCall(dll,long:"CryptEncrypt",long:privatekey,lpNull,long:1,long:0,lpnull,lpbinary:bbkeylen,long:blocklen)
      newlen=BinaryPeek4(bbkeylen,0)
      If newlen>keyblocksize
         BinaryPoke4(bbkeylen,0,keyblocksize)
         keyblocksize=newlen
         BinaryEodSet(keystore,keyblocksize)
      EndIf
      rc=DllCall(dll,long:"CryptEncrypt",long:privatekey,lpNull,long:1,long:0,lpbinary:keystore,lpbinary:bbkeylen,long:blocklen)
      If rc==0
         Message("Error","Failed to encrypt key!.. Exiting")
         Exit
      EndIf
      BinaryPoke4(keystore,keyblocksize,origkeyblocksize)
      While binarywriteex_C(keystore,0,DirScript():"\test.enc",0,keyblocksize+4)==0
         TimeDelay(.05)
      EndWhile
   Else
      Message("Error","Export Key Failed!.. Exiting")
      Exit
   EndIf
Else
   BinaryEodSet(keystore,0)
   keyblocksize=FileSize(DirScript():"\test.enc")-4
   BinaryPoke4(bbkeylen,0,keyblocksize)
   While binaryreadex_C(keystore,0,DirScript():"\test.enc",0,FileSize(DirScript():"\test.enc"))==0
      TimeDelay(.05)
   EndWhile
   origkeyblocksize=BinaryPeek4(keystore,keyblocksize)
   BinaryEodSet(keystore,keyblocksize)
   rc=DllCall(dll,long:"CryptDecrypt",long:privatekey,long:0,long:1,long:0,lpNull,lpbinary:bbkeylen)
   keyblocksize=BinaryPeek4(bbkeylen,0)
   BinaryPoke4(bbkeylen,0,keyblocksize)
   BinaryEodSet(keystore,keyblocksize)
   rc=DllCall(dll,long:"CryptDecrypt",long:privatekey,long:0,long:1,long:0,lpbinary:keystore,lpbinary:bbkeylen)
   If rc==0
      Message("Error","Failed to decrypt key!.. Exiting")
      Exit
   EndIf
   keyblocksize=origkeyblocksize
   BinaryEodSet(keystore,keyblocksize)
   bbpublickey=BinaryAlloc(100)
   BinaryEodSet(bbpublickey,100)
   keyfunc="CryptImportKey"
   rc=DllCall(dll,long:keyfunc,long:hcrypt,lpbinary:keystore,word:keyblocksize,long:0,word:0,lpbinary:bbpublickey)
   If rc<>0
      publickey=BinaryPeek4(bbpublickey,0)
      BinaryFree(bbpublickey)
   Else
      Message("Error","Import Key Failed!.. Exiting")
      Exit
   EndIf
EndIf

eof=FileSize(testfile)

BinaryEodSet(bbdatalen,32)
BinaryPoke4(bbdatalen,0,0)

;if eof > blocklen
;   while @true
;      if blocklen > eof then break
;      blocklen=blocklen + 1000
;   end while
;end if

If buttonpushed==2 Then Goto decryptloop

:encryptloop
BoxOpen("Encrypting","")
BoxColor(1, "0,255,0", 2);                  Green with yellow Wash Color
BoxTextColor(1, "255,255,255")
BoxDataTag(1,"HERCULES")
While @TRUE
   target=eof - offset
   If target < blocksize
      blocksize=target
      final=@TRUE
   Else
      final=@FALSE
   End If
   While BinaryReadEx_C(bbdata, 0, testfile, offset, blocksize)==0
      TimeDelay(.05)
   EndWhile
   BinaryPoke4(bbdatalen,0,blocksize)
   If final==@TRUE
      DllCall(dll,long:encrypt,long:publickey,long:0,long:final,long:0,lpnull,lpbinary:bbdatalen,long:blocklen)
      newlen=BinaryPeek4(bbdatalen,0)
      If newlen>blocksize
         BinaryPoke4(bbdatalen,0,blocksize)
         blocksize=newlen
         BinaryEodSet(bbdata,blocksize)
      EndIf
   EndIf
   rc=DllCall(dll,long:encrypt,long:publickey,long:0,long:final,long:0,lpbinary:bbdata,lpbinary:bbdatalen,long:blocklen)
   If rc==1
      advance=BinaryPeek4(bbdatalen,0)
      If advance==0 Then Message("",BinaryPeekStr(bbdata,0,blocksize))
      a=FileSize(outfile)
      While binarywriteex_C(bbdata,0,outfile,a,blocksize)==0
         TimeDelay(.05)
      EndWhile
      BoxDataClear(1,"HERCULES")
      BoxText(StrCat(@CRLF,@CRLF,@TAB,@TAB,"Processing: %a% of %eof%Bytes.."))
   End If
   offset=offset + blocksize
   If offset >= eof Then Break
   loop=loop + 1
End While
GoSub cleanup
Exit

:decryptloop
BoxOpen("Decrypting","")
BoxColor(1, "0,0,255",1 );                  Blue with Red Wash Color
BoxTextColor(1, "255,255,255")
BoxDataTag(1,"HERCULES")
While @TRUE
   If huge_Subtract(eof,offset) < blocksize
      blocksize=huge_Subtract(eof,offset)
      final=@TRUE
   Else
      final=@FALSE
   End If
   If offset >= eof Then Break
   While BinaryReadEx_c(bbdata, 0, testfile, offset, blocksize)==0
      TimeDelay(.05)
   EndWhile
   BinaryPoke4(bbdatalen,0,blocksize)
   rc=DllCall(dll,long:decrypt,long:publickey,long:0,long:final,long:0,lpbinary:bbdata,lpbinary:bbdatalen)
;   message("test",DLLLastError())
   If rc==1
      advance=BinaryPeek4(bbdatalen,0)
      While BinaryWriteEx_c(bbdata, 0, OutFile, offset, advance)==0
         TimeDelay(.05)
      EndWhile
      TimeDelay(2)
      BoxDataClear(1,"HERCULES")
      BoxText(StrCat(@CRLF,@CRLF,@TAB,@TAB,"Processing: %offset% of %eof%Bytes.."))
   End If
   offset=offset + advance
   loop=loop + 1
End While
GoSub cleanup
Exit

:cleanup
DllCall(dll,long:"CryptDestroyKey",long:publickey)
DllCall(dll,long:"CryptDestroyKey",long:privatekey)
DllCall(dll,long:"CryptReleaseContext",long:hcrypt,word:0)
Return

Article ID:   W14965
File Created: 2013:03:12:13:41:40
Last Updated: 2013:03:12:13:41:40