Last active
August 29, 2015 14:23
-
-
Save whitekid/9714b0a68d29263197e6 to your computer and use it in GitHub Desktop.
Simple AES encrypt/decrypt
This file contains hidden or 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
""" | |
Usage: | |
cat '1234567890123456' > secret.key | |
echo "hello world" | python encdec.py | python encdec.py -d | |
""" | |
import sys | |
import base64 | |
import getopt | |
from Crypto.Cipher import AES | |
_keyfile = 'secret.key' | |
_secret_key = None | |
def secret_key(): | |
global _secret_key | |
if _secret_key is None: | |
_secret_key = open(_keyfile).read().strip() | |
return _secret_key | |
def enc(msg): | |
msg = msg.rjust(32) | |
cipher = AES.new(secret_key(), AES.MODE_ECB) | |
return base64.b64encode(cipher.encrypt(msg)) | |
def dec(msg): | |
cipher = AES.new(secret_key(), AES.MODE_ECB) | |
return cipher.decrypt(base64.b64decode(msg)).strip() | |
def main(): | |
func = enc | |
opts, args = getopt.getopt(sys.argv[1:], 'ds:') | |
for o, a in opts: | |
if o == '-d': func = dec | |
elif o == '-s': _keyfile = a | |
sys.stdout.write(func(sys.stdin.read())) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment