-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.py
More file actions
executable file
·83 lines (67 loc) · 1.99 KB
/
crypto.py
File metadata and controls
executable file
·83 lines (67 loc) · 1.99 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
import math
import random
def alpha2num(x):
return ord(x) - 97
def num2alpha(x):
return chr(97 + x)
def make_key(x):
key = ""
for i in range(x):
key += num2alpha(random.randrange(26))
return key
def affine_char_enc(a, b, x):
return num2alpha((a*alpha2num(x) + b) % 26)
def affine_char_dec(a, b, x):
inverses = {1 : 1, 3 : 9, 5 : 21, 7 : 15, 9 : 3, 11 : 19, 15 : 7, 17 : 23, 19 : 11, 21 : 5, 23 : 17, 25 : 25}
return num2alpha((inverses[a]*alpha2num(x) - inverses[a]*b) % 26)
def affine(a,b, plaintext):
ciphertext = ""
for i in plaintext:
ciphertext += affine_char_enc(a,b,i)
return ciphertext
def affine_dec(a,b,ciphertext):
plaintext = ""
for i in ciphertext:
plaintext += affine_char_dec(a,b,i)
return plaintext
def vig_key_resize(key, length):
newkey = key * (length/len(key)+1)
return newkey[:length]
def vigenere(plaintext, key):
fullkey = vig_key_resize(key, len(plaintext))
ciphertext = ""
for i in range(len(plaintext)):
ciphertext += affine_char_enc(1,alpha2num(fullkey[i]),plaintext[i])
return ciphertext
def vigenere_dec(ciphertext, key):
fullkey = vig_key_resize(key, len(ciphertext))
plaintext = ""
for i in range(len(ciphertext)):
plaintext += affine_char_dec(1,alpha2num(fullkey[i]),ciphertext[i])
return plaintext
def preptext(text):
newtext = ""
for i in text.lower():
if 97 <= ord(i) and ord(i) <= 122: # between 'a' and 'z'
newtext += i
return newtext
def prepfile(filename):
f = open(filename,"r")
text = f.read()
newtext = ""
for i in text.lower():
if 97 <= ord(i) and ord(i) <= 122: # between 'a' and 'z'
newtext += i
return newtext
def count_chars(text):
stat_dict = {}
#stat_dict["len"] = len(text)
for i in range(26):
stat_dict[num2alpha(i)] = text.count(num2alpha(i))
#stat_dict[num2alpha(i) + "%"] = float(stat_dict[num2alpha(i)]) / float(stat_dict["len"])
return stat_dict
def char_percent(count, length):
stat_dict = {}
for i in range(26):
stat_dict[num2alpha(i)] = float(count[num2alpha(i)]) / float(length)
return stat_dict