This is my first Python project I have written. It is a simple Encrypter/Decrypter.
It has 4 commands:
- encrypt (tekst) - encrypts text and gives you a decryption key.
- key (key) - set the decryption key for decryption.
- decrypt (tekst) - decrypts tekst.
- exit - end program.
I am looking for some feedback on this. Because its my first time I was wondering if you guys could give me some tips on what I did wrong/how I could make the code better. It works like intended at the moment but I don't now if there are any bugs.
Hope you guys can give me some tips on how to make this code better!
import random
print(""" _____ _
| __ \ | |
| | | | ___ _ __ ___ _ __ _ _ _ __ | |_ ___ _ __
| | | |/ _ \ '_ \ / __| '__| | | | '_ \| __/ _ \ '__|
| |__| | __/ | | | (__| | | |_| | |_) | || __/ |
|_____/ \___|_| |_|\___|_| \__, | .__/ \__\___|_|
__/ | |
|___/|_| """)
woord = ''
exit = False
key = 0
while not exit:
arguments = ''
woord = input('> ')
command = woord.split(' ', 1)[0]
letters = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '"',
'#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<',
'=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~', ' ']
if woord != command:
arguments = woord.split(' ', 1)[1]
if woord == 'exit':
exit = True
print('Goodbeye!')
elif command == 'encrypt':
if arguments != '':
tekst = []
tekstpos = []
verplaatsing = random.randrange(1, 10)
x = 0
y = 0
versleuteld = []
for line in arguments:
tekst.extend(line)
while x < len(tekst):
position = letters.index(tekst[x])
if position < len(letters) - verplaatsing:
position += verplaatsing
elif position > len(letters) - verplaatsing:
position -= verplaatsing
tekstpos.append(position)
x += 1
while y < len(tekstpos):
cijfer = tekstpos[y]
versleuteld.append(letters[cijfer])
y += 1
print('Encrypted tekst: ' + ''.join(versleuteld))
print('Encryption key: ' + str(verplaatsing))
else:
print('The crypt command requires an argument. \nUse like this: crypt (tekst)')
elif command == 'key':
if arguments != '':
key = int(arguments)
print('Decryption key changed to: ' + str(key))
else:
print('Decryption key: ' + str(key))
elif command == 'decrypt':
if arguments != '' and key != 0:
tekst = []
tekstpos = []
x = 0
y = 0
ontsleuteld = []
for line in arguments:
tekst.extend(line)
while x < len(tekst):
position = letters.index(tekst[x])
if position < len(letters) - key - 1:
position -= key
elif position >= len(letters) - key - 1:
position += key
tekstpos.append(position)
x+= 1
while y < len(tekstpos):
cijfer = tekstpos[y]
ontsleuteld.append(letters[cijfer])
y += 1
print('Decrypted tekst: ' + ''.join(ontsleuteld))
elif key == 0:
print('Decrpyption key not defined')
else:
print('The encrypt command requires an argument. \nUse like this: encrypt (tekst)')