Labels

new blog 2.0

2009/03/02

Trivial encryption with coreutils

We all know /bin/tr and /bin/dd, two very powerful tools, both are part of Linux' coreutils. Combined together by a Unix pipe they become even more powerful, here is how.

ROT13
Rot13 is a simple substitution cipher. It consists in shifting each character of the alphabet 13 places to the right. If a character goes beyond the original Z place, it is moved to the beginning of the alphabet and continues shifting from there. Effectively all characters are moved by 13 places, so that the alphabet starts now with N instead of A and ends with M instead of Z.

Substitution ciphers are considered weak, because they are prone to pattern matching and statistical attacks. It is good enough to hide the contents of the message from human eyes at the first glance. /bin/tr is a Linux core tool that can be used to translate the alphabet into it's rot13 equivalent.
$ tr A-Za-z N-ZA-Mn-za-m
hello world
uryyb jbeyq
^C
$
ENDIANNESS
This feature comes to us thanks to diversity of computer hardware. Little-endians address the 16-bit values in memory differently from from Big-endians, what sometimes causes incompatibility issues. Here /bin/dd comes comes to rescure with it's conversion flag:
$ dd conv=swab bs=1
hello world
ehll oowlr
d^C12+0 records in
12+0 records out
12 bytes (12 B) copied, 13.3088 s, 0.0 kB/s
Byte swapping is not an encryption method in itself, because swapped bytes in a word are still readable. An interesting thing about the swap, however, is the fact that if a human readable sentence delimited by spaces is byte-swapped the words, while still somehow readable, change the length.

TRIVIAL ENCRYPTION CYPHER
The fact that the two converisions named above are absolutely independent from each other allows us to combine them together. A message encrypted this way is a little less likely to be decoded with a statistical or pattern matching algorithm.
$ cat secret|tr A-Za-z N-ZA-Mn-za-m|dd conv=swab
uG rrfepgrz frnfrts ybbyfj
!0+1 records in
0+1 records out
28 bytes (28 B) copied, 0.00179017 s, 15.6 kB/s

No comments: