Created
August 12, 2014 20:32
-
-
Save WilliamQLiu/3257f0ae30e6817a3aa5 to your computer and use it in GitHub Desktop.
Quick Obfuscation in 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
from Crypto.Cipher import AES | |
from base64 import b64encode, b64decode | |
import os | |
BLOCK_SIZE = 32 # Block size for the cipher object must be 16, 24, 32 for AES | |
PADDING = '*' | |
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING | |
EncodeAES = lambda c, s: b64encode(c.encrypt(pad(s))) | |
DecodeAES = lambda c, e: c.decrypt(b64decode(e)).rstrip(PADDING) | |
secret = os.urandom(BLOCK_SIZE) | |
cipher = AES.new(secret) | |
# Encode a string | |
encoded = EncodeAES(cipher, "This is the text I'm encoding") | |
print "Encrypted string:", encoded | |
# Decode the encoded string | |
decoded = DecodeAES(cipher, encoded) | |
print "Decrypted string:", decoded |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: Not my code, just saved since it seemed useful