def aes(key, data, counter=False): """ encrypt data with aes, using either pycryptopp or PyCrypto. Args key: The encryption key data: plain text data counter: a callable, usually not needed """ # using either pycryptopp... if hasattr(AES, "process"): a = AES(key) return a.process(data) # ... or PyCrypto counter = counter or Counter() a = AES.new(key, AES.MODE_CTR, counter=counter) rest = len(data) % 16 if not rest: return a.encrypt(data) # Data length must be a multiple of 16 # Pad with bytes all of the same value as the number of padding bytes pad = (16 - rest) data += chr(pad) * pad return a.encrypt(data)[:-pad]