#!/usr/bin/python3 from HMlib.colorLog import * import sys import argparse # converting function def shiftASCII(x, n, cap): # if char is capital, cap flag is True. if cap: minChr, maxChr = "A", "Z" else: minChr, maxChr = "a", "z" # char shift result = ord(x) + n # case that result char is over "Z" or "z". if result > ord(maxChr): result = (ord(minChr) - 1) + result % (ord(maxChr)) return chr(result) def rotN(text, N): result = [] for i in text: intOfI = ord(i) # case that char is capital. if 65 <= intOfI <= 90: result.append(shiftASCII(i, N, True)) # case that char is not capital. elif 97 <= intOfI <= 122: result.append(shiftASCII(i, N, False)) else: result.append(i) return "".join(result) if __name__=='__main__': # parse args parser = argparse.ArgumentParser(description="IT IS CAESAR CHIPHER TOOL") parser.add_argument('-d', '--data', type=str, default=None, help='string of caesar crypto') parser.add_argument('-i', '--input_file_name', type=str, default=None, help='input file') parser.add_argument('-o', '--output_file_name', type=str, default=None, help='output file') args = parser.parse_args() data = args.data input_file_name = args.input_file_name output_file_name = args.output_file_name cipherText = '' # read from input_file or data if input_file_name is not None: with open(input_file_name, 'r') as f: cipherText = f.read() else: cipherText = data # display result for i in range(26): # ROT13 if i == 13: result("rot{0:>2} ==> {1}".format(i, rotN(cipherText, i))) else: info("rot{0:>2} ==> {1}".format(i, rotN(cipherText, i))) # write to output_file if output_file_name is not None: with open(output_file_name, 'w') as f: for i in range(26): f.write("rot{0:>2} ==> {1}\n".format(i, rotN(cipherText, i)))