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

File tree

5 files changed

+208
-0
lines changed

5 files changed

+208
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# For caeser cipher the key size is fixed as: key = 3
2+
3+
import string
4+
5+
key = 3
6+
# store all the alphabets in upper and lowercase in a String variable
7+
all_letters = string.ascii_letters
8+
9+
# taking inputs
10+
plain_txt = input("Enter the text you want to Encrypt: \n")
11+
cipher_txt = []
12+
13+
# create a dictionary which maps the alphabets with respective substitution as per key size
14+
dict1 = {}
15+
for i in range(len(all_letters)):
16+
dict1[all_letters[i]] = all_letters[(i + key) % len(all_letters)]
17+
18+
# iterate through the plaintext characters and do necessary substittion
19+
for char in plain_txt:
20+
if char in all_letters:
21+
temp = dict1[char]
22+
cipher_txt.append(temp)
23+
else:
24+
temp = char
25+
cipher_txt.append(temp)
26+
27+
# concatenate all the list elements
28+
cipher_txt = "".join(cipher_txt)
29+
30+
# create a dictionary for reverse mapping
31+
dict2 = {}
32+
for i in range(len(all_letters)):
33+
dict2[all_letters[i]] = all_letters[(i - key) % (len(all_letters))]
34+
decrypt_txt = []
35+
36+
for char in cipher_txt:
37+
if char in all_letters:
38+
temp = dict2[char]
39+
decrypt_txt.append(temp)
40+
else:
41+
temp = char
42+
decrypt_txt.append(temp)
43+
decrypt_txt = "".join(decrypt_txt)
44+
45+
# display the Cipher Text and Recovered Plain Text
46+
print("\nCipher Text is: ", cipher_txt)
47+
print("Recovered plain text: ", decrypt_txt)
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import string
2+
3+
# store all the alphabets in upper and lowercase in a String variable
4+
all_letters = string.ascii_letters
5+
6+
# taking inputs
7+
key = int(input("Enter the key value in integer: \n"))
8+
plain_txt = input("Enter the text you want to Encrypt: \n")
9+
cipher_txt = []
10+
11+
# create a dictionary which maps the alphabets with respective substitution as per key size
12+
dict1 = {}
13+
for i in range(len(all_letters)):
14+
dict1[all_letters[i]] = all_letters[(i + key) % len(all_letters)]
15+
16+
# iterate through the plaintext characters and do necessary substittion
17+
for char in plain_txt:
18+
if char in all_letters:
19+
temp = dict1[char]
20+
cipher_txt.append(temp)
21+
else:
22+
temp = char
23+
cipher_txt.append(temp)
24+
25+
# concatenate all the list elements
26+
cipher_txt = "".join(cipher_txt)
27+
28+
# create a dictionary for reverse mapping
29+
dict2 = {}
30+
for i in range(len(all_letters)):
31+
dict2[all_letters[i]] = all_letters[(i - key) % (len(all_letters))]
32+
decrypt_txt = []
33+
34+
for char in cipher_txt:
35+
if char in all_letters:
36+
temp = dict2[char]
37+
decrypt_txt.append(temp)
38+
else:
39+
temp = char
40+
decrypt_txt.append(temp)
41+
decrypt_txt = "".join(decrypt_txt)
42+
43+
# display the Cipher Text and Recovered Plain Text
44+
print("\nCipher Text is: ", cipher_txt)
45+
print("Recovered plain text: ", decrypt_txt)

0 commit comments

Comments
 (0)