The program starts of with the password() function first, which gets the keyword or key that you want to shift your string with. Then you go into the choice() option, where you can either encrypt() or decrypt() your string.
The script converts each character of the key and string to a corresponding number, and then shifts the string based on the value of each character in the key. The values are listed in a separate python file, which I have appended below. Is It also logs all encryptions and decryptions along with their password into a log.txt file.
My question is, are there any flaws with my calculations? Given a sample key: "jabbathehut" and a sample message: "This is a secret message", could the original string be brute-forced within a week if someone knew the output and the encryption process, but not the key/password used?
try:
import pyperclip
except ImportError:
pass
finally:
import dll
from dll import alphabets, numbers, date
def encrypt(numlist, string, code):
encoded_num = []
encoded_str = []
#Converting string to encoded numbers and adding shift based on password
n = 0
for i in string:
if n > len(numlist) - 1:
n = 0
try:
encode_num = alphabets[i] + numlist[n]
except KeyError as detail:
dict_error("encrypt", numlist, code, detail)
while encode_num > 90:
encode_num = encode_num - 90
encoded_num.append(encode_num)
n += 1
#Convert encoded numbers to encoded words (in list)
for i in encoded_num:
encode_str = numbers[i]
encoded_str.append(encode_str)
#Display encoded result to the user
encoded_display = "".join(encoded_str)
print("\nYour string has been encoded and copied to the clipboard:\n{}"
.format(encoded_display))
input("")
#Attempts to copy encoded result into the clipboard
try:
pyperclip.copy(encoded_display)
except NameError:
pass
#Save information into log file
f = open("log.txt", "a")
#f.write() does not write on a new line!
f.write(date)
f.write("\nEncrypted: {}".format(encoded_display))
f.write("\nPassword: {}\n\n".format(code))
f.close()
choice(numlist, code)
def decrypt(numlist, enc_string, code):
decoded_num = []
decoded_str = []
#Converting string to decoded numbers and subtracting shift based on password
n = 0
for i in enc_string:
if n > len(numlist) - 1:
n = 0
try:
decode_num = alphabets[i] - numlist[n]
except KeyError as detail:
dict_error("decrypt", numlist, code, detail)
while decode_num < 1:
decode_num = decode_num + 90
decoded_num.append(decode_num)
n += 1
#Convert decoded numbers to decoded words (in list)
for i in decoded_num:
decode_str = numbers[i]
decoded_str.append(decode_str)
decoded_display = "".join(decoded_str)
print("\nYour string has been decoded:\n{}".format(decoded_display))
input("")
f = open("log.txt", "a")
f.write(date)
f.write("\nDecrypted: {}".format(decoded_display))
f.write("\nPassword: {}\n\n".format(code))
f.close()
choice(numlist, code)
def choice(numlist, code):
dll.clear()
print("\n\tPassword: {}".format(code))
option = input("\nWhat would you like to do?[1]Encrypt [2]Decrypt [3]Reset pass"
" [4]Color [5]Exit: ")
if option == "1":
string = input("\nEnter string here: ")
encrypt(numlist, string, code)
elif option == "2":
enc_string = input("\nEnter encrypted string here: ")
decrypt(numlist, enc_string, code)
elif option == "3":
password()
elif option == "4":
print("")
dll.choose_colour()
choice(numlist, code)
elif option == "5":
pass
else:
choice(numlist, code)
def password():
#It's confusing but code is the password, and numlist is the password in number format in a list
dll.clear()
numlist = []
code = input("\nSet the password: ")
codelist = list(code)
for letter in codelist:
try:
numlist.append(alphabets[letter])
except KeyError as detail:
dict_error("password", numlist, code, detail)
if code == "" :
print("\n\tError: Your password cannot be empty!")
input("")
password()
choice(numlist, code)
def dict_error(caller, numlist, code, detail):
if caller == "password":
print("\n\tError: the key {} in your entry cannot be used as a password".format(detail))
input("")
password()
elif caller == "encrypt" or "decrypt":
print("\n\tError: the key {} in your entry does not support encryption.".format(detail))
input("")
choice(numlist, code)
password()
dll.py contains these:
import datetime
date = datetime.datetime.now().strftime("%d %B %Y - %I:%M%p")
alphabets = {
"a" : 1, "A" : 27, " " : 53, "0" : 79,
"b" : 2, "B" : 28, "." : 54, "1" : 80,
"c" : 3, "C" : 29, "?" : 55, "2" : 81,
"d" : 4, "D" : 30, "!" : 56, "3" : 82,
"e" : 5, "E" : 31, '"' : 57, "4" : 83,
"f" : 6, "F" : 32, "'" : 58, "5" : 84,
"g" : 7, "G" : 33, ":" : 59, "6" : 85,
"h" : 8, "H" : 34, "-" : 60, "7" : 86,
"i" : 9, "I" : 35, "/" : 61, "8" : 87,
"j" : 10, "J" : 36, ">" : 62, "9" : 88,
"k" : 11, "K" : 37, "<" : 63, "(" : 89,
"l" : 12, "L" : 38, "=" : 64, ")" : 90,
"m" : 13, "M" : 39, "," : 65,
"n" : 14, "N" : 40, "@" : 66,
"o" : 15, "O" : 41, "#" : 67,
"p" : 16, "P" : 42, "$" : 68,
"q" : 17, "Q" : 43, "%" : 69,
"r" : 18, "R" : 44, "^" : 70,
"s" : 19, "S" : 45, "&" : 71,
"t" : 20, "T" : 46, "*" : 72,
"u" : 21, "U" : 47, "[" : 73,
"v" : 22, "V" : 48, "]" : 74,
"w" : 23, "W" : 49, "_" : 75,
"x" : 24, "X" : 50, "~" : 76,
"y" : 25, "Y" : 51, "\\" : 77,
"z" : 26, "Z" : 52, "|" : 78
}
numbers = {
1 : "a", 27 : "A", 53 : " ", 79 : "0",
2 : "b", 28 : "B", 54 : ".", 80 : "1",
3 : "c", 29 : "C", 55 : "?", 81 : "2",
4 : "d", 30 : "D", 56 : "!", 82 : "3",
5 : "e", 31 : "E", 57 : '"', 83 : "4",
6 : "f", 32 : "F", 58 : "'", 84 : "5",
7 : "g", 33 : "G", 59 : ":", 85 : "6",
8 : "h", 34 : "H", 60 : "-", 86 : "7",
9 : "i", 35 : "I", 61 : "/", 87 : "8",
10 : "j", 36 : "J", 62 : ">", 88 : "9",
11 : "k", 37 : "K", 63 : "<", 89 : "(",
12 : "l", 38 : "L", 64 : "=", 90 : ")",
13 : "m", 39 : "M", 65 : ",",
14 : "n", 40 : "N", 66 : "@",
15 : "o", 41 : "O", 67 : "#",
16 : "p", 42 : "P", 68 : "$",
17 : "q", 43 : "Q", 69 : "%",
18 : "r", 44 : "R", 70 : "^",
19 : "s", 45 : "S", 71 : "&",
20 : "t", 46 : "T", 72 : "*",
21 : "u", 47 : "U", 73 : "[",
22 : "v", 48 : "V", 74 : "]",
23 : "w", 49 : "W", 75 : "_",
24 : "x", 50 : "X", 76 : "~",
25 : "y", 51 : "Y", 77 : "\\",
26 : "z", 52 : "Z", 78 : "|"
}