SRC = sys.argv[1] OUT = sys.path[0] + "/../output/" + OUTFILENAME infile = open(SRC, 'r') outfile = open(OUT, 'w+') # provide some output print("Solving Vigenère cipher " + SRC + " with passphrase \'" + PSWD + "\'") # get data to decrypt data = str(infile.read()).lower() # break source text into as many chunks pswd length, store punctuation data subsect, punct = chunk(data, len(PSWD)) # get the shift value for each chunk of text shifts = [] for item in list(PSWD): shifts.append(ord(item) - LOWERCASE_A_ASCII) # decrypt subsections for index, string in enumerate(subsect): subsect[index] = caesar(string, -shifts[index]) # rebuild full decrypted string result = unchunk(subsect, punct) # write result to file, clean up, provide output outfile.write(result) infile.close() outfile.close() print("Decrypted file path: ./output/" + OUTFILENAME)
PSWD = list(sys.argv[2].lower()) SRC = sys.argv[1] OUT = sys.path[0] + "/../src/vcipher.txt" infile = open(SRC, 'r') outfile = open(OUT, 'w') # provide some output print("Encrypting file code/" + SRC + "...") # get source text from file srcStr = str(infile.read()).lower() # convert password to integers for shifting for index, char in enumerate(PSWD): PSWD[index] = ord(char) - LOWERCASE_A_ASCII # break source text into as many chunks as the password has chars subsect, punct = chunk(srcStr, len(PSWD)) # encrypt subsections for index, string in enumerate(subsect): subsect[index] = caesar(string, PSWD[index]) # rebuild full encrypted string result = unchunk(subsect, punct) # write result to file, clean up, provide output outfile.write(result) infile.close() outfile.close() print("Encrypted file path: code/src/vcipher.txt")
# caesar cipher with the given shift character import sys sys.path.append(sys.path[0] + "/../lib/") from cryptotools import caesar from cryptotools import LOWERCASE_A_ASCII # check for correct argument list if len(sys.argv) < 3: print("Error: arguments should be file to encrypt, then a shift value") sys.exit() # define some important things SHIFT = sys.argv[2] SRC = sys.argv[1] OUT = sys.path[0] + "/../src/ccipher.txt" infile = open(SRC, 'r') outfile = open(OUT, 'w') # provide some output print("Encrypting file code/" + SRC + "...") # gets a string to encrypt, then performs encryption srcStr = str(infile.read()).lower() result = caesar(srcStr, ord(SHIFT) - LOWERCASE_A_ASCII) # write result to file, clean up, provide output outfile.write(result) infile.close() outfile.close() print("Encrypted file path: code/src/ccipher.txt")
print("Argument Error! Expected format:\n" + "python3 breakcaesar.py to_decrypt.txt output_name.txt") sys.exit() # define some important things OUTFILENAME = sys.argv[2] SRC = sys.argv[1] OUT = sys.path[0] + "/../output/" + OUTFILENAME infile = open(SRC, 'r') outfile = open(OUT, 'w+') # provide some output print("Breaking encryption on file " + SRC + "...") # get data to decrypt data = str(infile.read()).lower() # determine shift value shift = getShift(data) # provide some output print("Shift value found: " + chr(shift + LOWERCASE_A_ASCII)) # perform decryption result = caesar(data, -shift) # write result to file, clean up, provide output outfile.write(result) infile.close() outfile.close() print("Decrypted file path: ./output/" + OUTFILENAME)