Created
September 17, 2016 16:24
-
-
Save mrummuka/348393c8a8b704d8d2cb5749f9c04295 to your computer and use it in GitHub Desktop.
pycipher-cli for executing pycipher library from command-line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# cli for pycipher (https://github.com/jameslyons/pycipher) | |
# | |
from __future__ import print_function | |
import pycipher | |
from pycipher import ADFGX | |
from pycipher import Affine | |
from pycipher import Atbash | |
from pycipher import Autokey | |
from pycipher import Beaufort | |
from pycipher import Bifid | |
from pycipher import ColTrans | |
from pycipher.caesar import Caesar | |
from pycipher.enigma import Enigma | |
from pycipher import Foursquare | |
from pycipher import Gronsfeld | |
from pycipher.m209 import M209 | |
from pycipher.playfair import Playfair | |
from pycipher import PolybiusSquare | |
from pycipher.porta import Porta | |
from pycipher.railfence import Railfence | |
from pycipher import Rot13 | |
from pycipher.simplesubstitution import SimpleSubstitution | |
from pycipher import Vigenere | |
import sys, getopt | |
def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) | |
def main(argv): | |
verbose = False | |
encrypt = False | |
cipher = '' | |
key = '' | |
keysquare = '' | |
msg = '' | |
try: | |
opts, args = getopt.getopt(argv,"hdec:k:m:q:v",["decrypt","encrypt","cipher=","key=","msg=","keysquare=", "verbose"]) | |
except getopt.GetoptError: | |
print ('pycipher-cli for executing ciphers implemented in pycipher library from command-line') | |
print (' To decrypt msg with cipher using key when required by cipher, run') | |
print (' python pycipher-cli.py -d -c <cipher> [-k <key>] -m <ciphertext>') | |
print (' To encrypt msg with cipher using key when cipher requires one, run:') | |
print (' python pycipher-cli.py -e -c <cipher> [-k <key>] -m <plaintext>') | |
sys.exit(2) | |
for opt, arg in opts: | |
if opt == '-h': | |
print ('pycipher-cli for running pycipher from command-line') | |
print (' To decrypt msg with cipher using key when required by cipher, run') | |
print (' python pycipher-cli.py -d -c <cipher> [-k <key>] [--keysquare=<ADFGVX keysquare>] -m <ciphertext> ') | |
print (' To encrypt msg with cipher using key when cipher requires one, run:') | |
print (' python pycipher-cli.py -e -c <cipher> [-k <key>] [--keysquare=<ADFGVX keysquare>] -m <plaintext> ') | |
sys.exit() | |
elif opt in ("-d", "--decrypt"): | |
encrypt = False | |
elif opt in ("-e", "--encrypt"): | |
encrypt = True | |
elif opt in ("-v", "--verbose"): | |
verbose = True | |
elif opt in ("-c", "--cipher"): | |
cipher = arg.lower() | |
elif opt in ("-k", "--key"): | |
key = arg | |
elif opt in ("-q", "--keysquare"): | |
keysquare = arg | |
elif opt in ("-m", "--msg"): | |
msg = arg | |
if verbose: | |
print ('Using cipher: '+cipher+', encrypt='+str(encrypt)+ ', key: ' + key + ', msg: ' + msg) | |
# new if | |
if cipher == 'rot13': | |
if encrypt: res = Rot13().encipher(msg) | |
else: res = Rot13().decipher(msg) | |
elif cipher == 'adfgvx': | |
if keysquare=='': | |
eprint ('ERROR: keysquare not set for adfgvx') | |
sys.exit(1) | |
elif encrypt: | |
keys=((keysquare, key)) | |
res = pycipher.ADFGVX(*keys).encipher(msg) | |
else: | |
keys=((keysquare, key)) | |
res = pycipher.ADFGVX(*keys).decipher(msg) | |
elif cipher == 'adfgx': | |
if keysquare=='': | |
eprint ('ERROR: keysquare not set for adfgx') | |
sys.exit(1) | |
elif encrypt: | |
keys=((keysquare, key)) | |
res = ADFGX(*keys).encipher(msg) | |
else: | |
keys=((keysquare, key)) | |
res = ADFGX(*keys).decipher(msg) | |
elif cipher == 'affine': | |
res = 'affine: TODO' | |
elif cipher == 'atbash': | |
if encrypt: res = Atbash().encipher(msg) | |
else: res = Atbash().decipher(msg) | |
elif cipher == 'autokey': | |
if encrypt: res = Autokey(key).encipher(msg) | |
else: res = Autokey(key).decipher(msg) | |
elif cipher == 'beaufort': | |
if encrypt: res = Beaufort(key).encipher(msg) | |
else: res = Beaufort(key).decipher(msg) | |
elif cipher == 'bifid': | |
if encrypt: res = Bifid(key).encipher(msg) | |
else: res = Bifid(key).decipher(msg) | |
elif cipher == 'caesar': | |
if encrypt: res = Caesar(int(key)).encipher(msg) | |
else: res = Caesar(int(key)).decipher(msg) | |
elif cipher == 'coltrans': | |
if encrypt: res = ColTrans(key).encipher(msg) | |
else: res = ColTrans(key).decipher(msg) | |
elif cipher == 'enigma': | |
res = "ENIGMA : TODO" | |
elif cipher == 'foursquare': | |
if encrypt: res = Foursquare(key).encipher(msg) | |
else: res = Foursquare(key).decipher(msg) | |
elif cipher == 'gronsfeld': | |
# syntax: -k 2,7,8,1,3,9,3 | |
keyarr = [] | |
try: | |
keyarr = map(int, key.split(",")) | |
except ValueError: | |
eprint ('ERROR: Syntax error in key "' + key + '", use e.g. -k 2,3,7,1,13 for Gronsfeld') | |
sys.exit(1) | |
if encrypt: res = Gronsfeld(keyarr).encipher(msg) | |
else: res = Gronsfeld(keyarr).decipher(msg) | |
elif cipher == 'm209': | |
if encrypt: res = M209(key).encipher(msg) | |
else: res = M209(key).decipher(msg) | |
elif cipher == 'playfair': | |
if encrypt: res = Playfair(key).encipher(msg) | |
else: res = Playfair(key).decipher(msg) | |
elif cipher == 'polybius': | |
if encrypt: res = PolybiusSquare(key).encipher(msg) | |
else: res = PolybiusSquare(key).decipher(msg) | |
elif cipher == 'porta': | |
if encrypt: res = Porta(key).encipher(msg) | |
else: res = Porta(key).decipher(msg) | |
elif cipher == 'railfence': | |
if encrypt: res = Railfence(int(key)).encipher(msg) | |
else: res = Railfence(int(key)).decipher(msg) | |
elif cipher == 'simple': | |
if encrypt: res = SimpleSubstitution(key).encipher(msg) | |
else: res = SimpleSubstitution(key).decipher(msg) | |
elif cipher == 'vigenere': | |
if encrypt: res = Vigenere(key).encipher(msg) | |
else: res = Vigenere(key).decipher(msg) | |
else: | |
eprint ('ERROR: unsupported cipher: ' + cipher) | |
sys.exit(2) | |
print (res) | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment