2

I'm writing a python program to encrypt and decrypt a custom string. This isn't going to be used for anything serious, it's just for fun. The encrypter seems to work fine, but here it is, the encrypter. The program writes everything to a file. As seen in the code below, the program loops through ASCII characters if the key (which is defined by the user) will put it above 127.

My issue is that when decrypting, I get some strange characters. It fails when using a key over 229.

Encrypter:

temp_key = 9999
message = "Hello"
result = ""
for char in message:
    ecry_char_int = ord(char) - temp_key
    while ecry_char_int < 0:
                temp_key -= 128
                ecry_char_int = 128 - temp_key
    result += chr(ecry_char_int)
print(result)

Decrypter:

result2 = ""
encoded = result
ekey = 9999
for char in encoded:
    decr_char_int = ord(char) + ekey
    while decr_char_int > 127:
         ekey -= 128
         decr_char_int = ekey
    result2 += chr(decr_char_int)
print(result2)

For example, encrypting "Hello" with the key 9999; I get the encrypted string of "qV]]`". Decrypting string "qV]]`" with key 9999 I get this:

'\x0fello'

What I'm trying to figure out is how I'm supposed to stop this from happening, since it's only the first character this happens with.

Note: I don't want/know how to install any additional modules (i.e. Cryptography) since this is/was being mostly developed on my school computer.

Hami
  • 704
  • 2
  • 9
  • 27
  • Could you please provide us with a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Hami Jun 10 '17 at 19:07
  • @MartijnPieters, you removed the strange ASCII code 15 character from the question and I can't re-add it with an edit because an edit has to be at least 6 characters. – Hami Jun 10 '17 at 19:14
  • @Hami: nothing was removed, only added to; see the "side-by-side markdown" diff at https://stackoverflow.com/posts/44477022/revisions – Martijn Pieters Jun 10 '17 at 19:15
  • @MartijnPieters here is a screenshot of what I see. Notice the strange character being removed: http://imgur.com/a/co8wD – Hami Jun 10 '17 at 19:18
  • 1
    @Hami: analysing the first revision source I can see it was there, but because it's not printable that's not very helpful. Better for the OP to use the `repr()` output instead, I'll add that. – Martijn Pieters Jun 10 '17 at 19:21
  • @MartijnPieters thank you for fixing that! – Hami Jun 10 '17 at 19:25
  • @MartijnPieters @Hami I used the `repr()` thing in my code, and it still didn't work. I did this to the decryption part. `result2 += chr(decr_char_int)` `print(repr(result2))` – Andrew Myerson Jun 10 '17 at 19:41
  • @AndrewMyerson: that was not meant to be a solution, only an aid to show what the contents of your variable are in a readable and reproducible fashion. – Martijn Pieters Jun 10 '17 at 19:43
  • @MartijnPieters ah, I see. – Andrew Myerson Jun 10 '17 at 19:47

1 Answers1

0

That's because you're not really encrypting anything - after the first character your algorithm turns into a simple Caesar cipher, where the first character will always be 'encrypted' as the value of 128 - temp_key % 128 for all conditions where temp_key > ord(first_char).

The problem is that after the initial ecry_char_int = ord(char) - temp_key, you're not considering your current character for anything if it happens to be smaller than the key (and that's the case with ord('H') being 72 while your key is 9999) - you go into a loop where you're successively subtracting 128 from the temp_key and while setting 128 - temp_key as ecry_char_int. Eventually, temp_key will be small enough to produce a positive number when subtracted from 128 at which point you'll set that number as your 'encrypted' character. So, you essentially went a long, loopy way to set the first character to the value of 128 - temp_key % 128 (which is 113, btw. and that's why the first character from your function will always be q for pretty much all keys >256).

But that's not all - since you've now modified the value of your temp_key to temp_key % 128 of the original (so 15 for your 9999 key), now all the consequent characters will be ascii left-shifted by that amount resulting e to become V (chr(ord("e") - 15)) etc. Hence - you've got yourself a Caesar cipher.

Your decryption function does pretty much the same in the opposite direction, the first character will always be constructed the same way and the rest of the characters will be ascii right-shifted by the same position to their original places.

Do yourself a favor and use a proper encryption if you need encryption. Here's a simple example.

zwer
  • 24,943
  • 3
  • 48
  • 66