Skip to content

Added new algo for RSA #11868

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 1 commit into from
Closed
Changes from all 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
120 changes: 120 additions & 0 deletions other/RSA_Algo/rsa_algo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import random

Check failure on line 1 in other/RSA_Algo/rsa_algo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

other/RSA_Algo/rsa_algo.py:1:1: INP001 File `other/RSA_Algo/rsa_algo.py` is part of an implicit namespace package. Add an `__init__.py`.
from sympy import isprime, mod_inverse
import sys

def generate_prime_candidate(length):

Check failure on line 5 in other/RSA_Algo/rsa_algo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

other/RSA_Algo/rsa_algo.py:1:1: I001 Import block is un-sorted or un-formatted
"""
Generate a large prime number candidate.

Check failure on line 8 in other/RSA_Algo/rsa_algo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

other/RSA_Algo/rsa_algo.py:8:1: W293 Blank line contains whitespace
>>> p = generate_prime_candidate(16)
>>> isprime(p)
True
"""
p = random.getrandbits(length)
while not isprime(p):
p = random.getrandbits(length)
return p

def generate_keys(keysize):
"""
Generate RSA keys.

Check failure on line 21 in other/RSA_Algo/rsa_algo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

other/RSA_Algo/rsa_algo.py:21:1: W293 Blank line contains whitespace
>>> public, private = generate_keys(16)
>>> len(bin(public)) - 2 # Check bit length of n
32
>>> len(bin(private)) - 2 # Check bit length of n
32
"""
try:
e = d = n = 0

p = generate_prime_candidate(keysize)
q = generate_prime_candidate(keysize)

Check failure on line 33 in other/RSA_Algo/rsa_algo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

other/RSA_Algo/rsa_algo.py:33:1: W293 Blank line contains whitespace
n = p * q
phi = (p - 1) * (q - 1)

e = random.randrange(1, phi)
g = gcd(e, phi)
while g != 1:
e = random.randrange(1, phi)
g = gcd(e, phi)

d = mod_inverse(e, phi)

return ((e, n), (d, n))
except Exception as ex:

Check failure on line 46 in other/RSA_Algo/rsa_algo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (BLE001)

other/RSA_Algo/rsa_algo.py:46:12: BLE001 Do not catch blind exception: `Exception`
print(f"Error generating keys: {ex}", file=sys.stderr)
sys.exit(1)

def gcd(a, b):
"""
Compute the greatest common divisor of a and b.

Check failure on line 53 in other/RSA_Algo/rsa_algo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

other/RSA_Algo/rsa_algo.py:53:1: W293 Blank line contains whitespace
>>> gcd(48, 18)
6
"""
while b != 0:
a, b = b, a % b
return a

def encrypt(pk, plaintext):
"""
Encrypt a message with a public key.

Check failure on line 64 in other/RSA_Algo/rsa_algo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

other/RSA_Algo/rsa_algo.py:64:1: W293 Blank line contains whitespace
>>> public, private = generate_keys(16)
>>> encrypted = encrypt(public, "test")
>>> isinstance(encrypted, list)
True
"""
try:
key, n = pk
cipher = [(ord(char) ** key) % n for char in plaintext]
return cipher
except Exception as ex:

Check failure on line 74 in other/RSA_Algo/rsa_algo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (BLE001)

other/RSA_Algo/rsa_algo.py:74:12: BLE001 Do not catch blind exception: `Exception`
print(f"Error during encryption: {ex}", file=sys.stderr)
return None

def decrypt(pk, ciphertext):
"""
Decrypt a message with a private key.

Check failure on line 81 in other/RSA_Algo/rsa_algo.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

other/RSA_Algo/rsa_algo.py:81:1: W293 Blank line contains whitespace
>>> public, private = generate_keys(16)
>>> encrypted = encrypt(public, "test")
>>> decrypted = decrypt(private, encrypted)
>>> decrypted == "test"
True
"""
try:
key, n = pk
plain = [chr((char ** key) % n) for char in ciphertext]
return ''.join(plain)
except Exception as ex:
print(f"Error during decryption: {ex}", file=sys.stderr)
return None

if __name__ == '__main__':
import doctest
doctest.testmod()

try:
keysize = 1024
public, private = generate_keys(keysize)

message = "Hello, RSA!"
print("Original message:", message)

encrypted_msg = encrypt(public, message)
if encrypted_msg:
print("Encrypted message:", encrypted_msg)

decrypted_msg = decrypt(private, encrypted_msg)
if decrypted_msg:
print("Decrypted message:", decrypted_msg)
else:
print("Decryption failed.", file=sys.stderr)
else:
print("Encryption failed.", file=sys.stderr)
except Exception as ex:
print(f"An error occurred: {ex}", file=sys.stderr)
sys.exit(1)
Loading