Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4a34e60

Browse files
RSA_Algorithm added
1 parent a8f5b4c commit 4a34e60

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

‎RSA_Algorithm_Script/README.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# RSA_Algorithm_Script
2+
3+
Short description of package/script
4+
5+
- This Script Was simple to setup
6+
- Just run the main file
7+
8+
## Setup instructions
9+
10+
Just Need to run the RSA_Algorithm_Script.py file and for running python3 is must be installed!
11+
12+
## Detailed explanation of script, if needed
13+
14+
This Script Is Only for RSA_Algorithm_Script use only!
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
def is_prime(n):
2+
if n < 2:
3+
return False
4+
for i in range(2, int(n**0.5) + 1):
5+
if n % i == 0:
6+
return False
7+
return True
8+
9+
def gcd(a, b):
10+
while b != 0:
11+
a, b = b, a % b
12+
return a
13+
14+
def multiplicative_inverse(a, b):
15+
s1, s2, m = 1, 0, b
16+
while b != 0:
17+
q, r = divmod(a, b)
18+
a, b = b, r
19+
s1, s2 = s2, s1 - q * s2
20+
return s1 % m
21+
22+
def powermod(x, y, p):
23+
res = 1
24+
x = x % p
25+
while y > 0:
26+
if y % 2 == 1:
27+
res = (res * x) % p
28+
y //= 2
29+
x = (x * x) % p
30+
return res
31+
32+
def main():
33+
while True:
34+
res = input('Do you want to enter prime numbers (y) or let the algorithm do it for you (n) or exit (e)? (y/n/e): ')
35+
if res == 'y':
36+
while True:
37+
p = int(input('Enter a prime number: '))
38+
if is_prime(p):
39+
break
40+
else:
41+
print(p, 'is not a prime number')
42+
43+
while True:
44+
q = int(input('Enter a different prime number: '))
45+
if is_prime(q) and p * q > 26:
46+
break
47+
else:
48+
print('Both prime numbers are the same or the product is less than 26!')
49+
50+
n = p * q
51+
phi_n = (p - 1) * (q - 1)
52+
53+
while True:
54+
a = int(input(f'Enter a number such that Greatest Common Divisor with {phi_n} is 1: '))
55+
if gcd(a, phi_n) != 1:
56+
continue
57+
else:
58+
break
59+
60+
b = multiplicative_inverse(a, phi_n)
61+
message = input('Enter the message to be encrypted (lower case): ').lower()
62+
63+
encrypted_string = ""
64+
encrypted_num = []
65+
66+
for ch in message:
67+
if ch != ' ':
68+
m = ord(ch) - 97
69+
e = powermod(m, a, n)
70+
encrypted_num.append(e)
71+
encrypted_string += chr(e % 26 + 97)
72+
else:
73+
encrypted_string += ' '
74+
75+
print('Encrypted message is:', encrypted_string)
76+
res = input("Do you want to decrypt it too? (y/n): ")
77+
if res == 'y':
78+
decrypted = ''
79+
j = 0
80+
for ch in encrypted_string:
81+
if ch != ' ':
82+
e = encrypted_num[j]
83+
m = powermod(e, b, n)
84+
ch = chr(m + 97)
85+
decrypted += ch
86+
j += 1
87+
else:
88+
decrypted += ' '
89+
90+
print("Decrypted message is:", decrypted)
91+
else:
92+
ans = input("Do you want to continue? (y/n): ")
93+
if ans == 'y':
94+
continue
95+
else:
96+
break
97+
elif res == 'n':
98+
p, q, a, b = 13, 17, 5, 77
99+
n = p * q
100+
message = input('Enter the message to be encrypted (lower case): ').lower()
101+
102+
encrypted_string = ""
103+
encrypted_num = []
104+
105+
for ch in message:
106+
if ch != ' ':
107+
m = ord(ch) - 97
108+
e = powermod(m, a, n)
109+
encrypted_num.append(e)
110+
encrypted_string += chr(e % 26 + 97)
111+
else:
112+
encrypted_string += ' '
113+
114+
print('Encrypted message is:', encrypted_string)
115+
res = input("Do you want to decrypt it too? (y/n): ")
116+
if res == 'y':
117+
decrypted = ''
118+
j = 0
119+
for ch in encrypted_string:
120+
if ch != ' ':
121+
e = encrypted_num[j]
122+
m = powermod(e, b, n)
123+
ch = chr(m + 97)
124+
decrypted += ch
125+
j += 1
126+
else:
127+
decrypted += ' '
128+
129+
print("Decrypted message is:", decrypted)
130+
else:
131+
ans = input("Do you want to continue? (y/n): ")
132+
if ans == 'y':
133+
continue
134+
else:
135+
break
136+
elif res == 'e':
137+
break
138+
else:
139+
print('Invalid command!')
140+
continue
141+
142+
if __name__ == '__main__':
143+
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /