0

1 Need help to shorten the code

2.(If posible) about case sensitive when mapping

Sample.Bat:

@echo off & setlocal EnableDelayedExpansion
set "Data=HELLO"


:Encrypt/Decrypt_Rot13

set Encrypted_Data=!Data!
set Encrypted_Data=!Encrypted_Data:A=A#!
set Encrypted_Data=!Encrypted_Data:B=B#!
set Encrypted_Data=!Encrypted_Data:C=C#!
set Encrypted_Data=!Encrypted_Data:D=D#!
set Encrypted_Data=!Encrypted_Data:E=E#!
set Encrypted_Data=!Encrypted_Data:F=F#!
set Encrypted_Data=!Encrypted_Data:G=G#!
set Encrypted_Data=!Encrypted_Data:H=H#!
set Encrypted_Data=!Encrypted_Data:I=I#!
set Encrypted_Data=!Encrypted_Data:J=J#!
set Encrypted_Data=!Encrypted_Data:K=K#!
set Encrypted_Data=!Encrypted_Data:L=L#!
set Encrypted_Data=!Encrypted_Data:M=M#!
set Encrypted_Data=!Encrypted_Data:N=N#!
set Encrypted_Data=!Encrypted_Data:O=O#!
set Encrypted_Data=!Encrypted_Data:P=P#!
set Encrypted_Data=!Encrypted_Data:Q=Q#!
set Encrypted_Data=!Encrypted_Data:R=R#!
set Encrypted_Data=!Encrypted_Data:S=S#!
set Encrypted_Data=!Encrypted_Data:T=T#!
set Encrypted_Data=!Encrypted_Data:U=U#!
set Encrypted_Data=!Encrypted_Data:V=V#!
set Encrypted_Data=!Encrypted_Data:W=W#!
set Encrypted_Data=!Encrypted_Data:X=X#!
set Encrypted_Data=!Encrypted_Data:Y=Y#!
set Encrypted_Data=!Encrypted_Data:Z=Z#!

set Encrypted_Data=!Encrypted_Data:A#=N!
set Encrypted_Data=!Encrypted_Data:B#=O!
set Encrypted_Data=!Encrypted_Data:C#=P!
set Encrypted_Data=!Encrypted_Data:D#=Q!
set Encrypted_Data=!Encrypted_Data:E#=R!
set Encrypted_Data=!Encrypted_Data:F#=S!
set Encrypted_Data=!Encrypted_Data:G#=T!
set Encrypted_Data=!Encrypted_Data:H#=U!
set Encrypted_Data=!Encrypted_Data:I#=V!
set Encrypted_Data=!Encrypted_Data:J#=W!
set Encrypted_Data=!Encrypted_Data:K#=X!
set Encrypted_Data=!Encrypted_Data:L#=Y!
set Encrypted_Data=!Encrypted_Data:M#=Z!
set Encrypted_Data=!Encrypted_Data:N#=A!
set Encrypted_Data=!Encrypted_Data:O#=B!
set Encrypted_Data=!Encrypted_Data:P#=C!
set Encrypted_Data=!Encrypted_Data:Q#=D!
set Encrypted_Data=!Encrypted_Data:R#=E!
set Encrypted_Data=!Encrypted_Data:S#=F!
set Encrypted_Data=!Encrypted_Data:T#=G!
set Encrypted_Data=!Encrypted_Data:U#=H!
set Encrypted_Data=!Encrypted_Data:V#=I!
set Encrypted_Data=!Encrypted_Data:W#=J!
set Encrypted_Data=!Encrypted_Data:X#=K!
set Encrypted_Data=!Encrypted_Data:Y#=L!
set Encrypted_Data=!Encrypted_Data:Z#=M!

echo. 
echo. Original Data:%data% 
echo. 
echo. Encrypted/Decrypted Data:%Encrypted_Data%
echo. 
pause

As you can see, i use mapping method and It worked perfectly (Input=HELLO,Output=URYYB),But i realy it tiring to modify it one by one if Needed or to make it as micro.

But when i trying to Shorten the code by using Loop and using same method .

Sample.Bat:

@echo off & setlocal EnableDelayedExpansion

set "Data=HELLO"

set "Process=Rot13"

set "Rot13_input= ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "Rot13_output= NOPQRSTUVWXYZABCDEFGHIJKLM"

::set "xxxxxx_input= xxxxxxxxxxxxxxxxxxx"
::set "xxxxxx_output= xxxxxxxxxxxxxxxxxxx"

if %Process% equ Rot13 set "Maxdigit=27"

for /l %%n in (1,1,%Maxdigit%) do ^
set "%Process%_input_%%n=!Rot13_input:~%%n,1!" & ^
set "%Process%_output_%%n=!Rot13_output:~%%n,1!"

::set Rot13_input_
::set Rot13_output_

set Encrypted_Data=!Data!

:: STUCK BELOW ::

for /l %%n in (1,1,%Maxdigit%) do ^
set Encrypted_Data=!Encrypted_Data:%Rot13_input_%%n%=%Rot13_input_%%n%#!

echo %Encrypted_Data%

:: STUCK ABOVE ::

pause

echo. 
echo. Original Data:%data% 
echo. 
echo. Encrypted/Decrypted Data:%Encrypted_Data%
echo. 
pause

I am stuck(error output) at second LOOP,Can anyone fix this or Maybe another methods it can be done?

3
  • 1
    Do not reinvent the wheel: ROT13 obfuscator Commented May 28, 2018 at 14:39
  • Thanks,Rot13 just a sample to my question , but i trying to see the code from your link Commented May 28, 2018 at 14:50
  • Try this: for /l %%n in (1,1,%Maxdigit%) do call set "Encrypted_Data=%%Encrypted_Data:!Rot13_input_%%n!=!Rot13_input_%%n!#%%" (this establishes 3 layers of variable expansion: 1. index %%n, 2. search and replace strings, 3. whole string) Commented May 28, 2018 at 15:38

1 Answer 1

1

This is the way I would do it:

@echo off & setlocal EnableDelayedExpansion

rem ---------------

set "Process=Rot13"

set "Rot13_input= ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "Rot13_output= NOPQRSTUVWXYZABCDEFGHIJKLM"

if %Process% equ Rot13 set "Maxdigit=26"

for /L %%n in (1,1,%Maxdigit%) do for /F %%c in ("!%Process%_input:~%%n,1!") do (
   set "%Process%_input_%%c=%%n"
)

rem ---------------

set "Data=HELLO"

set "DataLen=0"
for /L %%a in (10,-1,0) do (
   set /A "newLen=DataLen+(1<<%%a)"
   for %%b in (!newLen!) do if "!Data:~%%b,1!" neq "" set "DataLen=%%b"
)

set "Encrypted_Data="
for /L %%i in (0,1,%DataLen%) do for /F %%c in ("!Data:~%%i,1!") do for /F %%j in ("!%Process%_input_%%c!") do (
   set "Encrypted_Data=!Encrypted_Data!!Rot13_output:~%%j,1!"
)


echo Original Data: %data% 
echo/ 
echo Encrypted/Decrypted Data: %Encrypted_Data%
echo/ 
pause

And this is the case sensitive version:

@echo off & setlocal EnableDelayedExpansion

rem ---------------

set "Process=Rot13"

set "Rot13_input= ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "Rot13_output= NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"

if %Process% equ Rot13 set "Maxdigit=26"

for /L %%n in (0,1,%Maxdigit%) do for /F "delims=" %%c in ("!%Process%_input:~%%n,1!") do (
   set "%Process%_input_%%c=%%n"
)

rem ---------------

set "Data=Hello World"

set "DataLen=0"
for /L %%a in (10,-1,0) do (
   set /A "newLen=DataLen+(1<<%%a)"
   for %%b in (!newLen!) do if "!Data:~%%b,1!" neq "" set "DataLen=%%b"
)

set "Encrypted_Data="
for /L %%i in (0,1,%DataLen%) do for /F "delims=" %%c in ("!Data:~%%i,1!") do (
   set "index=!%Process%_input_%%c!"
   if "!Rot13_input:%%c=%%c!" neq "%Rot13_input%" set /A "index+=Maxdigit"
   for /F %%j in ("!index!") do set "Encrypted_Data=!Encrypted_Data!!Rot13_output:~%%j,1!"
)


echo Original Data: %data% 
echo/ 
echo Encrypted/Decrypted Data: %Encrypted_Data%
echo/ 
pause
Sign up to request clarification or add additional context in comments.

1 Comment

Can anyone share the link or explain about using shift(1<<%%a),because i don't understand it completely ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.