1

I became interested in Encryptions awhile back and I decided to play around with my own encryption script in Python. I've run into a 'few' problems. I'm sure the solution is easy, but for the life of me I'm unable to figure it out.

So: Here is my Encryption/Decryption script put into one script:

def encrypt(key1, msg):
    encrypted =[]
    for i, c in enumerate(msg):
            msg_c = ord(c)
            encrypted.append(unichr((msg_c + key1) % (key1+key2)))
    return ''.join(encrypted)

def decrypt(key2, encryped):
    decrypted = []
    for i, c in enumerate(encryped):
        enc_c = ord(c)
        decrypted.append(unichr((enc_c + key2) % (key1+key2)))
    return ''.join(decrypted)

if __name__ == '__main__':
    key1 = int(raw_input("Key 1: "))
    key2 = int(raw_input("Key 2: "))
    msg = raw_input("Message to Encrypt: ")
    encrypted = encrypt(key1, msg)
    decrypted = decrypt(key2, encrypted)

print 'Encrypted: ', repr(encrypted)
print 'Decrypted: ', repr(decrypted)

So, if I put Key 1 to 5521 and Key 2 to 223 and set my Message to Encrypt Test I receive the following as output:

Encrypted:  u'\u15e5\u15f6\u1604\u1605'
Decrypted:  u'Test'

So, that works. Which is fine and amazing. It displays what Test would be as Encrypted with Key 1 and Key 2. However, I'd like to break it into Two separate scripts. One that Encrypts and the Other that Decrypts.

So my Encrypt Script is as such:

def encrypt(key1, msg):
    encrypted =[]
    for i, c in enumerate(msg):
            msg_c = ord(c)
            encrypted.append(unichr((msg_c + key1) % (key1+key2)))
    return ''.join(encrypted)
if __name__ == '__main__':
    key1 = int(raw_input("Key 1: "))
    key2 = int(raw_input("Key 2: "))
    msg = raw_input("Message to Encrypt: ")
    encrypted = encrypt(key1, msg)

print 'Encrypted: ', repr(encrypted)

Using the same input as the first script, I get the following output:

Encrypted:  u'\u15e5\u15f6\u1604\u1605'

So, that works fine as well. It gives me the same encrypted string as before. Yet, here is my Decrypt script:

def decrypt(key2, encryped):
    decrypted = []
    for i, c in enumerate(encryped):
        enc_c = ord(c)
        decrypted.append(unichr((enc_c + key2) % (key1+key2)))
    return ''.join(decrypted)

if __name__ == '__main__':
    key1 = int(raw_input("Key 1: "))
    key2 = int(raw_input("Key 2: "))
    encrypted = raw_input("Message to Decrypt: ")
    decrypted = decrypt(key2, encrypted)

print 'Decrypted: ', repr(decrypted)

Now, if I input the same two keys, and I input the Message to Decrypt as: u'\u15e5\u15f6\u1604\u1605' I get the following Output:

Decrypted:  u'\u0154\u0106\u013b\u0154\u0110\u0114\u0144\u0114\u013b\u0154\u0110\u0114\u0145\u0115\u013b\u0154\u0110\u0115\u010f\u0113\u013b\u0154\u0110\u0115\u010f\u0114\u0106'

If I modify my input to exclude u'' and place just the \u15e5\u15f6\u1604\u1605 portion in, I get this:

Decrypted: u'\u013b\u0154\u0110\u0114\u0144\u0114\u013b\u0154\u0110\u0114\u0145\u0115\u013b\u0154\u0110\u0115\u010f\u0113\u013b\u0154\u0110\u0115\u010f\u0114'

Any ideas as to what I'm doing wrong? Seems like I should get the same output as, "u'Test'" yet for some reason I'm not.

  • 1
    http://stackoverflow.com/questions/477061/how-to-read-unicode-input-and-compare-unicode-strings-in-python ? – Eric Levieil Jul 22 '15 at 18:39
  • Are you sure you want to use unicode or encrypting to numbers will be fine too? – Eric Levieil Jul 22 '15 at 18:40
  • 2
    The encryption/decryption looks fine. It seems like raw_input() is not handling those unicode strings the way you want. – ate50eggs Jul 22 '15 at 18:53
  • It seems, from looking at it further, that it's treating the Message to Decrypt as a String and not as Unicode. I discovered this by using the following in my Decrypt script: `encrypted = ud.normalize('NFC',raw_input("Message to Decrypt: "))` Then I received this error: `TypeError: must be unicode, not str` – Markis Cook Jul 22 '15 at 19:13
  • If this is solved please show us the answer. If it is constrained to your personal code you may also delete the question. Otherwise, please indicate what you expect from answers (this is more a question for codereview though). – Maarten Bodewes Jul 24 '15 at 09:33
  • I'm voting to close this question as off-topic because it is about reviewing code from the author himself, rather than an explicit question. Try [code review](http://codereview.stackexchange.com) instead – Maarten Bodewes Jul 24 '15 at 09:34

0 Answers0