Hey guys i have started doing some python coding and i was able to create this program that decrypts the encrypted text i provide with the use of the key i provide can someone help me change this decryption of text into decryption of files.
import sys
def decrypt(cipher, key):
plain = ""
for index in range(len(cipher)):
if cipher[index].isalpha():
if cipher[index].isupper():
plain = plain + chr((ord(cipher[index]) - 64 - key) % 26 + 64)
elif cipher[index].islower():
plain = plain + chr((ord(cipher[index]) - 96 - key) % 26 + 96)
else:
plain = plain + cipher[index]
return plain
in_filename = sys.argv[1]
key = int(sys.argv[2])
out_filename = sys.argv[3]
with open(in_filename, "r") as f:
encrypted = f.read()
decrypted = decrypt(encrypted, key)
with open(out_filename, "w+") as f:
f.write(decrypted)
cipher = sys.argv[1]
plain = sys.argv[1]
key = int(sys.argv[2])
print('{}'.format(cipher))
print('{}'.format(decrypt(cipher, key)))
i can use my current program by typing in this command in the terminal
python cipher.py 'Yaholy' 7 which decrypts this word into Rather
You can use what you already have, and just read from a file and then write the result back out.
in_filename = sys.argv[1]
key = int(sys.argv[2])
out_filename = sys.argv[3] # if you want
with open(in_filename, "r") as f:
encrypted = f.read()
decrypted = decrypt(encrypted, key)
with open(out_filename, "w+") as f:
f.write(decrypted)
print(decrypted) # if you don't want to save to a file
Then you can call it with, for example, python cipher.py encrypted.txt 7 decrypted.txt
.