En/Decryption of file with array variables.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
triggerer
Posts: 6
Joined: 03 May 2019 06:04

En/Decryption of file with array variables.

#1 Post by triggerer » 03 May 2019 07:02

Hello all! New to batch programming, Im fascinated how much use .bat still has. Cheers to you all!

Now into the subject. With the help of some other posts, I am trying to make a .bat file to encrypt and decrypt a text file with a progress bar (ignore the progress bar).
I made 2 loops.
If the decrypted name.txt file exists then "encrypt" the file to name.encr and delete the other file
If the encrypted name.encr file exists then "decrypt" the file to name.txt and delete the other file

Each line of the file is taken, proccesed and written into the other file.

However I face 2 problems where each line is added to the start of the next one and I do not know if it is possible to make it encrypt and decrypt uppercase, lowercase and sympols like "@#$". Example:
Input:
"123
abc
DEF
@#$"

Encrypted and then decrypted.
Output:
"123
123123ABC
123123ABC123ABCDEF
123123ABC123ABCDEF123ABCDEF"


TL;DR: If encrypted file exists, then decrypt it. If decrypted file exists, then encrypt it. Plus support case sensitive and symbols if possible.
Thanx in advance for the help.

Code: Select all

@ECHO OFF
::Setting the progress bar
IF EXIST "mytextx.txt" (
	set set_file=mytextx.txt
) ELSE IF EXIST "mytextx.encr" (
	set set_file=mytextx.encr
)
set tags=

SETLOCAL EnableDelayedExpansion
for /f "Tokens=* Delims=" %%x in (%set_file%) do set Build=!Build!%%x
ECHO %Build%>x&FOR %%? IN (x) DO SET /A strlength=%%~z? - 2&del x
set /a cnt=0
for /f %%a in ('type "%set_file%"^|find "" /v /c') do set /a cnt=%%a
set /a full = 100
for /l %%a in (1,1,%full%) do (
 CALL:ADDSPACE)
set /a denom=%full%/%cnt%
set /a current_progress=0
::Setting the progress bar end


::Encryption Table
(set CHAR_ENCRYPT[A]=UDFMHD45) & (set CHAR_ENCRYPT[B]=H121FDGF) & (set CHAR_ENCRYPT[C]=FDGHJ56D) & (set CHAR_ENCRYPT[D]=FGSG54D6) & (set CHAR_ENCRYPT[E]=JUK456JH)
(set CHAR_ENCRYPT[F]=ERE4G54S) & (set CHAR_ENCRYPT[G]=T5H64FDD) & (set CHAR_ENCRYPT[H]=RG56F41G) & (set CHAR_ENCRYPT[I]=RG45FG4D) & (set CHAR_ENCRYPT[J]=RT8564F6)
(set CHAR_ENCRYPT[K]=VCBV5C3B) & (set CHAR_ENCRYPT[L]=FD8G9G2F) & (set CHAR_ENCRYPT[M]=FDG4CVJS) & (set CHAR_ENCRYPT[N]=FG4213FG) & (set CHAR_ENCRYPT[O]=FD456GC2)
(set CHAR_ENCRYPT[P]=TH56GDF5) & (set CHAR_ENCRYPT[Q]=CV54F6GR) & (set CHAR_ENCRYPT[R]=XDF64FTS) & (set CHAR_ENCRYPT[S]=X78DG9RT) & (set CHAR_ENCRYPT[T]=TGH74SDJ)
(set CHAR_ENCRYPT[U]=BCX856DF) & (set CHAR_ENCRYPT[V]=FGH654SD) & (set CHAR_ENCRYPT[W]=45KLD45D) & (set CHAR_ENCRYPT[X]=GF2H3FG2) & (set CHAR_ENCRYPT[Y]=GFH564GF)
(set CHAR_ENCRYPT[Z]=45TG21FG) & (set CHAR_ENCRYPT[1]=D45G213D) & (set CHAR_ENCRYPT[2]=GB456DFG) & (set CHAR_ENCRYPT[3]=SDF456GF) & (set CHAR_ENCRYPT[4]=PF6F1G32)
(set CHAR_ENCRYPT[5]=FD6DFGG1) & (set CHAR_ENCRYPT[6]=56DFG54G) & (set CHAR_ENCRYPT[7]=UISG4FDG) & (set CHAR_ENCRYPT[8]=FKJH6FDG) & (set CHAR_ENCRYPT[9]=IFDGJHK6)

::Decryption Table
(set CHAR_DECRYPT[UDFMHD45]=A) & (set CHAR_DECRYPT[H121FDGF]=B) & (set CHAR_DECRYPT[FDGHJ56D]=C) & (set CHAR_DECRYPT[FGSG54D6]=D) & (set CHAR_DECRYPT[JUK456JH]=E)
(set CHAR_DECRYPT[ERE4G54S]=F) & (set CHAR_DECRYPT[T5H64FDD]=G) & (set CHAR_DECRYPT[RG56F41G]=H) & (set CHAR_DECRYPT[RG45FG4D]=I) & (set CHAR_DECRYPT[RT8564F6]=J)
(set CHAR_DECRYPT[VCBV5C3B]=K) & (set CHAR_DECRYPT[FD8G9G2F]=L) & (set CHAR_DECRYPT[FDG4CVJS]=M) & (set CHAR_DECRYPT[FG4213FG]=N) & (set CHAR_DECRYPT[FD456GC2]=O)
(set CHAR_DECRYPT[TH56GDF5]=P) & (set CHAR_DECRYPT[CV54F6GR]=Q) & (set CHAR_DECRYPT[XDF64FTS]=R) & (set CHAR_DECRYPT[X78DG9RT]=S) & (set CHAR_DECRYPT[TGH74SDJ]=T)
(set CHAR_DECRYPT[BCX856DF]=U) & (set CHAR_DECRYPT[FGH654SD]=V) & (set CHAR_DECRYPT[45KLD45D]=W) & (set CHAR_DECRYPT[GF2H3FG2]=X) & (set CHAR_DECRYPT[GFH564GF]=Y)
(set CHAR_DECRYPT[45TG21FG]=Z) & (set CHAR_DECRYPT[D45G213D]=1) & (set CHAR_DECRYPT[GB456DFG]=2) & (set CHAR_DECRYPT[SDF456GF]=3) & (set CHAR_DECRYPT[PF6F1G32]=4)
(set CHAR_DECRYPT[FD6DFGG1]=5) & (set CHAR_DECRYPT[56DFG54G]=6) & (set CHAR_DECRYPT[UISG4FDG]=7) & (set CHAR_DECRYPT[FKJH6FDG]=8) & (set CHAR_DECRYPT[IFDGJHK6]=9)
(set CHAR_DECRYPT[HG45J6FG]=.) & (set CHAR_DECRYPT[DF456HG4]=,)



IF EXIST "mytextx.txt" (
	:: Encryption if loop
	ECHO Cyphering.
	(
 		FOR /f "delims=" %%a IN (mytextx.txt) DO (
  			SET Encrypt=%%a
			set /a current_progress=current_progress+%denom%
			set set_title=Cyphering
  			CALL:PROGRESS
  			(CALL :MYENC)>>mytextx.encr
 		)
	)
	ECHO Cyphering ended.
	DEL mytextx.txt
) ELSE IF EXIST "mytextx.encr" (
	:: Decryption if loop
	ECHO Decyphering.
	(
		FOR /f "delims=" %%a IN (mytextx.encr) DO (
			SET Decrypt=%%a
			set /a current_progress=current_progress+%denom%
			set set_title=Decyphering
  			CALL:PROGRESS
			(CALL :MYDEC)>>mytextx.txt
			ECHO %enil%
		)
	)
	ECHO Decyphering ended.
	DEL mytextx.encr
) ELSE (
	::No file exists. Warn the user and exit.
	ECHO No relative file exists.
	TIMEOUT 5
)
DEL _temp.bat
GOTO :EOF
::End of encryption/decryption


::Decryption loop
:MYDEC
set char=%Decrypt:~0,8%
set Decrypt=%Decrypt:~8%
set DecryptOut=%DecryptOut%!CHAR_DECRYPT[%char%]!
if not "%Decrypt%"=="" goto MYDEC
ECHO %DecryptOut%
GOTO :EOF

::Encryption loop
:MYENC
set char=%Encrypt:~0,1%
set Encrypt=%Encrypt:~1%
set EncryptOut=%EncryptOut%!CHAR_ENCRYPT[%char%]!
if not "%Encrypt%"=="" goto MYENC
ECHO %EncryptOut%
GOTO :EOF


::Progressbar functions
:ADDSPACE
 set "fullBar=%fullBar%-"
 set tags=%tags%#
 exit/b
 GOTO :EOF

:PROGRESS
set /a pct=full-%current_progress%
 (TITLE %set_title%
  echo/echo/%set_title%: %current_progress%/100
  echo/echo [%%tags:~0,%current_progress%%%%%fullBar:~0,%pct%%%]
 )>_temp.bat
 cls
 call _temp.bat
 GOTO :EOF
 ::Progressbar functions end

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: En/Decryption of file with array variables.

#2 Post by pieh-ejdsch » 03 May 2019 14:55

Hallo triggerer,
For each new line that you read, you should delete the variable that provides the output encoding for the single letter.
Otherwise you will get the previous out in the next out.
Furthermore, you should pay attention to it, if the single character can not be de / encoded it may NOT be present in the character set for encryption.
It may NOT be deleted when decrypting.
Say you have no cipher for the single character do not code it either.
Then leave this sign separately. Compute (automatically) a new unique cipher for this new character.
Phil

triggerer
Posts: 6
Joined: 03 May 2019 06:04

Re: En/Decryption of file with array variables.

#3 Post by triggerer » 02 Jun 2019 16:21

Hallo Phil,

Thank you for your reply.
What would your approach be to, not process a single character that there is no encryption available when encrypting and also not process it while decrypting?
I have changed the code by a lot (running successfully) so please tell me if you need me to post the main function in order to comment on the actual code.

Cheers!

Post Reply