JerryPlayz101
JerryPlayz101

Reputation: 117

Encryption with own Cipher Dictionary

Here is my code:

dictionary = {'A':'3',
'B':'u',
'C':'t',
'D':'5',
'E':'b',
'F':'6',
'G':'7',
'H':'8',
'I':'/',
'J':'9',
'K':'0',
'L':'-',
'M':'o',
'N':'i',
'O':';',
'P':'}',
'Q':'c',
'R':'n',
'S':'4',
'T':'m',
'U':'.',
'V':'y', 
'W':'v',
'X':'r', 
'Y':',',
'Z':'e',
}
print(dictionary)
inp = input(str("What do you want me to encode?").upper()).upper()
li = list(inp)
print(li)
for letter in inp:
    pass

I want to ask how I could use this dictionary to encrypt any message that goes through the input. Like 'Hello my name is Jerry' would turn into: (Without Phrasing) '8b--; o, i3ob /4 9bnn,'. Could someone please help me with this. Ive seen other questions like this being asked - but they use PyCrypto. I dont want to go through the hassle of installing it. Could someone please help me.

Thanks, Jerry

Upvotes: 1

Views: 456

Answers (2)

Steven Summers
Steven Summers

Reputation: 5384

You can use the str.translate method. The benefit here is that you only have to create the table once, so you can use the same table even if you have lots of strings to encrypt.

table = str.maketrans(dictionary) # create a translate table

inp = input("What do you want me to encode? ").upper()
res = inp.translate(table)
print(res)

Upvotes: 0

James
James

Reputation: 36623

You need to pass each character of the user input through the dictionary to get the cypher value out.

# removed the first .upper() here
inp = input(str("What do you want me to encode?")).upper()
li = list(inp)
print(li)

# create a list of letters passed through the dictionary
out = [dictionary[letter] for letter in inp]

# using ''.join makes the new list a single string
print(''.join(out))

Upvotes: 2

Related Questions