-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypt.py
More file actions
157 lines (126 loc) · 3.96 KB
/
crypt.py
File metadata and controls
157 lines (126 loc) · 3.96 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'''Crypt
This file contains encryption & decryption Functions.
This will be imported into any encryption related programs.
REQ: for simplicity I have refined this algorithm to members of the alphabet
ie. special characters and numbers will not be decrypted accurately
'''
import random
global __start__
__start__ = ord('a')
global __end__
__end__ = ord('z')
def encrypt(string):
'''(str)->(dict)
The encrypt function takes a string a returns the randomly generated key
with the encrypted text in an associative array.
>>> encrypt('a')
{21: 'v'}
>>> encrypt('whatever')
{17: 'oyrlvnvj'}
>>> encrypt('What is life')
{23: 'ufxr gq jgdc'}
'''
_key = 0
afterstr = ''
__afterdict__= {}
b4str = str(string).lower()
# first things first gotta get a key
# note 26 letters in the alphabet & 10 ints + special chars
# note to self: remember ord() and char()
_key = random.randint(1, 25)
'''the key will serve as a method to varify the correct reciever and to
encrypt the cyphertext'''
# make a list of the elements in the cyphertext
b4lst = list(b4str)
#make list of ascii numbers in string
b4lst2 = []
for i in b4lst:
if(i == ' '):
b4lst2.append(' ')
else:
b4lst2.append(ord(i))
# make the list of new ascii values
i = 0
afterlst = []
while(i < len(b4lst2)):
if(b4lst2[i] != ' '):
if((b4lst2[i] + _key) >= __end__):
#if it exceeds the letter range
diff = (b4lst2[i] + _key) - __end__
afterlst.append(diff + __start__)
i+=1
elif((b4lst2[i] + _key) <= __end__):
#if it is within the letter range
afterlst.append((b4lst2[i] + _key))
i+=1
else:
afterlst.append(' ')
i+=1
# now we have to change the new ascii values back into characters
afterlst2 = []
for i in afterlst:
if(i == ' '):
afterlst2.append(' ')
else:
afterlst2.append(chr(i))
# gotta stringyfy afterlst2
afterstr = stringyfy(afterlst2)
# gotta remember the key and the encrypted text so we can decrypt
__afterdict__ = {_key: afterstr}
return __afterdict__
def decrypt(dic):
'''(dict)-> (str)
this function will take __afterdict__ and reverse the encrytion function to
produce the origianl cypher text with the help of the key of course!
>>> decrypt({25: 'z'})
'a'
>>> decrypt({17: 'oyrlvnvj'})
'whatever'
>>> decrypt(encrypt('What is life'))
'What is life'
'''
# remember dict.keys() & dict.vaules()
# get the key & the encrypted txt
keys = list(dic.keys())
key = int(keys[0])
cryptxt = dic.get(key)
# apply decryption algorithm
#make a list of decrypted ascii values for cyphertxt
numtxt = []
for i in list(cryptxt):
if(i == ' '):
numtxt.append(' ')
# special case
elif((ord(str(i)) - key) < __start__):
diff1 = ord(str(i)) - __start__
diff2 = key - diff1
final = __end__ - diff2
numtxt.append(final)
else:
numtxt.append(ord(str(i)) - key)
#make decrpyt list
dcryptlst = []
for i in numtxt:
if(i == ' '):
dcryptlst.append(' ')
else:
dcryptlst.append(chr(i))
#make it a astring
final_string = stringyfy(dcryptlst)
return final_string
#-----------------------------------------------------------------------------#
def stringyfy(lst):
'''(list) -> string
when a list is imuted it takes all the elements and concatenates
them to make a string.
>>> stringyfy([1,2,3,4])
'1234'
>>> stringyfy(['me',' ','is',' ','so',' ','fine'])
'me is so fine'
REQ: integers, floats, strings and characters ONLY!
'''
#in retrospect not the longest of codes :)
nstring = ''
for i in lst:
nstring = nstring + str(i)
return nstring