Skip to content

Instantly share code, notes, and snippets.

@gevasiliou
Created October 6, 2019 21:30
Show Gist options
  • Save gevasiliou/dd91ddf649dada112f374ae0926a227a to your computer and use it in GitHub Desktop.
Save gevasiliou/dd91ddf649dada112f374ae0926a227a to your computer and use it in GitHub Desktop.
Encode / Decode Ascii Text by shifting chars
#!/usr/bin/env bash
function asciirotby {
case $1 in
"+1" | "-25") tr 'a-zA-Z' 'b-za-aB-ZA-A';; #Rotates by +1 , go to next letter (a becomes b) or rotate -25 (25 letters back)
"+2" | "-24") tr 'a-zA-Z' 'c-za-bC-ZA-B';; #Rotates by +2 , go to next 2 letters (a becomes c)
"+3" | "-23") tr 'a-zA-Z' 'd-za-cD-ZA-C';; #Ceasar Cipher - rotate by 3
"+4" | "-22") tr 'a-zA-Z' 'e-za-dE-ZA-D';;
"+5" | "-21") tr 'a-zA-Z' 'f-za-eF-ZA-E';;
"+6" | "-20") tr 'a-zA-Z' 'g-za-fG-ZA-F';;
"+7" | "-19") tr 'a-zA-Z' 'h-za-gH-ZA-G';;
"+8" | "-18") tr 'a-zA-Z' 'i-za-hI-ZA-H';;
"+9" | "-17") tr 'a-zA-Z' 'j-za-iJ-ZA-I';;
"+10" | "-16") tr 'a-zA-Z' 'k-za-jK-ZA-J';;
"+11" | "-15") tr 'a-zA-Z' 'l-za-kL-ZA-K';;
"+12" | "-14") tr 'a-zA-Z' 'm-za-lM-ZA-L';;
"+13" | "-13") tr 'a-zA-Z' 'n-za-mN-ZA-M';;
"+14" | "-12") tr 'a-zA-Z' 'o-za-nO-ZA-N';;
"+15" | "-11") tr 'a-zA-Z' 'p-za-oP-ZA-O';;
"+16" | "-10") tr 'a-zA-Z' 'q-za-pQ-ZA-P';;
"+17" | "-9") tr 'a-zA-Z' 'r-za-qR-ZA-Q';;
"+18" | "-8") tr 'a-zA-Z' 's-za-rS-ZA-R';;
"+19" | "-7") tr 'a-zA-Z' 't-za-sT-ZA-S';;
"+20" | "-6") tr 'a-zA-Z' 'u-za-tU-ZA-T';;
"+21" | "-5") tr 'a-zA-Z' 'v-za-uV-ZA-U';;
"+22" | "-4") tr 'a-zA-Z' 'w-za-vW-ZA-V';;
"+23" | "-3") tr 'a-zA-Z' 'x-za-wX-ZA-W';;
"+24" | "-2") tr 'a-zA-Z' 'y-za-xY-ZA-X';;
"+25" | "-1") tr 'a-zA-Z' 'z-za-yZ-ZA-Y';;
esac
}
@gevasiliou
Copy link
Author

gevasiliou commented Oct 6, 2019

Source the script running . ./rotatechars.sh
Then use the function like this: asciirotby +3 to rotate / encrypt each letter by +3 chars.
Alternativelly copy the function in your script. I use to keep functions like that in .bash_aliases since this file is sourced by default when shell is loaded.
Symbol + means add 3 chars , symbol - subtract 3 chars.
You can add or subtract any number of chars.

Example:

echo "This is a cool gist" |asciirotby +3
Wklv lv d frro jlvw

echo "Wklv lv d frro jlvw" |asciirotby -3
This is a cool gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment