Skip to content

Commit 152593e

Browse files
committed
Initial commit, with everything up to first encryption ver1, using fibonacci and pidigits.
0 parents  commit 152593e

File tree

6 files changed

+97
-0
lines changed

6 files changed

+97
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__/

encryption1.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from fibonacci import fibonacci
2+
from pi import pi
3+
4+
def generatekey(keylength):
5+
fibonacci_lst = fibonacci(keylength)
6+
pi_lst = pi(keylength)
7+
key = []
8+
for i in range(0,keylength):
9+
element = (fibonacci_lst[i]+pi_lst[i])%26
10+
key.append(element)
11+
return key
12+
13+
def encrypt(message):
14+
message = message.replace(" ","") #Cleaning input
15+
message = message.lower() #Cleaning input
16+
message_lst_chars = [] #list of characters in message
17+
message_lst = [] #List of message as numerals where a=1, b=2, etc
18+
encrypted_lst_char = [] #List of characters of ciphertext
19+
encrypted_lst = [] #List of ciphertext as numerals
20+
for letter in message: #converts plaintext str to lst
21+
message_lst_chars.append(letter)
22+
for char in message_lst_chars: #converts plaintext lst of chars to numbers
23+
message_lst.append(ord(char)-96)
24+
#print(message_lst)
25+
key_lst = generatekey(len(message_lst))
26+
#print(key_lst)
27+
for n in range(0,len(message_lst)): #Modulo arithmetic to encrypt plaintext
28+
element = (message_lst[n] + key_lst[n])%26
29+
encrypted_lst.append(element)
30+
#print(encrypted_lst)
31+
for x in encrypted_lst:
32+
if x==0: #0 as 26, because modulo 26 returns 0
33+
encrypted_lst_char.append("z")
34+
else: #Converts ciphertext lst of numbers to chars
35+
encrypted_lst_char.append(chr(x+96))
36+
encrypted = "".join(encrypted_lst_char)
37+
return encrypted
38+
39+
print(encrypt("Secret Message"))

fibonacci.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def fibonacci(length):
2+
lst = [0,1]
3+
while len(lst) < length:
4+
lst.append(lst[-1]+lst[-2])
5+
return lst
6+
7+
#print(fibonacci(20))

keytest1.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from encryption1 import generatekey
2+
3+
key = generatekey(100000)
4+
distribution={}
5+
for i in key:
6+
distribution[i] = distribution.get(i,0) + 1
7+
print(distribution)
8+
9+
#Frequency analysis of pi digits
10+
"""
11+
from pi import pi
12+
13+
key = pi(10000)
14+
distribution={}
15+
for i in key:
16+
distribution[i] = distribution.get(i,0) + 1
17+
print(distribution)
18+
"""

pi.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def pi(length):
2+
from mpmath import mp
3+
lst=[]
4+
mp.dps=length+1#Finds the length of pi to 1 more than necessary
5+
#Done so that rounding errors are not affecting the output
6+
#Credit to the developer(s) at mpmath
7+
pistring = str(mp.pi)
8+
pistring = pistring[0]+pistring[2:]
9+
for digit in pistring:
10+
lst.append(int(digit))
11+
return lst[0:-1] #This is done to shorten by 1
12+
13+
#print(pi(20))

primes.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def primes(length):
2+
if length==0:
3+
return []
4+
if length==1:
5+
return [2]
6+
lst = [2,3]
7+
num = 5
8+
while len(lst) < length:
9+
isprime = True
10+
for i in range(2,num//2):
11+
if num%i==0:
12+
isprime = False
13+
break
14+
if isprime == True:
15+
lst.append(num)
16+
num+=1
17+
return lst
18+
19+
#print(primes(20))

0 commit comments

Comments
 (0)