Last active
May 30, 2016 04:50
-
-
Save vincenting/6677e43c9709c8fc864fab91d27b2c01 to your computer and use it in GitHub Desktop.
A better AzDG crypt for python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hashlib | |
import random | |
import string | |
import base64 | |
def randomword(length): | |
return ''.join(random.choice(string.lowercase) for i in range(length)) | |
def encode_key(key, input): | |
md5_hash = hashlib.md5() | |
md5_hash.update(key) | |
encrypt_key = md5_hash.hexdigest() | |
result, idx = ('', 0) | |
for item in input: | |
if idx == len(encrypt_key): | |
idx = 0 | |
result += chr(ord(item) ^ ord(encrypt_key[idx])) | |
idx += 1 | |
return result | |
def encrypt(key, input): | |
rand_str = randomword(20) | |
result, idx = ('', 0) | |
for item in input: | |
if idx == len(rand_str): | |
idx = 0 | |
result += rand_str[idx] | |
result += chr(ord(item) ^ ord(rand_str[idx])) | |
idx += 1 | |
return base64.b64encode(encode_key(key, result)).replace('/', '_').replace('+', '-') | |
def decrypt(key, message): | |
message = message.replace('_', '/').replace('-', '+') | |
origin_str = base64.b64decode(message) | |
encoded_key = encode_key(key, origin_str) | |
result = '' | |
for (idx, val) in enumerate(encoded_key): | |
if idx % 2: | |
continue | |
result += chr(ord(val) ^ ord(encoded_key[idx + 1])) | |
return result | |
if __name__ == '__main__': | |
screct = 'heloworld' | |
print decrypt(screct, encrypt(screct, 'this is a message')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment