-1

Basically I am trying to make a small encyption program, however, I'm trying to find a way to be able to ask the user to input their sentence/sequence and then the program will replace every letter/number/symbol in the sequence with the encryption key, but I'm not sure how I would do that.

A = '@'
B = '`'
C = ':'
D = '$'
E = '%'
F = '^'
G = '&'
H = '*'
I = '('
J = ')'
K = '-'
L = '_'
M = '+'
N = '='
O = '!'
P = '~'
Q = '9'
R = '5'
S = '2'
T = '6'
U = '1'
V = '8'
W = '7'
X = '4'
Y = '?'
Z = '/'

a = '@!'
b = '#$'
c = '%^'
d = '&(d)'
e = '!@'
f = '~~'
g = '*('
h = '+)'
i = '<3'
j = '[]'
k = '{}}'
l = '||'
m = '*£'
n = ';n;'
o = '/['
p = '^('
q = '{£'
r = '>_<'
s = '\_/'
t = '-:-'
u = '^%^'
v = '/\'
w = '\|'
x = '&('
y = ':<'
z = ',.'

def encryption():
    sequence = str(input("Please input the sequence you want encrypted. "))

Also don't worry the program is nothing serious, hence why I can share the key .

Any help and explanations would be greatly appreciated, I'm still learning.

Calax
  • 37
  • 4
  • Does this answer your question? [How can I make multiple replacements in a string using a dictionary?](https://stackoverflow.com/questions/2400504/how-can-i-make-multiple-replacements-in-a-string-using-a-dictionary) – Marco F. Aug 25 '23 at 07:24
  • Worth to note that all replacements including `\ ` require it to be escaped, which means replacing it with `\\ ` or prepending `''` with r (e.g. `r'\_/'`) – matszwecja Aug 25 '23 at 07:39

3 Answers3

0

There are better ways to encrypt strings but if you wanna follow this way with this key, you can do is create a dictionary that will map to the value of a letter, in your case, a:@, then once you have input string, iterate over the string and replace the characters with characters in your dictionary.

replacements = {'a': '@'}

input_string = 'a'  # Your input string
translation_table = str.maketrans(replacements)

output_string = input_string.translate(translation_table)
print(output_string)
0

Why not create a dictionary with your characters as the key and your encrypted value as the value. You can then interate over the users string and join the results together.

Simple and Pythonic.

    enc_dict = {
    'A': '@',
    'B': '`',
    'C': ':',
    'D': '$',
    'E': '%',
    'F': '^',
    'G': '&',
    'H': '*',
    'I': '(',
    'J': ')',
    'K': '-',
    'L': '_',
    'M': '+',
    'N': '=',
    'O': '!',
    'P': '~',
    'Q': '9',
    'R': '5',
    'S': '2',
    'T': '6',
    'U': '1',
    'V': '8',
    'W': '7',
    'X': '4',
    'Y': '?',
    'Z': '/',
    ':' : '@!',
    'b': '#$',
    'c': '%^',
    'd': '&(d)',
    'e': '!@',
    'f': '~~',
    'g': '*(',
    'h': '+)',
    'i': '<3',
    'j': '[]',
    'k': '{}}',
    'l': '||',
    'm': '*£',
    'n': ';n;',
    'o': '/[',
    'p': '^(',
    'q': '{£',
    'r': '>_<',
    's': '\_/',
    't': '-:-',
    'u': '^%^',
    'v': '/\\',
    'w': '\|',
    'x': '&(',
    'y': ':<',
    'z': ',.'
}

def encryption(input_string):
    return ''.join([enc_dict[character] for character in input_string])

sequence = str(input("Please input the sequence you want encrypted. "))

print(encryption(sequence))

You will also need to change the value of your v value or it will cause a string literatal error. Just escape it using another backslash like 'v': '/\\'.

Johnny John Boy
  • 3,009
  • 5
  • 26
  • 50
0

The translate() method in Python is used to replace characters in a string based on a translation table. This method is particularly useful when you want to perform multiple character replacements efficiently, as it can be faster than using str.replace() for each individual replacement.

The translate() method takes one argument, which is a translation table. This translation table is usually created using the str.maketrans() method. The translation table maps each character you want to replace to its corresponding replacement character or to None if you want to remove the character.

mapping = {
    'A': '@', 'B': '`', 'C': ':', 'D': '$', 'E': '%', 'F': '^', 'G': '&', 'H': '*',
    'I': '(', 'J': ')', 'K': '-', 'L': '_', 'M': '+', 'N': '=', 'O': '!', 'P': '~',
    'Q': '9', 'R': '5', 'S': '2', 'T': '6', 'U': '1', 'V': '8', 'W': '7', 'X': '4',
    'Y': '?', 'Z': '/',
    'a': '@!', 'b': '#$', 'c': '%^', 'd': '&(d)', 'e': '!@', 'f': '~~', 'g': '*(',
    'h': '+)', 'i': '<3', 'j': '[]', 'k': '{}}', 'l': '||', 'm': '*£', 'n': ';n;',
    'o': '/[', 'p': '^(', 'q': '{£', 'r': '>_<', 's': '\\_/', 't': '-:-', 'u': '^%^',
    'v': '/\\', 'w': '|\\', 'x': '&(', 'y': ':<', 'z': ',.'
}


def encryption():
    sequence = str(input("Please input the sequence you want encrypted. "))
    return sequence.translate(str.maketrans(mapping))
Navid
  • 69
  • 7