15
15
'''
16
16
17
17
18
+
18
19
def aes_encrypt (plaintext : str , key : str ) -> str :
19
20
"""
20
21
AES-128 Encryption using CBC mode and PKCS7 padding.
@@ -30,9 +31,10 @@ def aes_encrypt(plaintext: str, key: str) -> str:
30
31
>>> aes_decrypt(enc, key) == msg
31
32
True
32
33
"""
33
- cipher = AES .new (key .encode ('utf-8' ), AES .MODE_CBC )
34
- ciphertext = cipher .encrypt (pad (plaintext .encode ('utf-8' ), AES .block_size ))
35
- return base64 .b64encode (cipher .iv + ciphertext ).decode ('utf-8' )
34
+ cipher = AES .new (key .encode ("utf-8" ), AES .MODE_CBC )
35
+ ciphertext = cipher .encrypt (pad (plaintext .encode ("utf-8" ), AES .block_size ))
36
+ return base64 .b64encode (cipher .iv + ciphertext ).decode ("utf-8" )
37
+
36
38
37
39
def aes_decrypt (ciphertext : str , key : str ) -> str :
38
40
"""
@@ -50,8 +52,9 @@ def aes_decrypt(ciphertext: str, key: str) -> str:
50
52
True
51
53
"""
52
54
raw = base64 .b64decode (ciphertext )
53
- cipher = AES .new (key .encode ('utf-8' ), AES .MODE_CBC , iv = raw [:AES .block_size ])
54
- return unpad (cipher .decrypt (raw [AES .block_size :]), AES .block_size ).decode ('utf-8' )
55
+ cipher = AES .new (key .encode ("utf-8" ), AES .MODE_CBC , iv = raw [: AES .block_size ])
56
+ return unpad (cipher .decrypt (raw [AES .block_size :]), AES .block_size ).decode ("utf-8" )
57
+
55
58
56
59
def main () -> None :
57
60
key = input ("Enter 16-character key (AES-128): " )
@@ -68,7 +71,9 @@ def main() -> None:
68
71
decrypted_message = aes_decrypt (encrypted_message , key )
69
72
print ("Decrypted message:" , decrypted_message )
70
73
74
+
71
75
if __name__ == "__main__" :
72
76
import doctest
77
+
73
78
doctest .testmod ()
74
79
main ()
0 commit comments