View unanswered posts | View active topics It is currently 17 Sep 2014 05:31



Post new topic Reply to topic  [ 6 posts ] 
Encoding / Decoding 
Author Message

Joined: 20 Mar 2012 20:53
Posts: 43
Post Encoding / Decoding
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:
@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


23 Apr 2012 21:23
Profile

Joined: 20 Mar 2012 20:53
Posts: 43
Post Re: Encoding / Decoding
decode example.bat:
Code:
@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


23 Apr 2012 21:27
Profile

Joined: 23 Dec 2011 13:59
Posts: 1831
Post Re: Encoding / Decoding
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:


24 Apr 2012 05:51
Profile

Joined: 20 Mar 2012 20:53
Posts: 43
Post Re: Encoding / Decoding
Yes, not a difficult crack at all I just wrote it for a little challenge :)


24 Apr 2012 15:22
Profile

Joined: 11 Nov 2011 12:04
Posts: 32
Post Re: Encoding / Decoding
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:
@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


24 Apr 2012 18:17
Profile

Joined: 20 Mar 2012 20:53
Posts: 43
Post Re: Encoding / Decoding
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:
!endpattern: =! | clip


24 Apr 2012 19:58
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 


Who is online

Users browsing this forum: Google [Bot] and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.