Python Forum
Vernam encryption method for files - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: General Coding Help (https://python-forum.io/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

encryption_key="g2049ih259gh49uh4509uh409uh0e89ru09UEWHF938RUR0E19H1R309FUH109HR1309HGUU98H9ueghr9u4h09hr49h9grut4h209rhOVJEDFHFE8H9R3UHGH8H2UH2EH29HG9H9UP(UHU9uh8UhUHu89hzxuU89XXCU9csvhuifeqph9fqbeo3gr8uhf8uh8u8zxcg8UOH8D21H2E8HFE278HE837E782FH29H297H2F9H27187ey872f874873qrev8gr18gr87*&&^*&^T76gf8g8g871d38oG76g187gd2876gr377264gr8731g78fr31r89834gg82284ruhg8uh48uh8u284uh28hr28uh(*G(&YGy7gf1398r319r3g8ygr8e8y(&*G987e3gf978egry8g98YG7YFEGWR7Y91GREY7G19R87G2897gg6efw9re1g9e1g997FE3GR92R8EE298F7YG2E8G18HG8yf3r8gre2982r472gr2489rgh87h17(*YG(*G(&^&^9^&*&^&^f&^F&^f87F676f7YF&^%R&)(*&*&Y(*G&FG&TF^F^%R%R&%&^DR%^D%$D%D%$EXYFCKHGVKJHBI*YT&^76t76r65r65D^RD&^54d%$E^%43w43W5@Q#@a%$#s6%$ES%&s%&D&^d^%RE^%*TF&IF&F*^f*Go8GOYG&F%D$#S@A@A4gOUIULIBLIBO(G^F^%^%E&^$%E&"
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:
        fh.writelines=encryption
        encryption=VernamCipherFunction(input_text, encryption_key)
    print("\nFile is encrypted");
    fh.close()
    break
exit
    


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.