Skip to content

Instantly share code, notes, and snippets.

@nori3tsu
Last active December 24, 2015 01:49
Show Gist options
  • Save nori3tsu/6726479 to your computer and use it in GitHub Desktop.
Save nori3tsu/6726479 to your computer and use it in GitHub Desktop.
AES/ECB/PKCS5Padding
from Crypto.Cipher import AES
import base64
BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[0:-ord(s[-1])]
class AESCipher:
def __init__(self, key):
self.key = key
def encrypt(self, text):
cipher = AES.new(self.key, AES.MODE_ECB)
raw = pad(text)
enc = cipher.encrypt(raw)
return base64.b64encode(enc)
def decrypt(self, text):
cipher = AES.new(self.key, AES.MODE_ECB)
enc = base64.b64decode(text)
return unpad(cipher.decrypt(enc))
if __name__== "__main__":
key="1234567890123456"
text = "nori3tsu"
cipher = AESCipher(key)
enctext = cipher.encrypt(text)
print "%s" % enctext
dectext = cipher.decrypt(enctext)
print "%s" % dectext
@mustafaozcaninfo
Copy link

Mustafa Özcan

Kişisel Blog makalelerimi SEO konusundaki deneyimleri paylaştığım blogum.Fırsatları ayağınıza getiren mustafa ozcan blog kişisel temalı yazılar ile gündemde yer alan güncel yazıları sizlere sunma fırsatı ile.

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