10

For JavaScript you have e.g. js uglifier, which obfuscates the javascript so that it is infeasible for humans to read the javascript code. Also, judging by @DavidCarlisle's xii.tex (see below), obfuscations is definitely possible in TeX.

\let~\catcode~`76~`A13~`F1~`j00~`P2jdefA71F~`7113jdefPALLF
PA''FwPA;;FPAZZFLaLPA//71F71iPAHHFLPAzzFenPASSFthP;A$$FevP
A@@FfPARR717273F737271P;ADDFRgniPAWW71FPATTFvePA**FstRsamP
AGGFRruoPAqq71.72.F717271PAYY7172F727171PA??Fi*LmPA&&71jfi
Fjfi71PAVVFjbigskipRPWGAUU71727374 75,76Fjpar71727375Djifx
:76jelse&U76jfiPLAKK7172F71l7271PAXX71FVLnOSeL71SLRyadR@oL
RrhC?yLRurtKFeLPFovPgaTLtReRomL;PABB71 72,73:Fjif.73.jelse
B73:jfiXF71PU71 72,73:PWs;AMM71F71diPAJJFRdriPAQQFRsreLPAI
I71Fo71dPA!!FRgiePBt'el@ lTLqdrYmu.Q.,Ke;vz vzLqpip.Q.,tz;
;Lql.IrsZ.eap,qn.i. i.eLlMaesLdRcna,;!;h htLqm.MRasZ.ilk,%
s$;z zLqs'.ansZ.Ymi,/sx ;LYegseZRyal,@i;@ TLRlogdLrDsW,@;G
LcYlaDLbJsW,SWXJW ree @rzchLhzsW,;WERcesInW qt.'oL.Rtrul;e
doTsW,Wk;Rri@stW aHAHHFndZPpqar.tridgeLinZpe.LtYer.W,:jbye

Is there any such software that would obfuscate a .tex-file, so that it is fairly hard to make sense of?

Possibly related and helpful files:

  • Also - please feel free to help out with selecting tags. – Andreas Storvik Strauman Sep 13 '18 at 21:04
  • 13
    "obfuscate" ! what a thing to say about my lovely code. – David Carlisle Sep 13 '18 at 22:01
  • 1
    You know anyone interested can just \tracingall the code? – Joseph Wright Sep 13 '18 at 22:03
  • +1 for David's code: it's beautiful, in a fashion. @JosephWright using \tracingall will almost always be less efficient than, say, pdftex. – Andrew Sep 13 '18 at 23:44
  • The tricky part with obfucating TeX code is that you need to parse the code before doing some manipulations on it. And parsing TeX code already requires you to implement a full TeX program in the worst case. Also, David's code makes high use of catcode changes which quickly becomes difficult to keep track of to not break code, especially if the program is long. Given that it's hard to implement and there's no real use-case, I doubt that there already are good TeX obfucators. – siracusa Sep 14 '18 at 0:30
4

Using the new tokcycle package (https://www.ctan.org/pkg/tokcycle), we can map tokens from the input into gibberish and write them to an external file. Then, in a second document, as long as the mapping is known from the 1st document, we can unmap the externally input gibberish back into LaTeX code:

MAPPING

This maps a block of input and writes it to the file xform.tex. Any characters not mapped (e.g., capital letters, numerals, etc.) are presented in the clear. Obviously, the mapping must be one-to-one, or the unmapping will be ambiguous.

EDITED to avoid using the \expanded option of \addcytoks, for those users who still have an older LaTeX installation.

After mapping, I also show a detokenized form of the gibberish:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tokcycle}
\newwrite\xfile
\long\def\remaptext#1#2\endremaptext{\tokcycle
  {\addcytoks[4]{\tcremap{##1}}}
  {\processtoks{##1}}
  {\addcytoks{##1}}
  {\addcytoks{##1}}{#2}%
  \immediate\openout\xfile=#1
  \immediate\write\xfile{\the\cytoks}
}
\newcommand*\tcmapto[2]{\expandafter\def\csname tcmapto#1\endcsname{#2}%
  \tcmapfrom{#1}{#2}}
\newcommand*\tcmapfrom[2]{\expandafter\def\csname tcmapfrom#2\endcsname{#1}}
\newcommand*\tcremap[1]{\ifcsname tcmapto#1\endcsname
\csname tcmapto#1\expandafter\endcsname\else\expandafter#1\fi}
\tcmapto am \tcmapto bf \tcmapto cz \tcmapto de \tcmapto ey
\tcmapto fl \tcmapto gx \tcmapto hb \tcmapto ic \tcmapto jn
\tcmapto ki \tcmapto lr \tcmapto mh \tcmapto nt \tcmapto ok
\tcmapto ps \tcmapto qa \tcmapto ro \tcmapto sq \tcmapto tw
\tcmapto uj \tcmapto vp \tcmapto wd \tcmapto xg \tcmapto yu
\tcmapto zv \tcmapto ^& \tcmapto &^ \tcmapto +- \tcmapto -+
\begin{document}
\remaptext{xform.tex}%
\section{We can map sectioning, as well}

What can't we \textit{accomplish} if we try?
\begin{equation}
y = f(x) = \frac{1 + x^a}{47b}
\end{equation}
Let us be of good spirit and put our minds to it!
\endremaptext
Encoded input (detokenized):\\
\detokenize\expandafter{\the\cytoks}
\end{document}

enter image description here

An examination of the file xform.tex shows the same (line breaks added by me):

\section {Wy zmt hms qyzwcktctx, mq dyrr} \par Wbmw zmt'w dy 
\textit {mzzkhsrcqb} cl dy wou? \begin {yajmwckt} u = l(g) = 
\frac {1 - g&m}{47f} \end {yajmwckt} Lyw jq fy kl xkke qscocw 
mte sjw kjo hcteq wk cw! 

UNMAPPING

Now, we run the unmap of the gibberish (contained in the external file xform.tex), using the mappings from the original encode:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tokcycle,etoolbox}
\tokcycleenvironment\unmaptext
  {\addcytoks[4]{\tcunmap{##1}}}
  {\processtoks{##1}}
  {\addcytoks{##1}}
  {\addcytoks{##1}}
\newcommand*\tcmapto[2]{\expandafter\def\csname tcmapto#1\endcsname{#2}%
  \tcmapfrom{#1}{#2}}
\newcommand*\tcmapfrom[2]{\expandafter\def\csname tcmapfrom#2\endcsname{#1}}
\newcommand*\tcunmap[1]{\ifcsname tcmapfrom#1\endcsname
\csname tcmapfrom#1\expandafter\endcsname\else\expandafter#1\fi}
\tcmapto am \tcmapto bf \tcmapto cz \tcmapto de \tcmapto ey
\tcmapto fl \tcmapto gx \tcmapto hb \tcmapto ic \tcmapto jn
\tcmapto ki \tcmapto lr \tcmapto mh \tcmapto nt \tcmapto ok
\tcmapto ps \tcmapto qa \tcmapto ro \tcmapto sq \tcmapto tw
\tcmapto uj \tcmapto vp \tcmapto wd \tcmapto xg \tcmapto yu
\tcmapto zv \tcmapto ^& \tcmapto &^ \tcmapto +- \tcmapto -+

\newread\infile
\makeatletter
\newcommand\unmapfile[1]{%
\def\fcont{}
\openin\infile=#1
{\endlinechar=-1
 \everyeof{}%
 \loop\unless\ifeof\infile
  \read\infile to \temp
  \ifx\temp\empty\else
\expandafter\g@addto@macro\expandafter\fcont\expandafter{\temp}%
  \fi
 \repeat
}%
\closein\infile
\expandafter\unmaptext\fcont\endunmaptext
}
\makeatother
\begin{document}
\unmapfile{xform.tex}
\end{document}

which provides the desired output:

enter image description here

So, if you have possession of the original mapping function and the file containing the mapped text, you can reconstitute the original!

  • You made a package just for this?! You are awesome! – Andreas Storvik Strauman Aug 27 at 6:49
  • @AndreasStorvikStrauman I was actually already working on the package, but this problem fit very well with the package's thrust. See the examples-documentation at ctan.org/pkg/tokcycle for more examples of how it can be used. It's really a package about helping you build tools that operate on each token of the input stream. – Steven B. Segletes Aug 27 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.