-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
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
Algo added #11869
Changes from 3 commits
66c1b0e
2f166eb
6b3f930
452470a
2a1c367
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import random | ||
import sys | ||
from sympy import isprime, mod_inverse | ||
from typing import Tuple, List | ||
Check failure on line 4 in other/RSA_Algo/rsa_algo.py
|
||
|
||
def generate_prime_candidate(length: int) -> int: | ||
""" | ||
Generate a large prime number candidate. | ||
|
||
>>> 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: int) -> Tuple[Tuple[int, int], Tuple[int, int]]: | ||
Check failure on line 19 in other/RSA_Algo/rsa_algo.py
|
||
""" | ||
Generate RSA keys. | ||
|
||
>>> 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) | ||
|
||
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: | ||
print(f"Unexpected error generating keys: {ex}", file=sys.stderr) | ||
sys.exit(1) | ||
|
||
def gcd(a: int, b: int) -> int: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: |
||
""" | ||
Compute the greatest common divisor of a and b. | ||
|
||
>>> gcd(48, 18) | ||
6 | ||
""" | ||
while b != 0: | ||
a, b = b, a % b | ||
return a | ||
|
||
def encrypt(pk: Tuple[int, int], plaintext: str) -> List[int]: | ||
""" | ||
Encrypt a message with a public key. | ||
|
||
>>> 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: | ||
print(f"Unexpected error during encryption: {ex}", file=sys.stderr) | ||
return None | ||
|
||
def decrypt(pk: Tuple[int, int], ciphertext: List[int]) -> str: | ||
""" | ||
Decrypt a message with a private key. | ||
|
||
>>> 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: | ||
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter:
a
Please provide descriptive name for the parameter:
b