0

Hello guys I am a beginner at python and was trying to make an Encryption code which uses an encryption key defined by me for example I made this code but it isn't working:

def encrypt():
    A = "f"
    B = "d"
    C = "z"
    T = "x"
    first = input()
    print(first)

I want the program to function like when a user enters CAT in the input the output should be zax but when I input even A in my current program it prints A as it is.

Any help would be appreciated!

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Taaha Rauf
  • 21
  • 8

3 Answers3

1

First you should make a dictionary to map your letter substitution , eg :

my_dict = { 'A':'f', 'B':'d', 'C':'z', 'T':'x', ... }  

Then you need an encrypt function to return the values of this dictionary :

def encrypt(data):
    return ''.join( my_dict[d] if d in my_dict else d for d in data )

And a decrypt function that does the opposite :

def decrypt(data):
    my_dict_rev = dict((v,k) for k,v in my_dict.items())
    return ''.join( my_dict_rev[d] if d in my_dict_rev else d for d in data )

Now lets test it :

my_data = 'TEST DATA'
enc_data = encrypt(my_data)
dec_data = decrypt(enc_data)

Output :

print(my_data)
print(enc_data)
print(dec_data)

TEST DATA
xESx Dfxf
TEST DATA
t.m.adam
  • 15,106
  • 3
  • 32
  • 52
1

You can just use string.translate method. A sample code would be:

def encrypt():
    encryption_table = {
        ord('A'):ord('f'),
        ord('B'):ord('d'),
        ord('C'):ord('z'),
        ord('T'):ord('x'),                        
    }
    first = input()
    print(first.translate(encryption_table))

For this code an input CAT will return zfx.

DSLima90
  • 2,680
  • 1
  • 15
  • 23
-3

Here is a simple encryption and decryption script if you want to use it. The password part can be left out or you can leave it in. The same goes with the loading bar. the loading bar is on lines 1-4 and the password is on the last 9 lines. If you do take out the password make sure to call start. you will also need to install tqdm through pip.

import time
from tqdm import *
for i in tqdm(range(1000)):
    time.sleep(0.001)
def encrypt(some_string):
    alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    cipher =   'bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA'
    encryption = ''
    for char in some_string:
        if(alphabet.find(char) == -1):
            encryption = encryption + char
        else:
            position = alphabet.index(char)
            encryption = encryption + cipher[position]
    return encryption
def decrypt(some_string):
    cipher =   'bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZa'
    alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    decryption = ''
    for char in some_string:
        if(cipher.find(char) == -1):
            decryption = decryption + char
        else:
            position = cipher.index(char)
            decryption = decryption + alphabet[position]
    return decryption   
def restart():
    print('Would you like to restart.')
    print('1: Yes.')
    print('2: No.')
    c = float(input('Type a 1 or a 2: '))
    if c == 1:
        print('Ok you can restart.')
        start()
    if c == 2:
        print('Sorry to see you leave.')
        quit()
    else:
        print('Invalid response.')
        restart()
def start():
    print('1: Encryption')
    print('2: Decryption')
    e = float(input('Type a 1 or a 2: '))
    if e == 1:
        print('Type your message here.')
        e = str(input('Enter message to encrypt: '))
        print(encrypt(e))
        restart()
    if e == 2:
        print('Type your messed up message here.')
        e = str(input('Enter your messed up message: '))
        print(decrypt(e))
        restart()
    else:
        print('Please type a 1 or a 2.')
        start()
def password():
    a = float(input('Enter the correct password please: '))
    if a == 5194703:
        print('Correct!')
        start()
    else:
        print('Invalid Password, try again!')
        password()
password()
Riley
  • 1