Question
Please Have a look at my coding according to this question there is some mistakes help me with that. question is... Write a Python program,
Please Have a look at my coding according to this question there is some mistakes help me with that.
question is...Write a Python program, implementing encryption/decryption without using any special security packages. The program should ask the user for encryption method, if you want to perform encryption or decryption, secret key and a text file to process. The output should be a processed file. You should implement at least two simple encryption methods, one substitution and one transposition method. For the substitution method, keep key sizes at maximum equivalent to eight bits, i.e. 28 =256 different possible keys that corresponds to one character. Make sure that you can both encrypt and decrypt files with your program.
coding is,.....
from collections import deque
import collections
import string
import math
from typing import Text
def caesar_encrypt(key, text):
alphabet_deque = collections.deque(string.ascii_uppercase + string.digits + string.punctuation)
alphabet_string = string.ascii_uppercase + string.digits + string.punctuation
A = 0
alphabet_deque.rotate(key)
Ciphertext = ""
for x in text:
if x == " ":
Ciphertext += " "
else:
A = alphabet_string.index(x)
Ciphertext += alphabet_deque[A]
return Ciphertext
def caesar_decrypt(key, text):
alphabet_deque = collections.deque(string.ascii_uppercase + string.digits + string.punctuation)
alphabet_list = string.ascii_uppercase + string.digits + string.punctuation
A = 0
alphabet_deque.rotate(key)
Ciphertext = ""
for x in text:
if x == " ":
Ciphertext += " "
else:
A = alphabet_deque.index(x)
Ciphertext += alphabet_list[A]
return Ciphertext
def encrypt(key, message): #Transposition
dummy = ((len(message) + len(key)) - (len(message) % len(key))) - len(message)
dummy_message = key + message + ("*" * dummy)
cols = int(len(key))
counter = 0
rows = math.ceil((len(dummy_message) / len(key)))
ciphertext = ""
for x in range(0, cols, 1):
counter = x
for y in range(0, rows):
ciphertext += dummy_message[counter]
counter += len(key)
return ciphertext
# Reversed encryption algorithm
def decrypt(key, ciphertext):#Transposition
cols = int(len(key))
counter = 0
counter2 = 0
rows = math.ceil((len(ciphertext) / len(key)))
text = [""] * len(ciphertext)
endtext = ""
for x in range(0, cols, 1):
counter = x
for y in range(0, rows):
text[counter] = ciphertext[counter2]
counter2 += 1
counter += len(key)
for i in range(len(key), len(ciphertext)):
endtext += text[i]
return endtext
def read_file(file_path):
lst = []
whole = ""
with open(file_path, "r") as file:
for line in file:
n = line.strip()
if n.find(", ") == -1:
n = n.split(":")
else:
n = n.split(", ")
for x in n:
if x != "":
lst.append(x)
for w in lst:
whole += w
return whole
#Used this function for clear previous messages
def write_file1(file_path, words):
with open(file_path, "w", encoding="UTF-8") as file:
word1 = ""
for word in words:
file.write(word)
return file
# Difference between first write fuction is(append)
def write_file(file_path, words):
with open(file_path, "a", encoding="UTF-8") as file:
for word in words:
file.write(word)
return file
import os
from typing import Text
import ciphers
path = os.getcwd()
right_path_A = path + "\\original_text.txt"
right_path_B = path + "\\cipher_text.txt"
right_path_C = path + "\\decrypted_message.txt"
encryption_method = int(input("Ceasar cipher: 1 Transposition cipher: 2 = "))
enc_decr = int(input("Encryptin: 1 Decryption: 2 = "))
word = ""
#My outputs will be written in one line. Because the alghorithm that I developed is simple.
#The program that asks user to inputs
if encryption_method == 1:
if enc_decr == 1:
ciphers.write_file1(right_path_B, "")# To delete previous message
key = int(input("Please enter the key :"))
word = ciphers.read_file(right_path_A)
word = ciphers.caesar_encrypt(key, str(word).upper())
ciphers.write_file(right_path_B, word)
else:
ciphers.write_file1(right_path_C, "")# To delete previous message
key = int(input("Please enter the key :"))
word = ciphers.read_file(right_path_B)
word = ciphers.caesar_decrypt(key, str(word).upper())
ciphers.write_file(right_path_C, word)
else:
if enc_decr == 1:
ciphers.write_file1(right_path_B, "") # To delete previous message
key = str(input("Please enter the text key :"))
word = ciphers.read_file(right_path_A)
word = ciphers.encrypt(key, str(word).upper())
ciphers.write_file(right_path_B, word)
else:
ciphers.write_file1(right_path_C, "")# To delete previous message
key = str(input("Please enter the text key :"))
word = ciphers.read_file(right_path_B)
word = ciphers.decrypt(key, str(word).upper())
ciphers.write_file(right_path_C, word)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started