Skip to content

Instantly share code, notes, and snippets.

@WilliamQLiu
Created August 12, 2014 20:32
Show Gist options
  • Save WilliamQLiu/3257f0ae30e6817a3aa5 to your computer and use it in GitHub Desktop.
Save WilliamQLiu/3257f0ae30e6817a3aa5 to your computer and use it in GitHub Desktop.
Quick Obfuscation in Python
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
@WilliamQLiu
Copy link
Author

Note: Not my code, just saved since it seemed useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment