Skip to content

Algo added #11869

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 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
Empty file added other/RSA_Algo/__init__.py
Empty file.
138 changes: 138 additions & 0 deletions other/RSA_Algo/rsa_algo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import random
import sys
from sympy import isprime, mod_inverse

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

Choose a reason for hiding this comment

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

Please provide return type hint for the function: generate_prime_candidate. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: length

"""
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):

Choose a reason for hiding this comment

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

Please provide return type hint for the function: generate_keys. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: 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 ValueError as ex:
print(f"Value error generating keys: {ex}", file=sys.stderr)
sys.exit(1)
except TypeError as ex:
print(f"Type error generating keys: {ex}", file=sys.stderr)
sys.exit(1)
except Exception as ex:

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (BLE001)

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

def gcd(a, b):

Choose a reason for hiding this comment

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

Please provide return type hint for the function: gcd. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: a

Please provide descriptive name for the parameter: a

Please provide type hint for the parameter: b

Please provide descriptive name for the parameter: b

"""
Compute the greatest common divisor of a and b.

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

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

def encrypt(pk, plaintext):

Choose a reason for hiding this comment

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

Please provide return type hint for the function: encrypt. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: pk

Please provide type hint for the parameter: plaintext

"""
Encrypt a message with a public key.

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

other/RSA_Algo/rsa_algo.py:70: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 TypeError as ex:
print(f"Type error during encryption: {ex}", file=sys.stderr)
return None
except Exception as ex:

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (BLE001)

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

def decrypt(pk, ciphertext):

Choose a reason for hiding this comment

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

Please provide return type hint for the function: decrypt. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: pk

Please provide type hint for the parameter: ciphertext

"""
Decrypt a message with a private key.

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

other/RSA_Algo/rsa_algo.py:90: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 TypeError as ex:
print(f"Type error during decryption: {ex}", file=sys.stderr)
return None
except Exception as ex:

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (BLE001)

other/RSA_Algo/rsa_algo.py:104:12: BLE001 Do not catch blind exception: `Exception`
print(f"Unexpected 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 ValueError as ex:
print(f"Value error: {ex}", file=sys.stderr)
sys.exit(1)
except TypeError as ex:
print(f"Type error: {ex}", file=sys.stderr)
sys.exit(1)
except Exception as ex:
print(f"Unexpected error: {ex}", file=sys.stderr)
sys.exit(1)
Loading