I am creating an encoding and decoding algorythm based on the python os "ord" function. I have built the encoder which works by multiplying the output of the ord function by a randomly generated number, but the decoder is having some errors. In theory, it should just be the encoder but in reverse, but it's having an error. The reverse should be the "chr()" function.
#Encryption tool
import random
import os
import time
import sys
import tkinter as tk
root = tk.Tk()
canvas1 = tk.Canvas(root, width=400, height=300)
canvas1.pack()
entry1 = tk.Entry(root)
canvas1.create_window(200, 140, window=entry1)
button1 = tk.Button(text='Encrypt', command=encrypt)
def a():
with open("encryptiontool.py") as f:
exec(f.read())
txtlen = 0
shiftnum = random.randint(1, 11)
os.system("afplay /System/Library/Sounds/Hero.aiff")
print("Welcome to SHIFTNUM Encryption Tool! ")
encrypt_input = input("Enter a word to be encrypted: ")
lengthinput = len(list(encrypt_input))
numberlength = int(lengthinput)
if shiftnum == 11:
os.system("afplay /System/Library/Sounds/Bottle.aiff")
print("!@**#(*!%#(&^%!&^@%ERROR!(@&^^#$%*^!%$&#^%$")
sys.exit()
encryptedlist = []
print("NUMSHIFT = ", shiftnum)
while numberlength > 0:
numberlength = numberlength - 1
encryptedlist.append(ord(encrypt_input[txtlen]) * shiftnum)
txtlen = txtlen + 1
print(encryptedlist)
os.system("afplay /System/Library/Sounds/Glass.aiff")
I have tried the following code, but the error is the same.
#Decryption tool
txtlen = 0
import re
numshift = int(input("NUMSHIFT = "))
encryptedtxtlist = input("Paste list here: ")
dechartxt = re.sub(r"[^a-zA-Z0-9 ]", "", encryptedtxtlist)
print(dechartxt.split())
listlength = len(dechartxt.split())
lstlen = int(listlength)
dechar = int(dechartxt)
decodedlist = []
while lstlen > 0:
lstlen = lstlen - 1
dechar = dechar / numshift
decodedlist.append(chr(int(dechar[txtlen])))
txtlen = txtlen + 1
print(decodedlist)
but the output is the same :
NUMSHIFT = 5
Paste list here: ['460']
['460']
Traceback (most recent call last):
File "/Users/ruben_sutton29/Desktop/pythonproject/decryptiontool.py", line 19, in <module>
decodedlist.append(chr(int(dechar[txtlen])))
TypeError: 'float' object is not subscriptable
Let's see if we can help simplify this a bit. It appears that using a list
to store the ciphertext is over-complicating things for you.
The encryption task:
The decryption task:
The two documented functions below, encrypt
and decrypt
follow these simple requirements.
Yes, the functions can be collapsed into single line statements, but have been intentionally left more verbose to aid in beginner clarity.
The primary thing to note is the simplified logic. Rather than passing in a list
to the decryption function, simply return and pass in a comma separated string of offset values. The list
was the primary tripping-up point.
def encrypt(text: str, n: int) -> str:
"""Encrypt a message, as an ASCII offset.
Args:
txt (str): Text to be encrypted.
n (int): Value of which each character's ASCII value is
multiplied.
Returns:
str: A comma separated string containing offset integer values.
"""
offsets = []
for c in text:
offsets.append(ord(c)*n)
return ','.join(map(str, offsets))
def decrypt(text: str, n: int) -> str:
"""Decrypt a message of ASCII offset integers into plaintext.
Args:
txt (str): Comma separated integer string to be decrypted.
n (int): Value of which each integer is divided.
Returns:
str: A decrypted plaintext message.
"""
decrypted = []
for i in text.split(','):
decrypted.append(chr(int(i)//n))
return ''.join(decrypted)
# Prompt the user for input.
plaintext = input('Enter your message: ')
n = int(input('Enter the offset value: '))
# Print the original message, for debugging.
print(f'{plaintext=}')
# Call the encryption function, and print the returned value.
ciphertext = encrypt(plaintext, n)
print(f'{ciphertext=}')
# Call the decryption function, and print the returned value.
plaintext2 = decrypt(ciphertext, n)
print(f'{plaintext2=}')
Enter your message: A secret message.
Enter the offset value: 5
plaintext='A secret message.'
ciphertext='325,160,575,505,495,570,505,580,160,545,505,575,575,485,515,505,230'
plaintext2='A secret message.'