Encrypt the plaintext with AES method. Parameters: plaintext -- String to be encrypted. key -- Key for encryption.
(plaintext, key)
| 23 | |
| 24 | |
| 25 | def encrypt(plaintext, key): |
| 26 | """ |
| 27 | Encrypt the plaintext with AES method. |
| 28 | |
| 29 | Parameters: |
| 30 | plaintext -- String to be encrypted. |
| 31 | key -- Key for encryption. |
| 32 | """ |
| 33 | |
| 34 | iv = os.urandom(iv_size) |
| 35 | cipher = Cipher(AES(pad(key)), CFB8(iv), default_backend()) |
| 36 | encryptor = cipher.encryptor() |
| 37 | |
| 38 | # If user has entered non ascii password (Python2) |
| 39 | # we have to encode it first |
| 40 | if isinstance(plaintext, str): |
| 41 | plaintext = plaintext.encode() |
| 42 | |
| 43 | return base64.b64encode(iv + encryptor.update(plaintext) + |
| 44 | encryptor.finalize()) |
| 45 | |
| 46 | |
| 47 | def decrypt(ciphertext, key): |
no test coverage detected