python – first program: encryption

This is the first code I write that is not just an exercise, but a complete program that I can actually execute.

Features :

  • Generate pairs of letters / numbers at random for each "printable" ASCII character
  • Save those pairs in a brine file
  • Encode / decode any string based on that
  • Basic user interface

I would greatly appreciate any advice, comment, comment that you can give me about it, since I have been learning to program only for a couple of months. Did I structure it well? Is this how you are supposed to put together a program? Is it commented well and is it enough …?

Also, I realize that it is probably a strange way to encrypt anything and there is surely a better / safer / easier way to perform encryption, but it was mainly an excuse to write the program.

Many thanks in advance to anyone who is willing to give me their time and experience!

Here it goes :

from the import chain to print, 
ascii_letters, 
ascii_lowercase, 
ascii_uppercase
import randomly
import gherkin

def generate_code ():
# Generates a random number for each printable ASCII character,
# Returns a list of tuples (characters, numbers) for each pair
characters = printable
numbers = random. sample (range (len (characters) + 1000), len (characters))
code = list (zip (characters, numbers))
return code

encode def (string, code):
# Replace each string character with its code number
# and add a random number of random letters

coded_string = []
    # find the corresponding number for each character
for the character in the string:
By letter, code number:
if the character == letter:
# add the corresponding number
coded_string.append (str (number))
coded_string.append (& # 39; & # 39 ;. join (
aleatory sample(
ascii_lowercase,
random.randint (2,6)
)
)
) # random letters used to separate numbers
for _ in the range (random.randrange (len (string))):
coded_string.insert (
random.randrange (len (coded_string)),
& # 39; & # 39; .join (random.sample (
ascii_uppercase, random.randint (1, 3)
))
) # randomly capitalized letters inserted randomly
return & # 39; & # 39; .join (coded_string)

def decoding (string, code):

def retrieve_letter (n):
By letter, code number:
if int (n) == number:
return letter
plus:
continue
back "No match found"


decoded_list = []
    decoded_string = & # 39; & # 39;
character = & # 39; & # 39;
for the article in the chain:
yes item.isdigit ():
character + = element
plus:
if the character! = & # 39; & # 39 ;:
decoded_list.append (character)
character = & # 39; & # 39;
for n in decoded_list:
decoded_string + = retrieve_letter (n)
back decoded_string

def save_code (object):
with open ("code.p", "wb") as f:
pickle.dump (object, f)

def load_code ():
try:
with open ("code.p", "rb") as f:
back pickle.load (f)
except FileNotFoundError:
print ("No saved code found.")
return none


def main ():

import time

code = generate_code ()

print ("Welcome to my encryption program!")

at the same time as true: # code selection menu

print ("Select an option:")
print ("1: use saved code")
print ("2: use new code and overwrite saved code")
print ("3: use new code and save saved code")
prompt = entry (">")
if requested == "1":
if load_code () == None:
code = generate_code ()
plus:
code = upload code ()
break
elif prompt == "2":
save_code (code)
break
elif prompt == "3":
break
plus:
"This option is not available"
continue

while True: #Main Loop, asks the user if he wants to encode / decode

print ("Would you like to encrypt a phrase? (Y / N)")

prompt = entry (">")
if requested in ("N", "no", "No", "n"):
print ("Press Enter to exit or type something to continue:")
prompt = entry (">")
if requested == & # 39; & # 39 ;:
print ("Thanks for using the program, goodbye!")
time.sleep (2)
break
plus:
phrase = entry ("Enter your text here:  n>")
print (f " nHere is your code: {code (phrase, code)}  n")

print ("Would you like to decipher a sentence? (Y / N)")

prompt = entry (">")
if requested in ("N", "no", "No", "n"):
print ("Press Enter to exit or type something to continue:")
prompt = entry (">")
if requested == & # 39; & # 39 ;:
print ("Thanks for using the program, goodbye!")
time.sleep (2)
break
plus:
coded_phrase = input ("Enter your code here:  n>")
print (f " nHere is your original text: {decoding (coded phrase, code)}  n")
time.sleep (1)
input ("Press Enter to continue")
print (" n")

yes __name__ == & # 39; __ main __ & # 39 ;:
principal()