Skip to content

Commit 23a8425

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent fc76072 commit 23a8425

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

Diff for: ciphers/aes_128.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from Crypto.Util.Padding import pad, unpad
33
import base64
44

5+
56
def aes_encrypt(plaintext: str, key: str) -> str:
67
"""
78
AES-128 Encryption using CBC mode and PKCS7 padding.
@@ -17,9 +18,10 @@ def aes_encrypt(plaintext: str, key: str) -> str:
1718
>>> aes_decrypt(enc, key) == msg
1819
True
1920
"""
20-
cipher = AES.new(key.encode('utf-8'), AES.MODE_CBC)
21-
ciphertext = cipher.encrypt(pad(plaintext.encode('utf-8'), AES.block_size))
22-
return base64.b64encode(cipher.iv + ciphertext).decode('utf-8')
21+
cipher = AES.new(key.encode("utf-8"), AES.MODE_CBC)
22+
ciphertext = cipher.encrypt(pad(plaintext.encode("utf-8"), AES.block_size))
23+
return base64.b64encode(cipher.iv + ciphertext).decode("utf-8")
24+
2325

2426
def aes_decrypt(ciphertext: str, key: str) -> str:
2527
"""
@@ -37,8 +39,9 @@ def aes_decrypt(ciphertext: str, key: str) -> str:
3739
True
3840
"""
3941
raw = base64.b64decode(ciphertext)
40-
cipher = AES.new(key.encode('utf-8'), AES.MODE_CBC, iv=raw[:AES.block_size])
41-
return unpad(cipher.decrypt(raw[AES.block_size:]), AES.block_size).decode('utf-8')
42+
cipher = AES.new(key.encode("utf-8"), AES.MODE_CBC, iv=raw[: AES.block_size])
43+
return unpad(cipher.decrypt(raw[AES.block_size :]), AES.block_size).decode("utf-8")
44+
4245

4346
def main() -> None:
4447
key = input("Enter 16-character key (AES-128): ")
@@ -55,7 +58,9 @@ def main() -> None:
5558
decrypted_message = aes_decrypt(encrypted_message, key)
5659
print("Decrypted message:", decrypted_message)
5760

61+
5862
if __name__ == "__main__":
5963
import doctest
64+
6065
doctest.testmod()
6166
main()

0 commit comments

Comments
 (0)