Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- * Author: Abszoluto
- * Code made with Python 3.7.0
- * What does that do ?
- This is a code that will open a file and cryptograph its content
- The cryptograph method is Vigenere Cypher
- * Version 2.0
- Added more safe inverting the array wich contains the characters cryptographed (after using Vigenere Cypher)
- """
- import io
- key = "0DB20660AFDE446C5722CCF2F5256999" # I am using a hash to use as the key for Vigenere Cypher
- inputfile = "content.txt" # Change here the name of the file that you want to cryptograph
- outputfile = "cryptcontent.txt" # Change here the name of the file that you want to decypher
- def code(filename):
- """
- * This method will open a file, cryptograph its content and save it in a file 'cryptcontent.txt'
- """
- characterAtKey = 0
- content = []
- file = open(inputfile,"r")
- with io.open(outputfile, "w", encoding="utf-8") as file2:
- while True:
- result = file.read(1) # If there isnt something to read, result will be 0
- if (not result):
- content = content[::-1] # Inverting the array with the cryptographed content
- for character in content:
- file2.write(chr(character)) # Write every single character at the file "filename"
- print("File coded sucessfully as 'cryptcontent.txt'.. \n")
- file.close()
- file2.close()
- break;
- content.append(ord(result)+ord(key[characterAtKey])) # adding the cryptographed character(value of (character) + value of (key[position]) ) at the array content
- characterAtKey = (characterAtKey + 1)%len(key) # next character at the key, the mod will go back at the first character when there isnt more characters to use
- def decode():
- """
- * This method will open a file cryptographed, decipher its content and print it at the console
- """
- decode = ""
- characterAtKey = 0
- with io.open(outputfile, "r", encoding="utf-8") as file:
- print("\nDECODING... \n")
- result = file.read() # reading the whole file
- result = result[::-1] # reversing the file content
- for charactere in result:
- decode = decode + (chr((ord(charactere)-ord(key[characterAtKey])))) # Concatenating the string decode with the character decypher
- characterAtKey = (characterAtKey + 1)%len(key) # next character at the key, the mod will go back at the first character when there isnt more characters to use
- file.close()
- print("FILE SUCESSFULLY DECODED: \n")
- print(decode)
- while True:
- """
- * Main Menu
- * Type Code for code a file with the name content.txt
- * Type decode to decode a file with the name of "cryptcontent.txt"
- * Type anything else to leave
- """
- option = input("Do you wanna 'code', 'decode' or 'leave' ? ")
- if option.lower() == "code":
- code(inputfile)
- elif option.lower() == "decode":
- decode()
- else:
- break
- input("Press any button to continue...")
Add Comment
Please, Sign In to add comment