0

I am trying to make a python program that will take in plain text and encrypt it using a key. It is written with python 2.7.4

This is my code so far

def encrypter(intext, shift, modder):
    plain = list(intext)
    out = ''
    j = 0
    key = list(shift)
    for i in plain:
        if mod > 0:
            x = chr((ord(i) + ord(key[(j % (len(plain) - 1)) % len(key)]) - 48) % 58 + 48)
        if mod < 0:
            x = chr(((ord(i) - ord(key[(j % (len(plain) - 1)) % len(key)]) - 48) % 58 + 48))
        out += x
        j += 1
    return out
sel = raw_input("Encrypt (e)/ Decrypt (d)")
if sel == 'e':
    mod = 1
elif sel == 'd':
    mod = -1
else:
    mod = 0
    print('Enter a proper value!')
if mod != 0:
    print(encrypter(raw_input('Enter the text:'), raw_input('Enter the key-phrase:'), mod)

When I encrypt something I get this:

C:\Users\aesha_000\py\Scripts\python.exe "D:/Programming/Computing GCSE/Resources/functiontest3.py"
Encrypt (e)/ Decrypt (d)e
Enter the text:alex
Enter the key-phrase:pass
]Yd:

Process finished with exit code 0

But the problem is when I then Decrypt it again I get the wrong output:

C:\Users\aesha_000\py\Scripts\python.exe "D:/Programming/Computing GCSE/Resources/functiontest3.py"
Encrypt (e)/ Decrypt (d)d
Enter the text:]Yd:
Enter the key-phrase:pass
a2e>

Process finished with exit code 0

Does anyone know the answer to this or am I just being stupid?

2
  • Just put +48 in mod<0 section...just guessing..without Implementing considering symmetry Commented May 5, 2015 at 12:25
  • possible duplicate of Python File Encryption Commented May 7, 2015 at 17:50

1 Answer 1

0

You should remove % 58 + 48. I'm assuming you're using it for having printable ascii chars, but you should not. You're loosing information.

If you want to display "not printable ascii chard", use base64.

Sign up to request clarification or add additional context in comments.

4 Comments

If I use that I get C:\Users\aesha_000\py\Scripts\python.exe "D:/Programming/Computing GCSE/Resources/functiontest3.py" Encrypt (e)/ Decrypt (d)e Enter the text:alex Enter the key-phrase:test ���� Process finished with exit code 0
Use base64 on encrypted text, and debase64 if you need to decypher
That's a classical cipher. It doesn't really improve the security of it when using the complete byte space, but it helps finding bugs, when using only printable chars.
I am not allowed to use any other cipher, as this is coursework for an exam!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.