Home > Enterprise >  text crypter program dosent give right results
text crypter program dosent give right results

Time:03-12

when you crypt a text it works but when you try to uncrypt in it gives weird result. My code was working but it suddenly broke. The things is I don't know if crypting part or uncrypting part is broken i would appreciate any help.


abc = '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'
code = '! @ # $ % ^ & * ( ) _   § ± , . / < > ? | ` ~ [ ] {'
code2 = '{ ] [ ~ ` | ? > < / . , ± §   _ ) ( * & ^ % $ # @ !'


while True:
    print()
    print('--------------')
    print('[1] text -> code\n[2] code -> text')
    print('--------------')
    inp = input('==> ')

# encrypting chosen
    if inp == '1':
        print('--------------')
        print('enter the text')
        print('--------------')
        inp = input('==> ')
        print('-----code-----')
        # for every character in the text given this runs
        for i in inp:
            # gets a random number between 1 and 2 because there is two diffirent alpabets
            # every letter will be cyrpted depending on a random alpabet
            rand = random.randint(1, 2)
            # gets the index for the character in abc
            ind = abc.index(i)
            if rand == 1:
                # prints the index in code2(1th alpabet) and adds '∑' so program know it belongs to 1th alpabet
                print(code2[ind], '∑', sep='', end='')
            elif rand == 2:
                # prints the index in code(@th alpabet) and adds '¥' so program know it belongs to 1th alpabet
                print(code[ind], '¥', sep='', end='')
# decrypting chosen
    elif inp == '2':
        print()
        print('--------------')
        print('enter the code')
        print('--------------')
        inp = input('==> ')
        print('-----text-----')
        # for every character in the text given this runs
        for i in inp:
            # checks the character next to the actual character if it is '∑'
            # to know which alpabet it belongs to
            if inp[inp.index(i) 1] == '∑':
                # gets the index and print the same index in abc
                ind = code2.index(i)
                print(abc[ind], end='')

            elif inp[inp.index(i) 1] == '¥':
                ind = code.index(i)
                print(abc[ind], end='')

    else:
        pass

CodePudding user response:

This does what you want. Note that I add your alphabet shift BEFORE the encoded character. That way, when iterating through the encoded text, I can just set the correct decoding alphabet.

import random

abc  ='abcdefghijklmnopqrstuvwxyz'
code ='!@#$%^&*()_ §±,./<>?|`~[]{'
code2='{][~`|?></.,±§ _)(*&^%$#@!'

while True:
    print('\n--------------')
    print('[1] text -> code\n[2] code -> text')
    print('--------------')
    inp = input('==> ')

# encrypting chosen
    if inp == '1':
        print('--------------')
        print('enter the text')
        print('--------------')
        inp = input('==> ')
        print('-----code-----')
        # for every character in the text given this runs
        for i in inp:
            # gets a random number between 1 and 2 because there is two diffirent alpabets
            # every letter will be cyrpted depending on a random alpabet
            rand = random.randint(1, 2)
            # gets the index for the character in abc
            ind = abc.index(i)
            if rand == 1:
                # prints the index in code2(1th alpabet) and adds '∑' so program know it belongs to 1th alpabet
                print('∑', code2[ind], sep='', end='')
            elif rand == 2:
                # prints the index in code(@th alpabet) and adds '¥' so program know it belongs to 1th alpabet
                print('¥', code[ind], sep='', end='')
# decrypting chosen
    elif inp == '2':
        print()
        print('--------------')
        print('enter the code')
        print('--------------')
        inp = input('==> ')
        print('-----text-----')
        # for every character in the text given this runs
        which = code
        for i in inp:
            if i == '∑':
                which = code2
            elif i == '¥':
                which = code
            else:
                ind = which.index(i)
                print(abc[ind], end='')

A better design would have an encode function and a decode function that return strings, thereby allowing the caller to decide what to DO with the strings.

  • Related