PyCrypto part 1: AES

from Crypto.Cipher import AES

encryptor = AES.new(key, AES_MODE, IV)

* AES, advanced Encryption Standard, it is very fast and secure, it's the 'de facto' (in effect/in fact) standard of symmetric encription; it has a fixed block size of 16 bytes, its keys can be 128, 192, or 256 bits long.

* key, a byte string, the secret key to use in the symmetric cipher, it must be 16(AES-128),24(AES-192), or 32(AES-256) bytes long.

* MODE_constant: the chaining mode to use for encryption or decryption.

AED.MODE_s: 

MODE_CBC =2:

                             Cipher-Block Chainning. 

MODE_CFB = 3:

                        Cipher FeedBack

MODE_ECB = 1:

                           Electronic Code Book

MODE_OFB = 5:

                           Output FeedBack

MODE_OPENPGP = 7.

IV:  the initialization Vector to use for encryption or decryption, it's a byte string.

1. it's ignored for MODE_ECB and MODE_CTR.

2. it's mandatory for MODE_OPENPGP

3. for other modes, it must be block_size long, and if it not present, then a given default value of all are zeros are used.

As an example, encryption can be done as follows:

from Crypto.Cipher import AES

from Crypto import Random

>>>key = b'Sixteen byte key'

>>>iv = Random.new().read(AES.block_size)

>>>cipher = AES.new(key, AES.MODE_CFB, iv)

>>>msg = iv + cipher.encrypt(b'Attack at dawn')

Doc Link: Crypto.Cipher.AES-module

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容