I built a pretty basic program ,,, that will take input in English ,, and encrypt it using random alphabets of different languages ;; And also decrypt it :-
def encrypt_decrypt():
inut = input("Text to convert ::-- ")
# feel free to replace the symbols ,, with ur own carecters or numbers or something
# u can also add numbers , and other carecters for encryption or decryption
decideing_variable = input("U wanna encrypt or decrypt ?? ,, write EN or DE ::- ")
if decideing_variable == "EN":
deep = inut.replace("a", "ᛟ").replace("b", "ᛃ").replace("c", "Ῡ").replace("d", "ϰ").replace("e", "Г").replace("f", "ξ").replace("g", "ᾫ").replace("h", "ῆ").replace("i", "₪").replace("j", "א").replace("k", "ⴽ").replace("l", "ⵞ").replace("m", "ⵥ").replace("n", "ঙ").replace("o", "Œ").replace("p", "უ").replace("q", "ক").replace("r", "ჶ").replace("s", "Ø").replace("t", "ю").replace("u", "ʧ").replace("v", "ʢ").replace("w", "ұ").replace("x", "Џ").replace("y", "န").replace("z", "໒")
print(f"\n{deep}\n")
elif decideing_variable == "DE":
un_deep = inut.replace("ᛟ", "a").replace("ᛃ", "b").replace("Ῡ", "c").replace("ϰ", "d").replace("Г", "e").replace("ξ","f").replace("ᾫ", "g").replace("ῆ", "h").replace("₪", "i").replace("א", "j").replace("ⴽ", "k").replace("ⵞ", "l").replace("ⵥ", "m").replace("ঙ", "n").replace("Œ", "o").replace("უ", "p").replace("ক", "q").replace("ჶ", "r").replace("Ø", "s").replace("ю", "t").replace("ʧ", "u").replace("ʢ", "v").replace("ұ", "w").replace("Џ", "x").replace("န", "y").replace("໒", "z")
print(f"\n{un_deep}\n")
encrypt_decrypt()
while writing this I didn't know any better way then chaining .replace() function ,,,
But I have a feeling , that this isn't the proper way to do it ,,
The code works fine . But ,, does any one know a better way of doing this ?
CodePudding user response:
It looks like you are doing a character by character replacement. The function you are looking for is string.maketrans
. You can give strings of equal length to convert each character to the desired character. Here is a working example online:
# first string
firstString = "abc"
secondString = "def"
string = "abc"
print(string.maketrans(firstString, secondString))
# example dictionary
firstString = "abc"
secondString = "defghi"
string = "abc"
print(string.maketrans(firstString, secondString))
You can also look at the official documentation for further details.
CodePudding user response:
You can make a dictionary for corresponding words and use this,
text = "ababdba"
translation = {'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': '໒'}
def translate(text,translation):
result = []
for char in text:
result.append( translation[char] )
return "".join(result)
print(translate(text,translation))
result is
ᛟᛃᛟᛃϰᛃᛟ
This might help you.
CodePudding user response:
You can use regex
and dict
to replace them.
import re
def encrypt_decrypt(inp, written):
e_d = {"a":"ᛟ", "b":"ᛃ", "c":"Ῡ", "d":"ϰ", "e":"Г", "f":"ξ","x":"Џ", "y":"န", "z":"໒"}
d_e = {v:k for k,v in e_d.items()}
if written == 'EN':
pattern = re.compile("|".join(e_d.keys()))
b = e_d
if written == 'DE':
pattern = re.compile("|".join(d_e.keys()))
b = d_e
res = pattern.sub(lambda x: b[x.group(0)], inp)
return res
>>> encrypt_decrypt('ᛟῩᛃ໒နῩᛃϰϰξЏ', 'DE')
'acbzycbddfx'
>>> encrypt_decrypt('acbzycbddfx', 'EN')
'ᛟῩᛃ໒နῩᛃϰϰξЏ'
CodePudding user response:
str.translate()
and str.maketrans()
are built to do all of the replacements in one go.
e.g.
>>> encrypt_table = str.maketrans("abc", "ᛟᛃῩ")
>>> "an abacus".translate(encrypt_table)
'ᛟn ᛟᛃᛟῩus'
NB. not string.maketrans()
which is how it used to be in Python 2, and is now outdated; Python 3 turned that into two systems, str.maketrans() for text and bytes.maketrans() for bytes. see How come string.maketrans does not work in Python 3.1?