Python Forum

Full Version: Vernam encryption method for files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have the following code snippet:

import secrets

def VernamCipherFunction(text, key):
      result = "";
      ptr = 0;
      for char in text:
            result = result + chr(ord(char) ^ ord(key[ptr]));
            ptr = ptr + 1;
            if ptr == len(key):
                  ptr = 0;
      return result

otp = ''
for i in range(55):
     otp += secrets.choice(encryption_key)

while True:
    input_text = input("\nEnter File To Encrypt:\t");
    fh=open(input_text, "w")
    for encryption in fh:
        encryption=VernamCipherFunction(input_text, encryption_key)
    print("\nFile is encrypted");

The program runs okay, but during runtime the whole file was truncated when users tried to input filename.

Any suggestions from any users will be appreciated.
Its purpose of the program is to prevent users from obfuscating any app developer's source code.
However, its only obstacle that's preventing me from successfully encrypting a selected file was not to
truncate it; it was supposed encrypt it instead using the file handling techniques using the
write lines method of the file writing object.