-1

I want to "encrypt" a file. This is just for fun, not intending to store or send any sensetive data using this "encryption".

This code example is meant to ilistrate what I would like to do...

SET A=D
SET B=S
SET C=Q
SET D=G

ECHO %A%%B%%C%%D%

The text ABCD will now be displayed as DSQG insted (if I wrote something meaningfull the "encrypted" result would not mean anything).

My question is:

Can I (if so, how?) add '%' before and after every character in the file?

I searched on how to read a file using batch, found this (jeb's answer): Batch files: How to read a file?

Is there a soulution where I could read a normal file, encrypt it and store as an encrypted version aswell?

Thanks so much for any answer!

Community
  • 1
  • 1
Espen
  • 45
  • 8
  • You have too many questions in your question. Answers not worth it. – Endoro Jul 08 '13 at 13:35
  • You try to implement `ROT13`, smaples are at [Simple batch Cryptography: howto crypt / decrypt ROT13:](http://www.dostips.com/forum/viewtopic.php?f=3&t=1226) – jeb Jul 08 '13 at 14:55
  • I am sorry if my qestion was a little unspesific and if you think I have done to little work beforehand :( I will try to make more effort on my next problem before I ask for help. Thanks! – Espen Jul 09 '13 at 06:13

1 Answers1

2

This borrows strlen from jeb,

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET PT_FILE=plain.txt
IF NOT "%~1"=="" SET "PT_FILE=%~1"

CALL :INIT_CIPHER

FOR /F "tokens=*" %%l IN ('findstr.exe /R /N "^" "%PT_FILE%"') DO @(
    REM @ECHO(
    REM @ECHO( %%l
    SET "LINE=%%~l"
    SET "LINE=!LINE:*:=!"
    REM @ECHO(!LINE!
    CALL :strlen LINE_LEN LINE
    REM @ECHO(Length(!LINE_LEN!^)
    IF !LINE_LEN! EQU 0 (
        ECHO(
    ) ELSE (
        SET OUTLINE_E=
        FOR /L %%i IN (0,1,!LINE_LEN!) DO (
            SET "CHAR=!LINE:~%%i,1!"
            IF "!CHAR!"==" " (
                SET "OUTLINE_E=!OUTLINE_E! "
            ) ELSE (
                @ECHO !CHAR!|findstr.exe /R "[A-Za-z]" >NUL
                IF ERRORLEVEL 1 (
                    SET "OUTLINE_E=!OUTLINE_E!!CHAR!" 
                ) ELSE (
                    SET CHAR_E=
                    CALL :ENC "!CHAR!" "CHAR_E"
                    REM @ECHO '!CHAR!' =^> E(!CHAR_E!^)
                    SET "OUTLINE_E=!OUTLINE_E!!CHAR_E!"
                )
            )
        )
        ECHO(!OUTLINE_E!
    )
)



GOTO :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


:ENC
CALL SET "%~2=!%~1!"
REM ECHO E(!%~2!^)
EXIT /B


:: https://stackoverflow.com/questions/5837418/how-do-you-get-the-string-length-in-a-batch-file
:strlen <resultVar> <stringVar>
(   
    setlocal EnableDelayedExpansion
    set "s=!%~2!#"
    set "len=0"
    for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
        if "!s:~%%P,1!" NEQ "" ( 
            set /a "len+=%%P"
            set "s=!s:~%%P!"
        )
    )
)
( 
    endlocal
    set "%~1=%len%"
    exit /b
)


:: Substitution Cipher
::   ABCDEFGHIJKLMNOPQRSTUVWXYZ
::   ZYXWVUTSRQPONMLKJIHGFEDCBA
::   perl -e "@a=('a'..'z');for ($i=0; $i<@a; $i++) { print('SET '.uc($a[$i]).'='.uc($a[$#a-$i]).qq(\n)); }"

:INIT_CIPHER
SET A=Z
SET B=Y
SET C=X
SET D=W
SET E=V
SET F=U
SET G=T
SET H=S
SET I=R
SET J=Q
SET K=P
SET L=O
SET M=N
SET N=M
SET O=L
SET P=K
SET Q=J
SET R=I
SET S=H
SET T=G
SET U=F
SET V=E
SET W=D
SET X=C
SET Y=B
SET Z=A
EXIT /B

Notes:

  • It is very slow.
  • It loses the case of the source plaintext.
  • It doesn't insert % around every character (which wouldn't work for special characters anyway), but does what it sounds like you want.

Results:

>>> type plain.txt
This is a simple file to be encrypted.
This is a second line. A  blank line follows.

This is the fourth line.
This line has a colon (:) in the middle of it.
This line has quotes: "I said, 'Pass the bread, please,' as politely as possible."


>>> enc plain.txt
GSRH RH Z HRNKOV UROV GL YV VMXIBKGVW.
GSRH RH Z HVXLMW ORMV. Z  YOZMP ORMV ULOOLDH.

GSRH RH GSV ULFIGS ORMV.
GSRH ORMV SZH Z XLOLM (:) RM GSV NRWWOV LU RG.
GSRH ORMV SZH JFLGVH: "R HZRW, 'KZHH GSV YIVZW, KOVZHV,' ZH KLORGVOB ZH KLHHRYOV."


>>> enc > cipher.txt
enc > cipher.txt


>>> enc cipher.txt
THIS IS A SIMPLE FILE TO BE ENCRYPTED.
THIS IS A SECOND LINE. A  BLANK LINE FOLLOWS.

THIS IS THE FOURTH LINE.
THIS LINE HAS A COLON (:) IN THE MIDDLE OF IT.
THIS LINE HAS QUOTES: "I SAID, 'PASS THE BREAD, PLEASE,' AS POLITELY AS POSSIBLE."
Community
  • 1
  • 1
mojo
  • 4,050
  • 17
  • 24
  • +1; this is a nice idea :) [here](http://ss64.org/viewtopic.php?pid=6478#p6478) you can find more about `strlen`. – Endoro Jul 08 '13 at 17:18
  • As said above, thank you so much for this answer! I was better than I hoped was possible in batch. I am very happy that you shared this code ;) And I am sorry if my qestion was a little unspesific and if you think I have done to little work beforehand :( I will try to make more effort on my next problem before I ask for help (this comment is meant for all of you). – Espen Jul 09 '13 at 06:12
  • Marked it as an anwer now :) – Espen Jul 09 '13 at 15:11