I know that generally it's a bad idea, but I've run into an instance where I need to do a duplex use of a crypto object. I have an embedded device, that I need to use that does this very thing, i.e. use the same state for two way communication.
To use it I've made a bit of a hack, of resetting the _next to allow me to reuse the object for bot decrypt and encrypt, i.e.
At init:
self.cipher = AES.new(key.encode("utf-8"), AES.MODE_CFB, self.iv.encode("utf-8"))
# Save the _next state of the cipher, for duplex hack
self.cipherCp = self.cipher._next
At Tx:
self.cipher._next = self.cipherCP
temp_buf = self.cipher.encrypt(bytes(tx_buf))
At Rx:
self.cipher._next = self.cipherCP
pkt.extend(self.cipher.decrypt(self.ser_dev.read(rx_size)))
This works, but I would rather not have to hack into the internals this way. Any way to get a flag or something to allow duplex use of an object?
BTW, Pycrypt allowed this to work....