Cryptography Algorithms to encrypt and decrypt · hashfx/Amazing-Python-Scripts@cfa703a · GitHub
Skip to content

Commit cfa703a

Browse files
committed
Cryptography Algorithms to encrypt and decrypt
Create an Algorithm directory under Cryptography and create scripts for Cryptography Algorithms like RSA, Diffie Hellman, Caesar Cipher and Substitution Cipher to perform key exchange, encryption and decryption of plain text.
1 parent d16ae2a commit cfa703a

5 files changed

Lines changed: 208 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
n = int(input("prime no: \n"))
2+
g = int(input("prime no: \n"))
3+
x = 3
4+
y = 6
5+
6+
7+
def al():
8+
A = (g**x) % n
9+
return A
10+
11+
12+
def bob():
13+
B = (g**y) % n
14+
return B
15+
16+
17+
def akey():
18+
Bval = bob()
19+
k1 = (Bval**x) % n
20+
return k1
21+
22+
23+
def bkey():
24+
Aval = al()
25+
k2 = (Aval**y) % n
26+
return k2
27+
28+
29+
if akey() == bkey():
30+
print("shared symmetric key")
31+
print(akey(), bkey())
32+
else:
33+
print("error")

Cryptography/Algorithms/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Cryptography Algorithms
2+
3+
# Description
4+
Its a collection of different cryptography algorithms used for encryption and key exchange protocols.
5+
Following are the list of algorithms:
6+
1. Ceaser Cipher
7+
2. Diffie Hellman Key Exchange Protocol
8+
3. RSA Encryption and Decryption
9+
4. Substitution Cipher
10+
11+
# Getting Started
12+
1. Run the programs locally
13+
2. Input the parameters such as key size and plain text when prompted

Cryptography/Algorithms/RSA.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import math
2+
import random
3+
4+
5+
# Arguments provide range to generate two prime numbers
6+
def primenumbers(srt, n):
7+
primenos = []
8+
prime = [True for i in range(n + 2 - srt)]
9+
prime[0] = False
10+
prime[1] = False
11+
for p in range(srt, int(math.sqrt(n)) + 1):
12+
if prime[p] == True:
13+
for i in range(p * p, n + 1, p):
14+
prime[i] = False
15+
for p in range(srt, n + 1):
16+
if prime[p]:
17+
primenos.append(p)
18+
P = random.choice(primenos)
19+
primenos.remove(P)
20+
Q = random.choice(primenos)
21+
return (P, Q)
22+
23+
24+
# RSA Algorithm Implementation:
25+
print("RSA Implementation starts for the connection")
26+
for c in range(5):
27+
print("------")
28+
29+
p, q = primenumbers(1, 30)
30+
N = p * q
31+
f = (p - 1) * (q - 1)
32+
33+
34+
# Generate Public Key for Encryption
35+
def publickey():
36+
for i in range(2, f):
37+
if math.gcd(f, i) == 1:
38+
E = i
39+
break
40+
return E
41+
42+
43+
E = publickey()
44+
45+
46+
def privatekey():
47+
for j in range(1, N):
48+
if math.fmod((E * j), (f)) == 1:
49+
# if (E * j) % (f) == 1:
50+
D = j
51+
break
52+
return D
53+
54+
55+
D = privatekey()
56+
print("Generated Public Key for Encryption E: ", E)
57+
print("Generated Private Key for Decryption D: ", D)
58+
Plain_text = float(input("Enter Plain text in numerical data type: \n"))
59+
60+
x = math.pow(Plain_text, E)
61+
Cipher_text = math.fmod(x, N)
62+
print("Generated Cipher Text using Public Key: ", Cipher_text)
63+
64+
D_user = int(input("Enter your private key for Decryption \n"))
65+
if D_user == D:
66+
y = math.pow(Cipher_text, D)
67+
Decrypted_text = math.fmod(y, N)
68+
print("The Cipher Text has been decrypted: ", Plain_text)
69+
else:
70+
print("ENTERED WRONG PRIVATE KEY")
Lines changed: 45 additions & 0 deletions

0 commit comments

Comments
 (0)