-
-
Save btall/f528411c95cbce86f284 to your computer and use it in GitHub Desktop.
Encryption using pycrypto, AES/ECB/PKCS5Padding
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 Crypto import Random | |
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): | |
""" | |
Requires hex encoded param as a key | |
""" | |
self.key = key.decode("hex") | |
def encrypt(self, raw): | |
""" | |
Returns hex encoded encrypted value! | |
""" | |
raw = pad(raw) | |
cipher = AES.new(self.key, AES.MODE_ECB) | |
return cipher.encrypt(raw).encode("hex") | |
def decrypt(self, enc): | |
""" | |
Requires hex encoded param to decrypt | |
""" | |
enc = enc.decode("hex") | |
cipher = AES.new(self.key, AES.MODE_ECB) | |
return unpad(cipher.decrypt(enc)) | |
if __name__ == "__main__": | |
key = "140b41b22a29beb4061bda66b6747e14" | |
ciphertext = "6c94326b6b726eb7c48b049a6e2fcf008a10ea14b603e6a6a38709b77d2af03f119dc26f9522e3ae43f1a5e0c5bb8493" | |
key = key[:32] | |
decryptor = AESCipher(key) | |
plaintext = decryptor.decrypt(ciphertext) | |
print "%s" % plaintext |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
GG !