Python/ciphers/transposition_cipher_encrypt_decrypt_file.py

37 lines
1.1 KiB
Python
Raw Normal View History

2018-10-19 12:48:28 +00:00
import time, os, sys
import transposition_cipher as transCipher
def main():
inputFile = 'Prehistoric Men.txt'
outputFile = 'Output.txt'
2018-10-20 19:45:08 +00:00
key = int(input('Enter key: '))
mode = input('Encrypt/Decrypt [e/d]: ')
2018-10-19 12:48:28 +00:00
if not os.path.exists(inputFile):
print('File %s does not exist. Quitting...' % inputFile)
sys.exit()
if os.path.exists(outputFile):
print('Overwrite %s? [y/n]' % outputFile)
2018-10-20 19:45:08 +00:00
response = input('> ')
2018-10-19 12:48:28 +00:00
if not response.lower().startswith('y'):
sys.exit()
2018-10-19 12:48:28 +00:00
startTime = time.time()
if mode.lower().startswith('e'):
with open(inputFile) as f:
content = f.read()
2018-10-19 12:48:28 +00:00
translated = transCipher.encryptMessage(key, content)
elif mode.lower().startswith('d'):
with open(outputFile) as f:
content = f.read()
2018-10-19 12:48:28 +00:00
translated =transCipher .decryptMessage(key, content)
with open(outputFile, 'w') as outputObj:
outputObj.write(translated)
2018-10-19 12:48:28 +00:00
totalTime = round(time.time() - startTime, 2)
print(('Done (', totalTime, 'seconds )'))
2018-10-19 12:48:28 +00:00
if __name__ == '__main__':
main()