Python Forum
Vernam encryption method for files - Printable Version

+- Python Forum (
+-- Forum: Python Coding (
+--- Forum: General Coding Help (
+--- Thread: Vernam encryption method for files (/Thread-Vernam-encryption-method-for-files)

Vernam encryption method for files - JohnCTX - Sep-18-2019

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.

RE: Vernam encryption method for files - JohnCTX - Sep-18-2019

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.