Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #FILE USAGE:
- #python3 <thisfile.py> <key> <message> [-d for decoding (optional)]
- import sys;
- strs = 'abcdefghijklmnopqrstuvwxyz1234567890 ' #use a string like this, instead of ord()
- def shifttext(shift,inp):
- data = []
- for i in inp: #iterate over the text not some list
- if i.strip() and i in strs: # if the char is not a space ""
- data.append(strs[(strs.index(i) + shift) % len(strs)])
- else:
- data.append(i) #if space the simply append it to data
- output = ''.join(data)
- return output
- def gentable():
- table = [];
- for i in range(0,len(strs)+1):
- table.append(shifttext(i,strs));
- return table;
- def getIndex(letter):
- x = 0;
- for i in strs:
- if letter == i:
- return x;
- x += 1;
- return 39;
- table = gentable();
- def getAlphabet(index):
- return table[index];
- def getLetterD(index,letter):
- for alph in table:
- if alph[index] == letter:
- return alph[0];
- def getLetter(index,alphabet):
- return alphabet[index];
- def encode(toEncode_,codeword_):
- i=0;
- word = '';
- for c in toEncode_:
- v = codeword_[i];
- word += getLetter(getIndex(v),getAlphabet(getIndex(c)));
- i+=1;
- if i >= len(codeword_):
- i = 0;
- return(word);
- def decode(toDecode_,codeword_):
- i=0;
- word = '';
- for c in toDecode_:
- v = codeword_[i];
- word+=getLetterD(getIndex(v),c);
- i+=1;
- if i >= len(codeword_):
- i = 0;
- return(word);
- codeword = ''
- esd = ''
- decodeM = False
- if len(sys.argv) >= 2:
- codeword = sys.argv[1];
- esd = ''
- decodeM = False;
- for i in range(2,len(sys.argv)):
- if sys.argv[i] == '-d':
- decodeM = True
- else:
- esd += sys.argv[i] + " ";
- if (esd == ''):
- print("Usage: python3 {0} code input [-d (DECODE MODE)]".format(sys.argv[0]));
- sys.exit();
- else:
- print("Usage: python3 {0} code input [-d (DECODE MODE)]".format(sys.argv[0]));
- sys.exit();
- esd = esd[:-1]
- if (decodeM):
- print('"'+decode(esd,codeword)+'"');
- else:
- print('"'+encode(esd,codeword)+'"');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement