From 5dfc803487e9defebc0dd8716f346b419460fd43 Mon Sep 17 00:00:00 2001 From: Pavan Sriram Kannuri <86733057+SportingDot1024@users.noreply.github.com> Date: Mon, 9 Oct 2023 23:17:45 +0530 Subject: [PATCH 1/6] Added/Blockchain RSA Algorithm --- blockchain/rivest_shamir_adleman.py | 123 ++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 blockchain/rivest_shamir_adleman.py diff --git a/blockchain/rivest_shamir_adleman.py b/blockchain/rivest_shamir_adleman.py new file mode 100644 index 000000000000..c4774cacaa40 --- /dev/null +++ b/blockchain/rivest_shamir_adleman.py @@ -0,0 +1,123 @@ +""" +RSA Encryption and Decryption using the `cryptography` library. + +This script demonstrates RSA encryption and decryption using the +`cryptography` library. The `cryptography` library is a package +which provides cryptographic recipes and primitives to Python +developers. It aims to support all currently supported versions +of Python. + +The RSA algorithm is an asymmetric encryption algorithm, meaning +that it uses two keys: one public and one private. The public key +can be shared with everyone, while the private key must be kept +secret. In RSA, this asymmetry is based on the practical difficulty +of the factorization of the product of two large prime numbers, the +"factoring problem". The public key consists of the modulus n and +the public (or encryption) exponent e. The private key consists of +the modulus n and the private (or decryption) exponent d, which must +be kept secret. RSA is often used to encrypt session keys, which are +symmetric keys used to encrypt data in a symmetric encryption scheme. +This avoids the need to distribute a symmetric key to each party +wanting to encrypt data. + +References: +- RSA Algorithm: https://en.wikipedia.org/wiki/RSA_(cryptosystem) +- cryptography library: https://cryptography.io/ + +""" + +from doctest import testmod + +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives import hashes +from cryptography.hazmat.primitives.asymmetric import padding, rsa + + +def generate_key_pair(): + """ + Generate RSA key pair. + + Returns: + private_key: Private key for decryption. + public_key: Public key for encryption. + + >>> private_key, public_key = generate_key_pair() + >>> isinstance(private_key, rsa.RSAPrivateKey) + True + >>> isinstance(public_key, rsa.RSAPublicKey) + True + """ + + private_key = rsa.generate_private_key( + public_exponent=65537, key_size=2048, backend=default_backend() + ) + + public_key = private_key.public_key() + + return private_key, public_key + + +def encrypt(message, public_key): + """ + Encrypt a message using RSA. + + Args: + message (str): Message to be encrypted. + public_key: Public key for encryption. + + Returns: + bytes: Encrypted ciphertext. + + >>> private_key, public_key = generate_key_pair() + >>> message = "Hello, this is a message to be encrypted!" + >>> ciphertext = encrypt(message, public_key) + >>> decrypted_message = decrypt(ciphertext, private_key) + >>> decrypted_message == message + True + """ + + ciphertext = public_key.encrypt( + message.encode(), + padding.OAEP( + mgf=padding.MGF1(algorithm=hashes.SHA256()), + algorithm=hashes.SHA256(), + label=None, + ), + ) + return ciphertext + + +def decrypt(ciphertext, private_key): + """ + Decrypt a ciphertext using RSA. + + Args: + ciphertext (bytes): Encrypted ciphertext. + private_key: Private key for decryption. + + Returns: + str: Decrypted plaintext. + + >>> private_key, public_key = generate_key_pair() + >>> message = "Hello, this is a message to be encrypted!" + >>> ciphertext = encrypt(message, public_key) + >>> decrypted_message = decrypt(ciphertext, private_key) + >>> decrypted_message == message + True + """ + + plaintext = private_key.decrypt( + ciphertext, + padding.OAEP( + mgf=padding.MGF1(algorithm=hashes.SHA256()), + algorithm=hashes.SHA256(), + label=None, + ), + ) + return plaintext.decode() + + +if __name__ == "__main__": + testmod(name="generate_key_pair", verbose=True) + testmod(name="encrypt", verbose=True) + testmod(name="decrypt", verbose=True) From d4b57f3b5621042e50c96c73d554faa7ba612808 Mon Sep 17 00:00:00 2001 From: Pavan Sriram Kannuri <86733057+SportingDot1024@users.noreply.github.com> Date: Tue, 10 Oct 2023 00:05:37 +0530 Subject: [PATCH 2/6] Update rivest_shamir_adleman.py --- blockchain/rivest_shamir_adleman.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/blockchain/rivest_shamir_adleman.py b/blockchain/rivest_shamir_adleman.py index c4774cacaa40..77c79f8a5ce1 100644 --- a/blockchain/rivest_shamir_adleman.py +++ b/blockchain/rivest_shamir_adleman.py @@ -32,8 +32,9 @@ from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.asymmetric import padding, rsa +from typing import Tuple, Union -def generate_key_pair(): +def generate_key_pair() -> Tuple[rsa.RSAPrivateKey, rsa.RSAPublicKey]: """ Generate RSA key pair. @@ -57,19 +58,19 @@ def generate_key_pair(): return private_key, public_key -def encrypt(message, public_key): +def encrypt(message: str, public_key: rsa.RSAPublicKey) -> bytes: """ Encrypt a message using RSA. Args: message (str): Message to be encrypted. - public_key: Public key for encryption. + public_key (rsa.RSAPublicKey): Public key for encryption. Returns: bytes: Encrypted ciphertext. >>> private_key, public_key = generate_key_pair() - >>> message = "Hello, this is a message to be encrypted!" + >>> message = "Hello, this is a message to be encrypted!" >>> ciphertext = encrypt(message, public_key) >>> decrypted_message = decrypt(ciphertext, private_key) >>> decrypted_message == message @@ -87,13 +88,13 @@ def encrypt(message, public_key): return ciphertext -def decrypt(ciphertext, private_key): +def decrypt(ciphertext: bytes, private_key: rsa.RSAPrivateKey) -> str: """ Decrypt a ciphertext using RSA. Args: ciphertext (bytes): Encrypted ciphertext. - private_key: Private key for decryption. + private_key (rsa.RSAPrivateKey): Private key for decryption. Returns: str: Decrypted plaintext. From d29ad7f69e4c46bc580e12ee69a31c3b87e95fcb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Oct 2023 18:36:11 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- blockchain/rivest_shamir_adleman.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/blockchain/rivest_shamir_adleman.py b/blockchain/rivest_shamir_adleman.py index 77c79f8a5ce1..fc77f528c2cb 100644 --- a/blockchain/rivest_shamir_adleman.py +++ b/blockchain/rivest_shamir_adleman.py @@ -34,6 +34,7 @@ from typing import Tuple, Union + def generate_key_pair() -> Tuple[rsa.RSAPrivateKey, rsa.RSAPublicKey]: """ Generate RSA key pair. @@ -70,7 +71,7 @@ def encrypt(message: str, public_key: rsa.RSAPublicKey) -> bytes: bytes: Encrypted ciphertext. >>> private_key, public_key = generate_key_pair() - >>> message = "Hello, this is a message to be encrypted!" + >>> message = "Hello, this is a message to be encrypted!" >>> ciphertext = encrypt(message, public_key) >>> decrypted_message = decrypt(ciphertext, private_key) >>> decrypted_message == message From 49cba92eddef88f1262c6e0875a18c1b064cc4a7 Mon Sep 17 00:00:00 2001 From: Pavan Sriram Kannuri <86733057+SportingDot1024@users.noreply.github.com> Date: Tue, 10 Oct 2023 00:18:51 +0530 Subject: [PATCH 4/6] Update rivest_shamir_adleman.py --- blockchain/rivest_shamir_adleman.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/blockchain/rivest_shamir_adleman.py b/blockchain/rivest_shamir_adleman.py index fc77f528c2cb..e69b18fa1e1a 100644 --- a/blockchain/rivest_shamir_adleman.py +++ b/blockchain/rivest_shamir_adleman.py @@ -32,10 +32,8 @@ from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.asymmetric import padding, rsa -from typing import Tuple, Union - -def generate_key_pair() -> Tuple[rsa.RSAPrivateKey, rsa.RSAPublicKey]: +def generate_key_pair() -> tuple[rsa.RSAPrivateKey, rsa.RSAPublicKey]: """ Generate RSA key pair. From ca007895c45a19b4256f7b790bf8cfc7fa1240e8 Mon Sep 17 00:00:00 2001 From: Pavan Sriram Kannuri <86733057+SportingDot1024@users.noreply.github.com> Date: Tue, 10 Oct 2023 00:33:30 +0530 Subject: [PATCH 5/6] Update requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 25dba6f5a250..d13bd24c1092 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,3 +22,4 @@ texttable tweepy xgboost yulewalker +cryptography From 39e34de30643f729157d9b1f93974c904824560a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Oct 2023 19:04:03 +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 d13bd24c1092..7dea95f120fa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ beautifulsoup4 +cryptography fake_useragent imageio keras @@ -22,4 +23,3 @@ texttable tweepy xgboost yulewalker -cryptography