Page 1 of 1

Encrypting data for a batch file

Posted: 25 Dec 2012 19:49
by keyboard1333
Ok, I know that there is always someone out there who can crack any kind of encryption, but this is to stop your average user/small time power user.

How can I encrypt a file, so that it's contents are not easily editable...
E.g. You can't just go into an exe file and edit away.
This is to store data for a batch file, that I don't particularly want users editing...

I'm considering just hexing the entire file, but that'd make it rather large...

Re: Encrypting data for a batch file

Posted: 25 Dec 2012 20:10
by foxidrive
keyboard1333 wrote:How can I encrypt a file, so that it's contents are not easily editable...
E.g. You can't just go into an exe file and edit away.


That would depend on how the data got into the .EXE file.

Re: Encrypting data for a batch file

Posted: 25 Dec 2012 20:14
by foxidrive
You can use something as simple as ROT13 or change it ROT7 or whatever else takes your fancy.

http://en.wikipedia.org/wiki/ROT13

Re: Encrypting data for a batch file

Posted: 26 Dec 2012 02:02
by keyboard1333
Actually, I just thought of a better example -
The source of a .docx file is all gibberish?
How do they do that?

But thanks for your reply!!!

Re: Encrypting data for a batch file

Posted: 26 Dec 2012 02:17
by foxidrive
A .docx file is a ZIP file, with a different extension. So is a .JAR file.

If you have a specific task to solve then tell us the details.
However if you are just wondering about the insides of computer files - they aren't really on topic in a batch file forum.

Re: Encrypting data for a batch file

Posted: 26 Dec 2012 04:35
by keyboard1333
It was more of a theory question than a practical problem.
I have had experiences before we're I've needed to store data, but been unable as it would be accessable to anybody on can edit a file...

Many software(s?), store their data in a way so that it is not easily editable from outside the application, and I was wondering how to do this.
I guess not just encrypting the data, but storing it in a way that is... I'm not sure how to say this?

Re: Encrypting data for a batch file

Posted: 26 Dec 2012 04:44
by foxidrive
You encrypt the data with an algorithm and decrypt it to read it again.
A basic example is to use ROT-13 to encrypt a file and then use ROT-13 again to decrypt it.


This is the ROT-13 encryption of the text above - it's simple but it stops people reading it, who don't know about ROT-13.

Lbhe rapelcg gur qngn jvgu na nytbevguz naq qrpelcg vg gb ernq vg ntnva.
N onfvp rknzcyr vf gb hfr EBG-13 gb rapelcg n svyr naq gura hfr EBG-13 ntnva gb qrpelcg vg.

Re: Encrypting data for a batch file

Posted: 27 Dec 2012 19:27
by Squashman
If you don't want people to edit your files then set the appropriate file permissions so they only have read access to that file.

Re: Encrypting data for a batch file

Posted: 27 Dec 2012 21:50
by carlos
maybe you want encode, not encrypt.
encrypt should be hard in batch because is slow and have some limitations.

Re: Encrypting data for a batch file

Posted: 28 Dec 2012 01:44
by keyboard1333
Yes, that's what I'm looking for :P
Encode... :D
Any tips on that?

Re: Encrypting data for a batch file

Posted: 28 Dec 2012 02:48
by foxidrive
I think in this instance, encode and encrypt mean the same thing.

You are changing the input data in a known way, so you can obfuscate it, and also restore it to the original format.

Re: Encrypting data for a batch file

Posted: 02 Jan 2013 22:30
by Aacini
foxidrive wrote:You encrypt the data with an algorithm and decrypt it to read it again.
A basic example is to use ROT-13 to encrypt a file and then use ROT-13 again to decrypt it.


This is the ROT-13 encryption of the text above - it's simple but it stops people reading it, who don't know about ROT-13.

Lbhe rapelcg gur qngn jvgu na nytbevguz naq qrpelcg vg gb ernq vg ntnva.
N onfvp rknzcyr vf gb hfr EBG-13 gb rapelcg n svyr naq gura hfr EBG-13 ntnva gb qrpelcg vg.

Your example is wrong! The first encripted word "Lbhe" is NOT the encription of "You" because it has one letter more. I think that if you post examples as simple as this one (or complex ones, for that matter) you must be sure that the example is right!

Below is a basic ROT-13 encription Batch program:

Code: Select all

@echo off
rem simple rot-13 conversion filter program
rem fvzcyr ebg-13 pbairefvba svygre cebtenz
setlocal enabledelayedexpansion
set letter=abcdefghijklmnopqrstuvwxyz
for /l %%a in (0,1,25) do (
   set /a rot13=%%a+13
   if !rot13! geq 26 set /a rot13-=26
   for %%b in (!rot13!) do set rot13!letter:~%%a,1!=!letter:~%%b,1!
)
for /f "delims=" %%a in ('findstr "^"') do (
   setlocal disabledelayedexpansion
   set "line=%%a"
   setlocal enabledelayedexpansion
   set lastchar=0
   for /l %%b in (9,-1,0) do (
      set /a "lastchar|=1<<%%b"
      for %%c in (!lastchar!) do if "!line:~%%c,1!" equ "" set /a "lastchar&=~1<<%%b"
   )
   set output=
   for /l %%b in (0,1,!lastchar!) do (
      set char=!line:~%%b,1!
      if defined rot13!char! for %%c in (!char!) do set char=!rot13%%c!
      set output=!output!!char!
   )
   echo !output!
   endlocal & endlocal
)
The program works as a filter, that is, it read lines from the keyboard (stdin) and send output to screen (stdout). If you run this program over itself (rot13.bat < rot13.bat), you get this one:

Code: Select all

@rpub bss
erz fvzcyr ebg-13 pbairefvba svygre cebtenz
erz simple rot-13 conversion filter program
frgybpny ranoyrqrynlrqrkcnafvba
frg yrggre=nopqrstuvwxyzabcdefghijklm
sbe /y %%n va (0,1,25) qb (
   frg /n ebg13=%%n+13
   vs !ebg13! trd 26 frg /n ebg13-=26
   sbe %%o va (!ebg13!) qb frg ebg13!yrggre:~%%n,1!=!yrggre:~%%o,1!
)
sbe /s "qryvzf=" %%n va ('svaqfge "^"') qb (
   frgybpny qvfnoyrqrynlrqrkcnafvba
   frg "yvar=%%n"
   frgybpny ranoyrqrynlrqrkcnafvba
   frg ynfgpune=0
   sbe /y %%o va (9,-1,0) qb (
      frg /n "ynfgpune|=1<<%%o"
      sbe %%p va (!ynfgpune!) qb vs "!yvar:~%%p,1!" rdh "" frg /n "ynfgpune&=~1<<%%o"
   )
   frg bhgchg=
   sbe /y %%o va (0,1,!ynfgpune!) qb (
      frg pune=!yvar:~%%o,1!
      vs qrsvarq ebg13!pune! sbe %%p va (!pune!) qb frg pune=!ebg13%%p!
      frg bhgchg=!bhgchg!!pune!
   )
   rpub !bhgchg!
   raqybpny & raqybpny
)
Previous program use a simple method that convert all letters to lower-case. Of course, you may modify this program to correctly manage upcase and lowcase letters, or to achieve other types of encription. This is just an example of an encription method writen in Batch (that I think is what you are looking for). :wink:

I hope it helps...

Antonio

Re: Encrypting data for a batch file

Posted: 03 Jan 2013 10:59
by dbenham
Nice Aacini :D

But I see at least one bug. Your use of a simple FOR will cause problems if text contains * or ?.

Also, I don't understand why your string length computation uses 9. 12 is required to support the maximum ~8191 line length.

Finally, your master loop is skipping empty lines, easily fixed with FINDSTR /N followed by search for *: and replace with nothing.

Nearly two years ago I published an efficient batch ROT13 routine: viewtopic.php?p=7028#p7028

After adding support for mixed case and fixing the issues noted above, your algorithm is ~20% faster than my old method :D

Here is my implementation of your algorithm:

Code: Select all

@echo off
:: simple rot-13 conversion filter program
setlocal disableDelayedExpansion
set lower=abcdefghijklmnopqrstuvwxyz
set upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ
for /l %%a in (0,1,25) do (
  set /a "rot13=(%%a+13)%%26"
  setlocal enableDelayedExpansion
  for %%b in (!rot13!) do for /f "tokens=1-4" %%A in (
    "!lower:~%%a,1! !lower:~%%b,1! !upper:~%%a,1! !upper:~%%b,1!"
  ) do (
    endlocal
    set "lower%%A=%%B"
    set "upper%%C=%%D"
  )
)
for /f "delims=" %%a in ('findstr /n "^"') do (
  set "line=%%a"
  setlocal enableDelayedExpansion
  set "line=!line:*:=!"
  set output=
  if defined line (
    set /a len=0
    for /l %%b in (12,-1,0) do (
      set /a "len|=1<<%%b"
      for %%c in (!len!) do if "!line:~%%c,1!" equ "" set /a "len&=~1<<%%b"
    )
    for /l %%b in (0,1,!len!) do (
      set "char=!line:~%%b,1!"
      if defined lower!char! for /f delims^=^ eol^= %%c in ("!char!") do (
        if "!lower:%%c=%%c!" neq "!lower!" (
          set "char=!upper%%c!"
        ) else set "char=!lower%%c!"
      )
      set "output=!output!!char!"
    )
  )
  echo(!output!
  endlocal
)


Dave Benham