Batch to encrypt and decrypt passwords using vbscript and powershell with symmetric encryption -


i want integrate vbscript use function symmetric encryption function batch file ask user enter password using powershell mask input text :

@echo off set "pscommand=powershell -command "$pword = read-host 'enter password' -assecurestring ; ^     $bstr=[system.runtime.interopservices.marshal]::securestringtobstr($pword); ^         [system.runtime.interopservices.marshal]::ptrtostringauto($bstr)"" /f "usebackq delims=" %%p in (`%pscommand%`) set password=%%p echo %password% pause 

vbscript code :

encrypted_string = crypt("123456789") wscript.echo encrypted_string decrypted_string = crypt(encrypted_string) wscript.echo decrypted_string '*************************************************************************** function crypt(text)  dim i,a = 1 len(text)       = mod len(255)       if = 0 = len(255)       crypt = crypt & chr(asc(mid(255,a,1)) xor asc(mid(text,i,1))) next end function '*************************************************************************** 

so, i'm trying combine codes batch file :

the combined batch-file :

@echo off & setlocal enabledelayedexpansion title %~n0 - encrypt_decrypt passwords hackoo 2016 mode 60,5 & color 0e :main call :clean call :inputpassword "please choose password" mypass call :crypt_decrypt !mypass! >%tmp%\mycryptedpass.txt (set /p cryptpass=)<%tmp%\mycryptedpass.txt echo encrypted password :!cryptpass!  pause cls call :crypt_decrypt !cryptpass!>%tmp%\myplaintextpass.txt (set /p myplaintextpass=)<%tmp%\myplaintextpass.txt echo password in plain text : !myplaintextpass!  pause goto :main ::******************************************************************************** :inputpassword cls echo. set "pscommand=powershell -command "$pword = read-host '%1' -assecurestring ; ^     $bstr=[system.runtime.interopservices.marshal]::securestringtobstr($pword); ^       [system.runtime.interopservices.marshal]::ptrtostringauto($bstr)""         /f "usebackq delims=" %%p in (`%pscommand%`) set %2=%%p goto :eof    ::******************************************************************************** :crypt_decrypt call :clean ( echo stringencrypted = crypt("%~1"^) echo wscript.echo stringencrypted echo '**************************************************************************** echo function crypt(text^)  echo dim i,a echo = 1 len(text^) echo       = mod len(255^) echo       if = 0 = len(255^) echo       crypt = crypt ^& chr(asc(mid(255,a,1^)^) xor asc(mid(text,i,1^)^)^) echo next echo end function echo '**************************************************************************** )>%tmp%\crypt_decrypt.vbs cscript /nologo %tmp%\crypt_decrypt.vbs goto :eof ::******************************************************************************** :clean if exist %tmp%\crypt_decrypt.vbs del %tmp%\crypt_decrypt.vbs goto :eof ::******************************************************************************** 

so, last batch script can encrypt , dercypt strings; when enter numbers or didn't work ??? example if enter :

  1. 123456789 password ==> not ok
  2. hackoo123 password ==> not ok

thank !

your encryption can produce null (ascii decimal 0), carriage return (ascii decimal 13), , newline (ascii decimal 10) bytes, of wreak havoc when write value file , try read in again. possible work carriage return , newline within environment variables, null absolute no go.

well, end of story. batch can use fc in binary mode read binary file, byte byte, outputting each byte in hex notation. (see hexdump.bat) don't think want go there.

if want deal encrypted values within batch environment variables, suggest come new encryption scheme avoids troublesome bytes. @ minimum must avoid null bytes.

another option abandon symmetric encryption, , let vbs convert encrypted form hex notation before write disk.

which leads me concern - why writing password disk? not sound idea.

final note - passing arbitrary strings on command line fraught peril. better off passing name of environment variable contains value, , let called routine value variable. vbs can read environment variable given variable name.


Comments