Skip to content

Instantly share code, notes, and snippets.

@june9713
Last active December 19, 2019 13:30
Show Gist options
  • Save june9713/3689bd04ed9d2fe4a472ade275dcc880 to your computer and use it in GitHub Desktop.
Save june9713/3689bd04ed9d2fe4a472ade275dcc880 to your computer and use it in GitHub Desktop.
import base64
import hashlib
from Crypto import Random
from Crypto.Cipher import AES
BLOCK_SIZE = 16
pad = (lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS).encode())
unpad = lambda s: s[:-ord(s[len(s) - 1:])]
class AESCipher(object):
def __init__(self, key):
#self.key = hashlib.sha256(key.encode()).digest()
#sha256 으로 변환하면 오류 발생하므로 변환하지 않고 그냥 문자열
self.key = key
def encrypt(self, message):
message = message.encode()
raw = pad(message)
cipher = AES.new(self.key, AES.MODE_ECB)
enc = cipher.encrypt(raw)
return base64.b64encode(enc).decode('utf-8')
def decrypt(self, enc):
enc = base64.b64decode(enc)
cipher = AES.new(self.key, AES.MODE_ECB)
dec = cipher.decrypt(enc)
return unpad(dec).decode('utf-8')
aes = AESCipher("10000000000000010123247643377177")
print(aes.encrypt("abcdefg"))
# ----> 'LTsDS0EG6uckmwiSl1gOqw=='
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment