Encoding / Decoding

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Encoding / Decoding

#1 Post by MrKnowItAllxx » 23 Apr 2012 21:23

I just thought I would share this concept that I used to encode / decode text - nothing fancy, but I thought the algorithm was interesting (it counts in different bases for each digit of the 4 digit "code" to encode the letters) and it is fairly quick as well. Though you may find the code to be a little sloppy.

Anyway just thought I'd share - maybe someone will find it interesting / useable. I noticed that someone was making a password input prompt in their batch file anyway, maybe you could use it to encode the passwords or something, lol :)

code can only be 4 digits and is purely numerical (0 not advised as a digit)

encode example.bat:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set /p message=))
set /p code=Code:
call :encode string "%message%" "%code%"
echo %string%>message.txt
echo %string%
pause
goto :eof

:encode
rem Encodes a string using a code specified.
rem -Requires length function
rem -Requires genPage function
rem
rem    Syntax: encode [rVar] "string" "code"
set "str=%~2"
set "str=%str: =_%"
set "rStr="
set "page=%temp%\code.tmp"
call :genPage "%page%" %~3
call :length len "%str%"
set /a len=len-1
for /l %%a in (0,1,%len%) do (
for /f "usebackq tokens=1,2 delims=;" %%b in (%page%) do (
if "!str:~%%a,1!"=="%%b" set "rStr=!rStr!%%c"
))
del /q "%page%"
endlocal & if not "%~1"=="" set %~1=%rStr% & exit /b
goto :eof

:length
rem Returns the length of a string.
rem
rem    Syntax: length [rVar] "string"
setlocal enabledelayedexpansion
set "str=%~2"
set "len=0"
set "count=0"
:length_loop
if "!str:~%len%,1!"=="" (
endlocal & if not "%~1"=="" set %~1=%len% & exit /b
) else set /a len=len+1
goto :length_loop

:genPage
rem Generates a codepage for use encoding / decoding text.
rem based on a 4 digit code.
rem -Requires length function
rem
rem    Syntax: genPage "page name" [code]
setlocal enabledelayedexpansion
set "file=%~1"
set "code=%~2"
set "s1=%code:~0,1%"
set "s2=%code:~1,1%"
set "s3=%code:~2,1%"
set "s4=%code:~3,1%"
set "list=._!?/,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890$"
set "letter=."
set "str=%str: =.%"
for /l %%a in (0,1,%s1%) do (
for /l %%b in (0,1,%s2%) do (
for /l %%c in (0,1,%s3%) do (
for /l %%d in (0,1,%s4%) do (
call :nextLetter
echo !letter!;%%a%%b%%c%%d>>%file%
if not defined letter goto :eof
))))
endlocal
exit /b

:nextLetter
for /l %%a in (0,1,69) do if "!list:~%%a,1!"=="!letter!" set /a next=%%a+1 & set "letter=!list:~%next%,1!"
goto :eof


note - the encoded text is saved in a file called message.txt and read into decode.bat automatically so the user doesn't have to type the encoded text into the decode.bat prompt

MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Re: Encoding / Decoding

#2 Post by MrKnowItAllxx » 23 Apr 2012 21:27

decode example.bat:

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%a in (message.txt) do set "message=%%a"
set /p code=Code:
call :decode string "%message%" "%code%"
echo %string%
pause
goto :eof

:decode
rem Decodes a string using a code specified.
rem -Requires length function
rem -Requires genPage function
rem
rem    Syntax: encode [rVar] "string" "code"
set "str=%~2"
set "rStr="
set "page=%temp%\code.tmp"
call :genPage "%page%" %~3
call :length len "%str%"
set /a len=len-1
for /l %%a in (0,4,%len%) do (
for /f "tokens=1,2 delims=;" %%b in (%page%) do (
if "!str:~%%a,4!"=="%%c" set "rStr=!rStr!%%b"
))
set "rStr=%rStr:_= %"
del /q "%page%"
endlocal & if not "%~1"=="" set %~1=%rStr% & exit /b
goto :eof

:length
rem Returns the length of a string.
rem
rem    Syntax: length [rVar] "string"
setlocal enabledelayedexpansion
set "str=%~2"
set "len=0"
set "count=0"
:length_loop
if "!str:~%len%,1!"=="" (
endlocal & if not "%~1"=="" set %~1=%len% & exit /b
) else set /a len=len+1
goto :length_loop

:genPage
rem Generates a codepage for use encoding / decoding text
rem based on a 4 digit code.
rem -Requires length function
rem
rem    Syntax: genPage "page name" [code]
setlocal enabledelayedexpansion
set "file=%~1"
set "code=%~2"
set "s1=%code:~0,1%"
set "s2=%code:~1,1%"
set "s3=%code:~2,1%"
set "s4=%code:~3,1%"
set "list=._!?/,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890$"
set "letter=."
set "str=%str: =.%"
for /l %%a in (0,1,%s1%) do (
for /l %%b in (0,1,%s2%) do (
for /l %%c in (0,1,%s3%) do (
for /l %%d in (0,1,%s4%) do (
call :nextLetter
echo !letter!;%%a%%b%%c%%d>>%file%
if not defined letter goto :eof
))))
endlocal
exit /b

:nextLetter
for /l %%a in (0,1,69) do if "!list:~%%a,1!"=="!letter!" set /a next=%%a+1 & set "letter=!list:~%next%,1!"
goto :eof

Squashman
Expert
Posts: 4114
Joined: 23 Dec 2011 13:59

Re: Encoding / Decoding

#3 Post by Squashman » 24 Apr 2012 05:51

Brute forcing 4 digits is a trivial task for most any computer these days.
Heck my son forgot the combination to his for digit lock so one afternoon we plopped in the Lord of the Rings series and started turning tumblers. Was just a matter of time before we got. Wish he would have remembered it started with a 9. :lol:

MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Re: Encoding / Decoding

#4 Post by MrKnowItAllxx » 24 Apr 2012 15:22

Yes, not a difficult crack at all I just wrote it for a little challenge :)

Cat
Posts: 32
Joined: 11 Nov 2011 12:04

Re: Encoding / Decoding

#5 Post by Cat » 24 Apr 2012 18:17

I made something like this a while ago, it used a text password to encode the text. Vigenere cipher, I think it's called.

Code: Select all

@echo off
setlocal enabledelayedexpansion
set "chars=a b c d e f g h i j k l m n o p q r s t u v w x y z"
set "nums=1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26"
set /p str="Enter text to encrypt:"
set /p pass="Supply a password:"
cls&set n=0
echo Processing...
:loop
set "str2=!str2!!str:~%n%,1! "
set /a n+=1
if "!str:~%n%,1!" neq "" goto loop
set "str2=!str2:~0,-1!"
set n=0
:loop2
set "pass2=!pass2!!pass:~%n%,1! "
set /a n+=1
if "!pass:~%n%,2!" neq "" goto loop2
set "pass2=!pass2:~0,-1!"
set num=0
:loop3
if not defined char (set char=a) else (set "char=")
set /a num+=1
for /f "tokens=%num%" %%a in ("%pass2%") do set char=%%a
if %char%9==9 goto loop4A
choice /c "!chars: =!" /t 0 /d %char% >nul
set pattern=%pattern% %errorlevel%
goto loop3
:loop4A
set num=0
:loop4
if not defined char2 (set char2=a) else (set "char2=")
set /a num+=1
for /f "tokens=%num%" %%a in ("%str2%") do set char2=%%a
if "%char2%9"=="9" goto preloop5
choice /c "!chars: =!" /t 0 /d %char2% >nul
cls&echo Processing...
set pattern2=%pattern2% %errorlevel%
goto loop4
:preloop5
set "pattern=%pattern% %pattern% %pattern% %pattern% %pattern% %pattern% %pattern% %pattern% %pattern% %pattern% %pattern% %pattern% %pattern% %pattern%"
if "!pattern2 =!" gtr "!pattern: =!" goto preloop5
set num=0
set num2=0
:loop5
set var6=
set /a num2+=1
set /a num+=1
for /f "tokens=%num%" %%a in ("%pattern2%") do set var6=%%a
if "%var6%"=="" set num=27 &goto end
for /f "tokens=%num2%" %%b in ("%pattern%") do set /a numpick=("%var6%"+"%%b"-1)
if !numpick! gtr 26 set /a numpick-=26
set "endpattern=%endpattern% %numpick%"
goto loop5
:end
set /a num-=1
for /f "tokens=%num%" %%a in ("%chars%") do for /f "tokens=%num%" %%b in ("%nums%") do set endpattern=!endpattern:%%b=%%a!
if not !num! equ 1 goto end
echo Completed.&echo\
echo !endpattern: =!
echo !endpattern: =! | clip&echo\
echo The code has been copied to the clipboard.
pause

MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Re: Encoding / Decoding

#6 Post by MrKnowItAllxx » 24 Apr 2012 19:58

pretty cool :) I like that yours allows for text passwords, I might get around to making another that can use a text password

Also, I had no idea you could do this :D very cool!

Code: Select all

!endpattern: =! | clip

Hemlok
Posts: 10
Joined: 17 Nov 2019 15:32

Re: Encoding / Decoding

#7 Post by Hemlok » 19 Nov 2019 21:26

Cat wrote:
24 Apr 2012 18:17
I made something like this a while ago, it used a text password to encode the text. Vigenere cipher, I think it's called.

Code: Select all

@echo off
setlocal enabledelayedexpansion
set "chars=a b c d e f g h i j k l m n o p q r s t u v w x y z"
set "nums=1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26"
set /p str="Enter text to encrypt:"
set /p pass="Supply a password:"
cls&set n=0
echo Processing...
:loop
set "str2=!str2!!str:~%n%,1! "
set /a n+=1
if "!str:~%n%,1!" neq "" goto loop
set "str2=!str2:~0,-1!"
set n=0
:loop2
set "pass2=!pass2!!pass:~%n%,1! "
set /a n+=1
if "!pass:~%n%,2!" neq "" goto loop2
set "pass2=!pass2:~0,-1!"
set num=0
:loop3
if not defined char (set char=a) else (set "char=")
set /a num+=1
for /f "tokens=%num%" %%a in ("%pass2%") do set char=%%a
if %char%9==9 goto loop4A
choice /c "!chars: =!" /t 0 /d %char% >nul
set pattern=%pattern% %errorlevel%
goto loop3
:loop4A
set num=0
:loop4
if not defined char2 (set char2=a) else (set "char2=")
set /a num+=1
for /f "tokens=%num%" %%a in ("%str2%") do set char2=%%a
if "%char2%9"=="9" goto preloop5
choice /c "!chars: =!" /t 0 /d %char2% >nul
cls&echo Processing...
set pattern2=%pattern2% %errorlevel%
goto loop4
:preloop5
set "pattern=%pattern% %pattern% %pattern% %pattern% %pattern% %pattern% %pattern% %pattern% %pattern% %pattern% %pattern% %pattern% %pattern% %pattern%" 
if "!pattern2 =!" gtr "!pattern: =!" goto preloop5
set num=0
set num2=0
:loop5
set var6=
set /a num2+=1
set /a num+=1
for /f "tokens=%num%" %%a in ("%pattern2%") do set var6=%%a
if "%var6%"=="" set num=27 &goto end
for /f "tokens=%num2%" %%b in ("%pattern%") do set /a numpick=("%var6%"+"%%b"-1)
if !numpick! gtr 26 set /a numpick-=26
set "endpattern=%endpattern% %numpick%"
goto loop5
:end
set /a num-=1
for /f "tokens=%num%" %%a in ("%chars%") do for /f "tokens=%num%" %%b in ("%nums%") do set endpattern=!endpattern:%%b=%%a!
if not !num! equ 1 goto end
echo Completed.&echo\
echo !endpattern: =!
echo !endpattern: =! | clip&echo\
echo The code has been copied to the clipboard.
pause

What is the reverse to this encryption method with password? Im not very good with encryption, sorry if its obvious.

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

Re: Encoding / Decoding

#8 Post by pieh-ejdsch » 03 Dec 2019 14:24

Hallo Hemlok,
If you calculate the whole thing backwards, the matching result would also come out.
When encrypting, only the values of the text are added together with the values of the password. Backwards, the password is subtracted from the text. Thus, the text becomes visible again.

I made a little bit (too much) and that's when it came out.
It handles all printable characters of the ASCII.
But this code table will only work in a modified way with the original batch.
Jsut try it.

Code: Select all

@echo off
setlocal disabledelayedexpansion
call :setallmacros
setlocal enabledelayedexpansion
>nul chcp 1252
set "numC=0123456789"
set "lowerC=abcdefghijklmnopqrstuvwxyz"
set "upperC=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "otherC=#$%%&'()*+,-./:;<=>?@[\]^^_`{|}~ ^!""
set "extendC=àáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞß"
set "specialC=‘’“”•–—˜™š›œž€‚ƒ„…†‡ˆ‰Š‹ŒŽŸ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿"
set "alphanum="
for %%i in (num lower other upper extend special) do set "alphanum=!alphanum!!%%iC!" &set "%%iC="
%strLen(var):var=!alphanum!%
set /a "cCount=len,len-=1,C0=0"
for /l %%i in (0 1 %len%) do set "%%i=" &2>nul set "!alphanum:~%%i,1!="
for /l %%i in (0 1 %len%) do ( set "%%i=!alphanum:~%%i,1!"
  if %%i gtr 9 2>nul ( setlocal enabledelayedexpansion
    set /a "NN=XX=-1, XX=!alphanum:~%%i,1!, !alphanum:~%%i,1!=%%i, NN=!alphanum:~%%i,1!,XX+=NN"
    if !XX! equ %%i ( endlocal &set "!alphanum:~%%i,1!=%%i"
    ) else endlocal &set "C0=!C0!!alphanum:~%%i,1!%%i!alphanum:~%%i,1!"
) )
for %%i in (alphanum "strlen(var)" str len new) do set "%%~i="
:text
set /p text="Enter text:"||goto :text
:pass
set /p pass="Supply a password:"||goto :pass
call :yesNo "Do you want to encrypt the text:" ", the text will be decrypted if you choose:"
if errorlevel 1 (set "dec=+") else set "dec=-"
set "T1=!text!"
:encrypt
if Not defined P1 set "P1=!Pass!"
2>nul set /a "T0=0,T0=!T1:~0,1!"
if !T1:~0^,1! neq 0 if !T0! equ 0 set "T0=10"
if !T0! gtr 9 for /f tokens^=2delims^=^%T1:~,1% %%T in ("!C0!") do set /a T0=%%T
2>nul set /a "P0=0,P0=!P1:~0,1!"
if !P1:~0^,1! neq 0 if !P0! equ 0 set "P0=10"
if !P0! gtr 9 for /f tokens^=2delims^=^%P1:~,1% %%P in ("!C0!") do set /a P0=%%P
set /a "out=(cCount +T0 %dec%P0 ) %%(cCount)"
for %%i in (!out!) do set "new=!new!!%%i!" 
set "T1=!T1:~1!"
if defined T1 ( set "P1=!P1:~1!"
 goto :encrypt
)
echo(!new!
pause
exit /b

:YesNo [message]Y [message]N
@echo off
setlocal
set "y=0"
for /f "eol=0 tokens=3,4,6delims=(/) " %%a  in ('"echo n|xcopy /L /-y %windir%\win.ini %windir%\system.ini"') do (
 <nul set/p"=%~1[%%a]%~2[%%b]%%c "
 xcopy /Lpy "%~f0" nul:\* |findstr /b [1-9] >nul && (set /a "y=1" &echo %%a) ||  echo %%b
)
exit /b %y%

:setAllMacros
for /f "tokens=1-2 delims=;" %%i in ("cmd.exe;.") do (
  for /f "delims==-" %%i in ('2^>nul set') do set "%%i="
  set "COMSPEC=%COMSPEC%"
  set "Path=%%~dp$PATH:i"
  set "PATHEXT=%PATHEXT%"
  set "temp=%temp%"
  set "windir=%windir%"
)
:: define a newline with line continuation
(set \n=^^^
%= The empty line is critical - DO NOT REMOVE =%
)
:strLen.var
@set strLen(var^)=(%\n%
 set "str=Avar"%\n%
 set "len=0"%\n%
 for /l %%i in (12 -1 0) do (%\n%
  set /a "len|=1<<%%i"%\n%
  for %%# in (!len!^) do if .!str:~%%#^^^,1!==. set /a "len&=~1<<%%i"%\n%
))
exit /b
Phil

Post Reply