diff --git a/caesar.py b/caesar.py index 9b1e6d4..b78c77e 100644 --- a/caesar.py +++ b/caesar.py @@ -1,12 +1,13 @@ -import sys +from lib.input import * from lib.vars import alphabet +from lib.vars import listToString -if( len(sys.argv) >= 4 ): - in_key = int(sys.argv[1]) - in_txt = sys.argv[2] - in_alphabet = sys.argv[3] +if( inputHasKeys(["-k", "-i", "-a"]) ): + in_key = int(getValueOfKey("-k")) + in_txt = getValueOfKey("-i") + in_alphabet = getValueOfKey("-a") else: - print("file.py {int KEY} {string TXT} {string ALPHABET_TYPE}") + print("file.py -k {int KEY} -i {string TXT} -a {string ALPHABET_TYPE}") exit() alen = len(alphabet[in_alphabet]) @@ -16,7 +17,8 @@ decryp_list = [""] * len(in_txt) for char in txt_list: index = alphabet[in_alphabet].index(char) - + print("Decrypting char-index: " + str(txt_list.index(char)) + " (" + char + ")") + index = index + in_key # shift the alphabet while( index >= alen ): #cycle through the alphabet diff = (index + in_key) - (alen - 1) @@ -24,4 +26,4 @@ for char in txt_list: decryp_list[txt_list.index(char)] = alphabet[in_alphabet][index] -print(decryp_list) +print( "Output: " + listToString(decryp_list) ) diff --git a/lib/__pycache__/input.cpython-38.pyc b/lib/__pycache__/input.cpython-38.pyc new file mode 100644 index 0000000..599bc6f Binary files /dev/null and b/lib/__pycache__/input.cpython-38.pyc differ diff --git a/lib/__pycache__/vars.cpython-38.pyc b/lib/__pycache__/vars.cpython-38.pyc index e012f29..12b3f1a 100644 Binary files a/lib/__pycache__/vars.cpython-38.pyc and b/lib/__pycache__/vars.cpython-38.pyc differ diff --git a/lib/input.py b/lib/input.py new file mode 100644 index 0000000..d5690f7 --- /dev/null +++ b/lib/input.py @@ -0,0 +1,10 @@ +import sys + +def getValueOfKey(key): # get the value of an input key, example: -c {VALUE} + for i in sys.argv: + if( i == key ): + return sys.argv[sys.argv.index(key) + 1] + return -1 # if no value was found return -1 + +def inputHasKeys(keyList): + return all(key in sys.argv for key in keyList) diff --git a/lib/vars.py b/lib/vars.py index 995c07c..0739f7f 100644 --- a/lib/vars.py +++ b/lib/vars.py @@ -10,3 +10,10 @@ alphabet["ENG"] = eng_alphabet alphabet["SWE"] = swe_alphabet # Functions +def listToString( l ): + returnStr = "" + + for char in l: + returnStr += char + + return returnStr