-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa.py
101 lines (89 loc) · 4.4 KB
/
rsa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import sympy
import math
# The RSA class generates public and private keys, and provides methods for encrypting and decrypting
# messages using the RSA algorithm.
class RSA:
def __init__(self, startPrime=100000, endPrime=1000000):
self.P = 139273 # sympy.randprime(100000, 1000000) # 139273
self.Q = 139291 # sympy.nextprime(self.P) # 139291
self.E = 11 # 11
self.D = 7054253411 # 7054253411
self.generatePrimes(startPrime, endPrime)
self.N = self.P * self.Q
self.PHIN = (self.P - 1) * (self.Q - 1)
self.generate_public_key()
self.generate_private_key()
print(self.P, self.Q, self.D, self.E, self.PHIN, self.N)
def generatePrimes(self, startPrime, endPrime):
"""
This function generates two random prime numbers within a given range, ensuring that they are not
equal.
:param startPrime: The smallest prime number that can be generated by the function
:param endPrime: The largest prime number that can be generated. The function will generate prime
numbers between startPrime and endPrime
"""
self.P = sympy.randprime(startPrime, endPrime)
self.Q = sympy.randprime(startPrime, endPrime)
while self.P == self.Q:
self.Q = sympy.randprime(startPrime, endPrime)
def get_public_key(self):
"""
The function returns a tuple containing the values of E and N.
:return: A tuple containing the public key values E and N.
"""
return (self.E, self.N)
def get_private_key(self):
"""
The function returns the private key of an object.
:return: The private key `D` is being returned.
"""
return self.D
def generate_public_key(self):
"""
This function generates a public key by finding a suitable value for E that is coprime with PHIN.
"""
for i in range(self.PHIN, 2, -1):
if math.gcd(i, self.PHIN) == 1:
self.E = i
break
def generate_private_key(self):
"""
This function generates a private key using the values of E, PHIN, and D.
"""
self.D = pow(self.E, -1, self.PHIN)
def fast_power(self, base, exponent, modulus):
"""
The function calculates the result of raising a base to a given exponent modulo a given modulus
using a fast power algorithm.
:param base: The base number that will be raised to the exponent power
:param exponent: The exponent is the power to which the base is raised. For example, in the
expression 2^3, the base is 2 and the exponent is 3
:param modulus: Modulus is a mathematical operation that returns the remainder when one number is
divided by another. In the context of this function, modulus is used to ensure that the result of
the calculation stays within a certain range. The modulus parameter in this function is the number
by which the result is divided to get the
:return: The function `fast_power` returns the result of raising the `base` to the power of
`exponent` modulo `modulus`.
"""
result = 1
while exponent > 0:
if exponent % 2 == 1:
result = (result * base) % modulus
base = (base * base) % modulus
exponent //= 2
return result
def RSA(self, message, exponent, N):
"""
This function implements the RSA encryption algorithm by computing the fast power of a message,
exponent, and modulus.
:param message: The message is the plaintext that needs to be encrypted using the RSA algorithm. It
is usually represented as a number or a string of characters
:param exponent: The exponent is a positive integer used in the RSA encryption algorithm to encrypt
and decrypt messages. It is typically a large prime number and is kept secret by the owner of the
private key. The exponent is used to raise the message to a power modulo N, where N is the product
of two large prime
:param N: N is the product of two large prime numbers p and q, which are kept secret. It is used as
the modulus for the RSA encryption and decryption operations
:return: the result of the `fast_power` method with the arguments `message`, `exponent`, and `N`.
"""
return self.fast_power(message, exponent, N)