Simple batch Cryptography: howto crypt / decrypt ROT13:

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
ByteAnalyser
Posts: 17
Joined: 16 Jul 2010 20:05

Simple batch Cryptography: howto crypt / decrypt ROT13:

#1 Post by ByteAnalyser » 04 Aug 2010 16:37

HRZR_EHACNGU:P:\Cebtenzzv

UEME_RUNPATH:C:\Program Files\myProggy\


---::[ ForeworD ]::---
Once again hello to all :) DosTips.com Community, aGerman :) here a new challenge:

---::[ SituatioN ]::---
W/ my previous 2 questions and your answers, I finally managed to write my simple script. Everything is working fine, but it is working only for WinXP in italian language. For any other language i should change some particulars my script.

---::[ AnalysiS ]::---
01) set path2crypt="%ProgramFiles%\program_dir\" (which will be smthg like c:\program files\myProggy\)
02) FOR every character from a-Z DO IF a --> n IF b --> o IF c --> p (in few words rot13 encryption (13th rotation subsittution)
03) at the end there should be a variable like %pathencrypted% which contains the rot13-encrypted string.

00) for example if I have:
UEME_RUNPATH:C:\Program Files\myProggy\
it should become:
HRZR_EHACNGU:P:\Cebtenz Svyrf\zlCebttl\
It should NOT replace or take actions on symbols: \ " ' + * - %

---::[ What did I dO? ]::---
As I do always, googled around for 5 days... printed and studied as good as possible the batch-FOR-command help... Unfortunately, even if I resolved myself other 3 issues, this time I need again help.

---::[ What do I neeD? ]::---
I need to change a script so that it works on all windows xp in all languages.

---::[ NoteS ]::---
Just again some comments or info about "very special" functions or characters used...

---::[ ConclusioN ]::---
Thank you again for your wonderful site and forum and for your grateful help!!!

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Simple batch Cryptography: howto crypt / decrypt ROT13:

#2 Post by aGerman » 04 Aug 2010 18:46

OMG! :shock:

Code: Select all

@echo off &setlocal

set "string=UEME_RUNPATH:C:\Program Files\myProggy\"

set "capitals=A B C D E F G H I L K L M N O P Q R S T U V W X Y Z"
set "smalls=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 /a n=0
for %%a in (%capitals%) do (
  call set C_%%a=%%n%%
  call set C_%%n%%=%%a
  set /a n+=1
)

set /a n=0
for %%a in (%smalls%) do (
  call set S_%%a=%%n%%
  call set S_%%n%%=%%a
  set /a n+=1
)

set /a n=0
:loop
call set "char=%%string:~%n%,1%%"
if "%char%"=="" goto ahead
set "char=%char:^=^^%"
set "char=%char:&=^&%"
set "char=%char:<=^<%"
set "char=%char:>=^>%"
set "char=%char:|=^|%"
echo.%char%|findstr "%capitals%">nul &&(
 call set "num=%%C_%char%%%"
 call set /a num+=13
 call set /a num=%%num%% %%%% 26
 call call set "char=%%%%C_%%num%%%%%%"
)
echo.%char%|findstr "%smalls%">nul &&(
 call set "num=%%S_%char%%%"
 call set /a num+=13
 call set /a num=%%num%% %%%% 26
 call call set "char=%%%%S_%%num%%%%%%"
)
set "newstring=%newstring%%char%"
set /a n+=1
goto loop

:ahead
echo %newstring%
pause>nul



Very slow and will not work if you have percent signs in your string. No idea if it will work for quotes etc. You could make some tests.
If I have more time tomorrow, maybe I could fix it.

Otherwise I will try to implement a temporary VBScript.

Regards
aGerman

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Simple batch Cryptography: howto crypt / decrypt ROT13:

#3 Post by jeb » 05 Aug 2010 04:49

ByteAnalyser wrote:---::[ NoteS ]::---
Just again some comments or info about "very special" functions or characters used...


Are you interested in "very special" functions or do you have a question about one of your functions?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Simple batch Cryptography: howto crypt / decrypt ROT13:

#4 Post by aGerman » 05 Aug 2010 12:11

Sorry ByteAnalyser, seems this is too much for batch and for VBScript too.

