Skip to content

added aes-128 algorithm in ciphers #11716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions ciphers/aes_128.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad


"""

Check failure on line 6 in ciphers/aes_128.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

ciphers/aes_128.py:1:1: I001 Import block is un-sorted or un-formatted
AES (Advanced Encryption Standard) is a symmetric encryption algorithm used
for secure data encryption. AES-128 uses a 128-bit key to encrypt and decrypt
data blocks of 128 bits. This implementation uses Cipher Block Chaining (CBC) mode
with PKCS7 padding for encrypting messages.

For more details, visit:
https://en.wikipedia.org/wiki/Advanced_Encryption_Standard
"""


def aes_encrypt(plaintext: str, key: str) -> str:
"""
AES-128 Encryption using CBC mode and PKCS7 padding.

:param plaintext: The plaintext message to be encrypted.
:param key: The encryption key (16 characters = 128 bits).
:return: Encrypted message (Base64 encoded).

>>> msg = "This is a secret message."
>>> key = "thisisaverysecret"
>>> enc = aes_encrypt(msg, key)
>>> dec = aes_decrypt(enc, key)
>>> aes_decrypt(enc, key) == msg
True
"""
cipher = AES.new(key.encode("utf-8"), AES.MODE_CBC)
ciphertext = cipher.encrypt(pad(plaintext.encode("utf-8"), AES.block_size))
return base64.b64encode(cipher.iv + ciphertext).decode("utf-8")


def aes_decrypt(ciphertext: str, key: str) -> str:
"""
AES-128 Decryption using CBC mode and PKCS7 padding.

:param ciphertext: The Base64 encoded encrypted message.
:param key: The decryption key (16 characters = 128 bits).
:return: Decrypted plaintext message.

>>> msg = "This is a secret message."
>>> key = "thisisaverysecret"
>>> enc = aes_encrypt(msg, key)
>>> dec = aes_decrypt(enc, key)
>>> dec == msg
True
"""
raw = base64.b64decode(ciphertext)
cipher = AES.new(key.encode("utf-8"), AES.MODE_CBC, iv=raw[: AES.block_size])
return unpad(cipher.decrypt(raw[AES.block_size :]), AES.block_size).decode("utf-8")


def main() -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file ciphers/aes_128.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file ciphers/aes_128.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file ciphers/aes_128.py, please provide doctest for the function main

key = input("Enter 16-character key (AES-128): ")
if len(key) != 16:
raise ValueError("Key must be 16 characters long!")

message = input("Enter message: ")

# Encryption
encrypted_message = aes_encrypt(message, key)
print("Encrypted message:", encrypted_message)

# Decryption
decrypted_message = aes_decrypt(encrypted_message, key)
print("Decrypted message:", decrypted_message)


if __name__ == "__main__":
import doctest

doctest.testmod()
main()
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ tweepy
# yulewalker # uncomment once audio_filters/equal_loudness_filter.py is fixed
typing_extensions
xgboost
pycryptodome

Loading