Custom Python Encryption algorithm -


hey, have been working on while, , can remebr brother stepped me through same alogorithm.

basicly, adds ascii values of both characters key, , phrase.

i can encrypt this:

def encrypt(key, string):     encoded = ''     in range(len(string)):         key_c = ord(key[i % len(key)])         string_c = ord(string[i % len(string)])         encoded += chr((key_c + string_c) % 127)     return encoded 

but can't seem remember did far decrypting. difficult revers mod :p ideas?

that's simple, let's see how works. first of all, encrypted message obtained subtracting key.

enc = msg + key (mod 127) 

how can obtain original message? that's easy, subtract key in both side

enc - key = msg + key - key (mod 127) 

and here get:

enc - key = msg (mod 127) 

for more details, please reference modular arithmetic, think should belong 1 of group/field/ring. i'm not expert in math, deeper theoretical knowledge, can found them in number theory. here refined code:

def encrypt(key, msg):     encryped = []     i, c in enumerate(msg):         key_c = ord(key[i % len(key)])         msg_c = ord(c)         encryped.append(chr((msg_c + key_c) % 127))     return ''.join(encryped)  def decrypt(key, encryped):     msg = []     i, c in enumerate(encryped):         key_c = ord(key[i % len(key)])         enc_c = ord(c)         msg.append(chr((enc_c - key_c) % 127))     return ''.join(msg)  if __name__ == '__main__':     key = 'this_is_my_awsome_secret_key'     msg = 'hello world'     encrypted = encrypt(key, msg)     decrypted = decrypt(key, encrypted)      print 'message:', repr(msg)     print 'key:', repr(key)     print 'encrypted:', repr(encrypted)     print 'decrypted:', repr(decrypted) 

output

message: 'hello world' key: 'this_is_my_awsome_secret_key' encrypted: '\x1dnv`o\nko`fd' decrypted: 'hello world' 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -