简体   繁体   中英

AES python decrypt and encrypt

Let's say there is this code.

def decrypt(key, iv, ct):
    cipher = AES.new(key, AES.MODE_CBC, iv)
    pt = cipher.decrypt(ct)

I cannot seem to understand what AES.new exactly is doing. It says "returns an AES cipher" but what exactly is it generating? I am confused as to why they are decrypting something before it is even encrypted like putting cipher.encrypt() before cipher.decrypt(ct). And what exactly is "ct" in cipher.decrypt(ct)?

Sorry for the ambiguity of the question. Thanks in advance.

I can't see which AES library are you using but let me put it in a general context.

AES is a cipher, means it is an engine that can convert plaintext into ciphertext or vice versa. You will need to provide a key and IV for the encryption algorithm for one block (eg 128 bits or 256 bits) but usually your plaintext is bigger than one block. Therefore you need to give a mode (eg CBC) to tell how consecutive blocks are created. See block cipher mode of operation for detail on what exactly is it.

In your code, obviously it is in the scenario that someone gave you a ciphertext and you know the key and IV. Then you want to convert it back to plaintext. So you build a cipher and use it to decrypt your ciphertext ( ct ).

If you have a plaintext and want to get the ciphertext, you will use the ct = cipher.encrypt(pt) function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM