Command Line Cryptography

For a while, I've been wanting to have some simple tools for encryption and decryption of simple ciphers such as monoalphabets such as Caesar, atbash, ROT13 and so on. I thought I would have to write them myself. Fortunately I came across example 12-18 on this site and it's problem solved.

[sourcecode language="bash"] #!/bin/bash

# Will encrypt famous quotes in a simple monoalphabetic substitution. # The result is similar to the "Crypto Quote" puzzles #+ seen in the Op Ed pages of the Sunday paper.

# http://www.faqs.org/docs/abs/HTML/textproc.html

key=NOPQRSTUVWXYZABCDEFGHIJKLM # The "key" is nothing more than a scrambled alphabet. # Changing the "key" changes the encryption.

echo "If you have not specified a file, type your input, when done, enter ctrl-D" echo ""

# The 'cat "$@"' construction gets input either from stdin or from files. # If using stdin, terminate input with a Control-D. # Otherwise, specify filename as command-line parameter.

cat "$@" | tr "a-z" "A-Z" | tr "A-Z" "$key" # | to uppercase | encrypt # Will work on lowercase, uppercase, or mixed-case quotes. # Passes non-alphabetic characters through unchanged.

# to decrypt # cat "$@" | tr "$key" "A-Z"

exit 0 [/sourcecode]

I use OS X, so I saved this into a file called 'ROT13', and made it executable by going to the terminal and typing

[sourcecode language="bash"] chmod 755 ROT13 [/sourcecode]

In the terminal, I can execute the file by going to the directory containing the file and typing ./ROT13 (I haven't put my scripts directory in the path yet)

I type my input, hit return, then ctrl-D and return.... voila!

Changing 'KEY' will produce a different encryption. For example atbash uses this key: ZYXWVUTSRQPONMLKJIHGFEDCBA

Now, the next step is to write a script to create blocks of N characters (default, 5) - and a command line vigenere. Hmm, that's harder!