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.