In batch you have to loop over the string to parse it sign by sign. But some special characters trigger an interrupt. E.g. quots:
if """=="" ... ->Syntax Error!
Other signs disappear, like ^. Don't know exactly where...
I did not try to replace letters (like set "string=%string:A=M%) because this is not case sensitive. A and a would be replaced by M.

Yesterday I thought I could use VBScript. But this does not work because all quotes disappear if you try to pass it in an argument to the script.

Jeb is a batch artist. He made things using batch I've never seen before. But unfortunately he didn't left a suggestion.

Regards
aGerman

ByteAnalyser
Posts: 17
Joined: 16 Jul 2010 20:05

Re: Simple batch Cryptography: howto crypt / decrypt ROT13:

#5 Post by ByteAnalyser » 06 Aug 2010 08:47

@aGerman: man, infinitely thank you very much again for yor replies. You're simply great and I read a lot of your posts in other threads with enthusiasm :) I will go through your code and study it as good as possible. Really thanks and Respect!

@jeb: thank you a lot... with "special functions" I thought about where can I read explanations about for example (in aGerman's script)

Code: Select all

call set S_%%a=%%n%% REM what does here exactly happen
or
set "char=%char:^=^^%" REM same as above...

It's because I don't want to wast your time so that's why I'm asking about where to find explanations for those (not-for-newbie) syntaxes :)

Once again thank you very much for everything, this site and people in this forum helped me to unserstand a lot of new thins :D
Kind Regards :)

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Simple batch Cryptography: howto crypt / decrypt ROT13:

#6 Post by jeb » 06 Aug 2010 13:50

Code: Select all

call set S_%%a=%%n%% REM what does here exactly happen
or
set "char=%char:^=^^%" REM same as above...


The first line is a simple set of the variables named S_a, S_B, S_c,... with the values 0, 1, 2, ...
It is the same like

Code: Select all

set S_a=0
set S_b=1
set S_c=2


The second line is a replacement function, the each ^ in the char variable will be replaced by ^^, that can be useful, because if try to echo a ^ it will be lost (because it is a special character for escaping the next character like "^(" )

A good thing to understand the code is to set echo on, like

Code: Select all

set /a n=0
echo on
for %%a in (%smalls%) do (
  call set S_%%a=%%n%%
  call set S_%%n%%=%%a
  set /a n+=1
)
echo off


Then it is very simply too understand how the code works.

ByteAnalyser
Posts: 17
Joined: 16 Jul 2010 20:05

Re: Simple batch Cryptography: howto crypt / decrypt ROT13:

#7 Post by ByteAnalyser » 06 Aug 2010 18:16

Thank you so much jeb, you're very kind. The explanation is very good (what I was looking for, too) and the "echo on" tip is useful --> I will do many trys with this. Thanks again to both for your greatful help :)

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Simple batch Cryptography: howto crypt / decrypt ROT13:

#8 Post by aGerman » 06 Aug 2010 19:03

Seems I got it.

I tried:

example.txt

Code: Select all

UEME_RUNPATH:C:\Program Files\myProggy\
&()[]{}^=;!'+,`~-_><|*?%$/.@"#0123




Batch code to process these lines

Code: Select all

@echo off &setlocal

set "capitals=A B C D E F G H I L K L M N O P Q R S T U V W X Y Z"
set "smalls=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 /a n=0
for %%a in (%capitals%) do (
  call set C_%%a=%%n%%
  call set C_%%n%%=%%a
  set /a n+=1
)

set /a n=0
for %%a in (%smalls%) do (
  call set S_%%a=%%n%%
  call set S_%%n%%=%%a
  set /a n+=1
)

:: process file
for /f "usebackq delims=" %%a in ("example.txt") do (
  set "string=%%a"
  call :procLine
)

pause
goto :eof

:: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:procLine
set "newstring="

set "countstring=%string:^=^^%"
set "countstring=%countstring:&=^&%"
set "countstring=%countstring:<=^<%"
set "countstring=%countstring:>=^>%"
set "countstring=%countstring:|=^|%"

:: *** output before
echo %countstring%

>%temp%\len.tmp echo.%countstring%
for %%i in (%temp%\len.tmp) do set /a len=%%~zi
del %temp%\len.tmp
set /a len-=3

for /l %%i in (0,1,%len%) do (
  call set "char=%%string:~%%i,1%%"
  call :procChar
)

:: *** output after
echo %newstring%
echo.
goto :eof


:: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:procChar
set "char=%char:^=^^%"
set "char=%char:"=^"%"
set "char=%char:&=^&%"
set "char=%char:<=^<%"
set "char=%char:>=^>%"
set "char=%char:|=^|%"

set "findChar=%char:^=^^^%"

echo.%findChar%|findstr "%capitals%">nul &&(
  call set "num=%%C_%char%%%"
  set /a num+=13
  set /a num=num%%26
  call call set "char=%%%%C_%%num%%%%%%"
)

echo.%findChar%|findstr "%smalls%">nul &&(
  call set "num=%%S_%char%%%"
  set /a num+=13
  set /a num=num%%26
  call call set "char=%%%%S_%%num%%%%%%"
)

set "newstring=%newstring%%char%"
goto :eof



Hope this will work for you.
Come with your issues if something isn't clear.

Regards
aGerman

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Simple batch Cryptography: howto crypt / decrypt ROT13:

#9 Post by ghostmachine4 » 06 Aug 2010 20:42

if you are not doing some kind of homework or project, use a programming language. Here's an example in Python, where rot13 already is implemented for you

Code: Select all

C:\test>python
ActivePython 2.6.4.8 (ActiveState Software Inc.) based on
Python 2.6.4 (r264:75706, Nov  3 2009, 13:23:17) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> string="UEME_RUNPATH:C:\Program Files\myProggy\"
>>> "\\".join( [ i.encode("rot13") for i in string.split("\\") ])
'HRZR_EHACNGU:P:\\Cebtenz Svyrf\\zlCebttl'

ByteAnalyser
Posts: 17
Joined: 16 Jul 2010 20:05

Re: Simple batch Cryptography: howto crypt / decrypt ROT13:

#10 Post by ByteAnalyser » 07 Aug 2010 09:10

aGerman, jeb, ghostmachine4 thank you all 3!!! I will let you know when everything is implemented ;) There should be more Communities w/ your "good willing" to help and skills.

Thx and Respect to all!

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Simple batch Cryptography: howto crypt / decrypt ROT13:

#11 Post by jeb » 08 Aug 2010 03:29

aGerman wrote:Seems I got it.


Sorry, but you didn't :wink:
Your example is too short
This one fails.

example.txt

Code: Select all

UEME_RUNPATH:C:\Program Files\myProggy\
&()[]{}^=;!'+,`~-_><|*?%$/.@"#0123
"&()[]{}^=;!'+,`~-_><|*?%$/.@"#0123


")" kann syntaktisch an dieser Stelle nicht verarbeitet werden.
I suppose in english this could be
")" is not unexcpected at this point

This problem could be tricky to solve.

But first, I got another problem.

Error: the command "echo." can't be found ...
I search my path and found a file named "echo", that was the problem, but the better way should be to use "echo\" instead of "echo."

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Simple batch Cryptography: howto crypt / decrypt ROT13:

#12 Post by aGerman » 08 Aug 2010 05:37

jeb wrote:
aGerman wrote:Seems I got it.


Sorry, but you didn't :wink:
[...]
This problem could be tricky to solve.

:shock: :x I don't wanna know it. :wink:
Will do my best. Do you have any suggestions, jeb?

jeb wrote:Error: the command "echo." can't be found ...
I search my path and found a file named "echo", that was the problem, but the better way should be to use "echo\" instead of "echo."

Interresting. I always used echo. because I thought it makes no difference which of the possible signs is placed behind echo. Thank's for pointing.

Regards
aGerman

ByteAnalyser
Posts: 17
Joined: 16 Jul 2010 20:05

Re: Simple batch Cryptography: howto crypt / decrypt ROT13:

#13 Post by ByteAnalyser » 08 Aug 2010 09:59

Danke vielmals aGerman und jeb, Ihr seid einfach großartig...
Thx you're greate :)

" Zeichen ist kein Problem... dass can I do manually as it is not strictly necessary because future strings to be processed in the script will never (wenn nicht durch ein Fehler) contain the " char. I will tell you more as soon as I have enough time to work on it, because I'm working 14 and 1/2 hours a day and so I don't have much time to stay in front of my beloved pc :(

Thanks again ;)

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Simple batch Cryptography: howto crypt / decrypt ROT13:

#14 Post by jeb » 08 Aug 2010 13:02

ByteAnalyser wrote:" Zeichen ist kein Problem

Ok, at this point it isn't a problem, but next time ...

aGerman wrote:Will do my best. Do you have any suggestions, jeb?


That's why the problem is tricky, I believe there is no simple, clean solution.

Example string
12>345^6"78^9a>b

Because, the first replacement have to be the escape of the quotes, " to ^" to avoid errors like
pipe to >345 or to >b

Code: Select all

set "var=%line:"=^^"%


But then you lost some single ^ characters.

A possible solution could be to work with the string always with single characters, like you do at the rot13 decryption

(searching for a better solution) :?: :?
jeb @brandenburgia/holiday

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Simple batch Cryptography: howto crypt / decrypt ROT13:

#15 Post by aGerman » 08 Aug 2010 13:43

jeb

I found a solution for the first part with a funny construct of MORE and FINDSTR. Don't need any replacements now.

Code: Select all

@echo off &setlocal

set "capitals=A B C D E F G H I L K L M N O P Q R S T U V W X Y Z"
set "smalls=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 /a n=0
for %%a in (%capitals%) do (
  call set "C_%%a=%%n%%"
  call set "C_%%n%%=%%a"
  set /a n+=1
)

set /a n=0
for %%a in (%smalls%) do (
  call set "S_%%a=%%n%%"
  call set "S_%%n%%=%%a"
  set /a n+=1
)

:: process file
for /f "tokens=1* delims=:" %%a in ('findstr /n . "example.txt"') do (
  set "string=%%b"
  set /a n=%%a-1
  >%temp%\len.tmp (call more +%%n%% "example.txt"|findstr /n .|findstr /b /c:"1:")
  for %%i in (%temp%\len.tmp) do set /a len=%%~zi-5
  del %temp%\len.tmp
  call :procLine
)

pause
goto :eof



But now I have the problems in the further code now. Have a look at the error (echo is on, I reduced the prompt to >).

Code: Select all

>echo\^( | findstr "a b c d e f g h i j k l m n o p q rs t u v w x y z"  1>nul  && (
call set "num=%S_^(%"
 set /a num+=13
 set /a num=num%26
 call call set "char=%%S_%num%%%"
)

>set "newstring=^"  & ("
Der Befehl "("" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.

>goto :eof

As you can see I escaped the brackets too, but no success.
Any ideas?

BTW: Have a nice holiday!

Regards
aGerman

Post Reply