From fc76072958f92c074020a55c05f1e233386e1b05 Mon Sep 17 00:00:00 2001 From: grpathak22 Date: Thu, 3 Oct 2024 23:36:24 +0530 Subject: [PATCH 1/6] added aes-128 algorithm in ciphers Signed-off-by: grpathak22 --- ciphers/aes_128.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 ciphers/aes_128.py diff --git a/ciphers/aes_128.py b/ciphers/aes_128.py new file mode 100644 index 000000000000..e0ba4f4825ea --- /dev/null +++ b/ciphers/aes_128.py @@ -0,0 +1,61 @@ +from Crypto.Cipher import AES +from Crypto.Util.Padding import pad, unpad +import base64 + +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: + 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() From 23a842545a1472c4f4146e0c8f444e0944ff5f61 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Oct 2024 18:09:58 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ciphers/aes_128.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/ciphers/aes_128.py b/ciphers/aes_128.py index e0ba4f4825ea..614d6b1f1298 100644 --- a/ciphers/aes_128.py +++ b/ciphers/aes_128.py @@ -2,6 +2,7 @@ from Crypto.Util.Padding import pad, unpad import base64 + def aes_encrypt(plaintext: str, key: str) -> str: """ AES-128 Encryption using CBC mode and PKCS7 padding. @@ -17,9 +18,10 @@ def aes_encrypt(plaintext: str, key: str) -> str: >>> 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') + 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: """ @@ -37,8 +39,9 @@ def aes_decrypt(ciphertext: str, key: str) -> str: 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') + 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: key = input("Enter 16-character key (AES-128): ") @@ -55,7 +58,9 @@ def main() -> None: decrypted_message = aes_decrypt(encrypted_message, key) print("Decrypted message:", decrypted_message) + if __name__ == "__main__": import doctest + doctest.testmod() main() From 9fd5b47b7b0ded19ac6d289b41bf626a83bd1eec Mon Sep 17 00:00:00 2001 From: grpathak22 Date: Thu, 3 Oct 2024 23:50:01 +0530 Subject: [PATCH 3/6] made changes for ruff tests Signed-off-by: grpathak22 --- ciphers/aes_128.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/ciphers/aes_128.py b/ciphers/aes_128.py index e0ba4f4825ea..f9f38dbc6935 100644 --- a/ciphers/aes_128.py +++ b/ciphers/aes_128.py @@ -1,6 +1,19 @@ -from Crypto.Cipher import AES -from Crypto.Util.Padding import pad, unpad import base64 +from Crypto.Cipher import AES +from Crypto.Util.Padding import pad, unpad + + + +''' +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: """ From 05a31fb3aa6dd36babe8155f945420c92aebcbb2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Oct 2024 18:22:34 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ciphers/aes_128.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/ciphers/aes_128.py b/ciphers/aes_128.py index 92c52ce4fb62..495a92bd63aa 100644 --- a/ciphers/aes_128.py +++ b/ciphers/aes_128.py @@ -1,19 +1,17 @@ import base64 -from Crypto.Cipher import AES +from Crypto.Cipher import AES from Crypto.Util.Padding import pad, unpad - -''' -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 +""" +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: + For more details, visit: https://en.wikipedia.org/wiki/Advanced_Encryption_Standard -''' - +""" def aes_encrypt(plaintext: str, key: str) -> str: From 51871d44efeef91687aab4f2402296e7f169a231 Mon Sep 17 00:00:00 2001 From: grpathak22 Date: Fri, 4 Oct 2024 00:01:59 +0530 Subject: [PATCH 5/6] updated requirements.txt Signed-off-by: grpathak22 --- requirements.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/requirements.txt b/requirements.txt index afbf25ba6edc..deb4642b008e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,3 +22,5 @@ tweepy # yulewalker # uncomment once audio_filters/equal_loudness_filter.py is fixed typing_extensions xgboost +pycryptodome + From 791c928998b6bcde80e5f10d0b81e3aabc08c92d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Oct 2024 18:35:09 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index deb4642b008e..3571eac78309 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,6 +8,7 @@ numpy opencv-python pandas pillow +pycryptodome # projectq # uncomment once quantum/quantum_random.py is fixed qiskit ; python_version < '3.12' qiskit-aer ; python_version < '3.12' @@ -22,5 +23,4 @@ tweepy # yulewalker # uncomment once audio_filters/equal_loudness_filter.py is fixed typing_extensions xgboost -pycryptodome