-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.py
85 lines (79 loc) · 1.3 KB
/
crypto.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
#comments are debugging statements
import pickle
import random
def prime(a):
for i in range (2,a//2+1):
if a%i==0:
return False
else:
return True
def prime_generator(code,count=100):
g=[]
a=1
p=code+1
while a<=count:
if prime(p):
g.append(p)
a+=1
p+=1
return g
def key_generator():
key=random.randint(10,1000)
if prime(key):
return key
else:
return key_generator()
def encrypt(word):
k=[]
code=key_generator()
#print (code)
l=prime_generator(code)
#print (l)
for j in word:
temp=1
m=0
for q in range (48,ord(j)+1):
temp*=l[m]
m+=1
k.append(temp)
return k,code
def decrypt(data,code):
buf=[]
y=prime_generator(code)
#print (y)
for i in data:
ans,j,count=0,0,0
#print (y[j])
#print(i%y[1])
#print('Hello')
while ans==0:
#print (count)
#print ('Hello')
#print(i,j)
#print (y[j])
ans=i%y[j]
#print(i,j)
j+=1
#print(j)
count+=1
count+=46
#Why 46?
buf.append(chr(count))
#print(buf)
s=''
for k in buf:
s+=k
return s
n=input('Enter a Alphanumerino: ')
#print (prime(n))
e,f=encrypt(n)
#print(e)
print ('Your code is: ',f)
filehandler=open('secrets.dat','wb')
pickle.dump(e,filehandler)
filehandler.close()
#print(e,f)
filehandler2=open('secrets.dat','rb')
m=pickle.load(filehandler2)
print (decrypt(m,f))
filehandler2.close()