Sunday, May 18, 2008

Perform Elementary Encryption with tr Command

Reading the "Stars and Stripes" newspaper today, I ran across the popularly syndicated JUMBLE puzzle, which is a scrambled word game. Interestingly enough, the versatile UNIX tr command came to mind again. By providing a jumbled alphabet key, the UNIX tr command can perform an elementary encryption operation. Here is a simple example.

To "encrypt" the contents of mysysad.txt in uppercase.
# sh
# cat mysysad.txt | tr '[a-z]' '[A-Z]' | tr '[A-Z]' "PETRUSCMANFXZJOIVKBGWHDYLQ" > mysys.txt

# more mysys.txt
GMU GKPJBXPGU WGAXAGL IKOHARUB HUKBPGAXGL DAGMAJ WJAY.

To "decrypt" the contents of mysys.txt in lowercase (original state)
# cat mysys.txt | tr "PETRUSCMANFXZJOIVKBGWHDYLQ" '[a-z]'

Or use the echo command per Denis' (commenter) suggestion
# echo "GMU GKPJBXPGU WGAXAGL IKOHARUB HUKBPGAXGL DAGMAJ WJAY." | tr 'PETRUSCMANFXZJOIVKBGWHDYLQ' '[a-z]'

4 comments:

Denis said...

mmh ..
echo "GMU GKPJBXPGU WGAXAGL IKOHARUB HUKBPGAXGL DAGMAJ WJAY." | tr 'PETRUSCMANFXZJOIVKBGWHDYLQ' '[a-z]'
gives:
sgd sq[mrk[sd tshkhsx oqnuhcdr udqr[shksx vhsghm tmhw.

Perhaps try:
echo "GMU GKPJBXPGU WGAXAGL IKOHARUB HUKBPGAXGL DAGMAJ WJAY." | tr 'PETRUSCMANFXZJOIVKBGWHDYLQ' 'a-z'
in place of the previous command

Thanks for this reminder!
ddm

esofthub said...

Hi Denis,
--
echo "GMU GKPJBXPGU WGAXAGL IKOHARUB HUKBPGAXGL DAGMAJ WJAY." | tr 'PETRUSCMANFXZJOIVKBGWHDYLQ' '[a-z]'
--

I tested the aforementioned and it works the same as the cat mysys.txt output.

Btw, I tested the example on bash and sh using a Solaris 8 box.

Thank you for your comment.

Matt said...

Cyrnfr qba'g hfr guvf sbe nalguvat vzcbegnag


+13

Cosect said...

I´m using discryptor.net to encrypt my data. It is userfriendly and really fast.