0

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

1 Answers1

0

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.

Iluvatar
  • 1,537
  • 12
  • 13
  • But i want to be able to input any file so if someone gave me an encrypted file with any random name i wouldn't have to go into the code to change the open file name. so i was wondering if there is a way of doing this with sys.argv[] – Zwei September Phantom Dec 06 '16 at 10:26
  • The same way you get the input. So just replace `"read_file.txt"` with `sys.argv[1]` for example, and run your code with `python cipher.py file.txt 7`. You can replace the output file the same way. – Iluvatar Dec 06 '16 at 10:28
  • okay i understand that thanks but where exactly should i place this piece of code within my code if you don't mind helping me with that sorry. – Zwei September Phantom Dec 06 '16 at 10:29
  • Okay i have edited my code but i got myself all confused i get an error saying in_filename not defined – Zwei September Phantom Dec 06 '16 at 10:44
  • Don't modify your `decrypt()` function. It was fine before. Add all the stuff in the answer after it. – Iluvatar Dec 06 '16 at 10:46
  • okay its working now thanks what should i get rid of to stop getting the plain text displayed – Zwei September Phantom Dec 06 '16 at 10:51
  • Remove the print statement where it prints the decrypted text. – Iluvatar Dec 06 '16 at 10:52
  • when i run the program it decrypts whats in the file into another file but it also encrypts the name of the file itself so when i input in the terminal something like python cipher.py output.txt 7 newoutput.txt it will print out on the terminal output.txt hnminm.mqm – Zwei September Phantom Dec 06 '16 at 10:58
  • do i need to get rid of return plain ? – Zwei September Phantom Dec 06 '16 at 10:59
  • Uhh, in the code you have in your question currently, you have `cipher = sys.argv[1]`, which is the input file, and then you decrypt the filename and print it out. If you don't need anything printed out, remove the last 5 lines. – Iluvatar Dec 06 '16 at 11:03
  • oh thats great thanks man. Hey i was just wondering and i have no idea if something like that is even possible but what if i didn't know the key is it actually possible to create something in my program that will find the key for me or am i just making stuff up ? – Zwei September Phantom Dec 06 '16 at 11:09
  • Well that would involve making a program that tries all the keys and can determine once it's decrypted something whether it makes sense. Two options for that probably, either look for common words and if they exist it's probably decrypted, or start out each message with a specific character for example, then by looking at that you know what the key is. (The second way is insecure really, as anyone who knows what the first character is can break it, but you're using a Caesar cipher anyway so...) – Iluvatar Dec 06 '16 at 11:13
  • You wouldn't be able to help me with creating something like that would you i have tried creating a character count program but its meh im just trying to learn more about decryption and cease cipher. – Zwei September Phantom Dec 06 '16 at 11:15
  • I think this is something to figure out yourself. Look into finding substrings (http://stackoverflow.com/questions/7361253/how-to-determine-whether-a-substring-is-in-a-different-string) and take it from there. – Iluvatar Dec 06 '16 at 11:21