forked from MartaVohnoutovaBukovec/IOS-655-Python-a-Bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mCipher.py
147 lines (115 loc) · 4.21 KB
/
mCipher.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
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
# Module mCipher
# Vigerere cipher function
# Caesar cipher function
# Affine cipher
# Vernam cipher
import math as m
from operator import itemgetter, attrgetter
def vigenere(s,k,en):
s,k=s.replace(" ","").upper(),k.replace(" ","").upper() # remove all spaces and chenge to all capitals - for string and key
out_s = ""
if len(s) > len(k):
k = (k * len(s))[0:len(s)] # add key to the length of string
else:
k = k[:len(s)]
if en:
for i in range(len(s)):
if ord(s[i]) in range(65,91): # outside alphabets let it be as it is
number_ord = ord(s[i]) + ord(k[i]) - 65
if number_ord > 90: # adjust alphabet boundary
number_ord -= 26
else:
number_ord = ord(s[i])
out_s = out_s + chr(number_ord)
else:
for i in range(len(s)):
if ord(s[i]) in range(65,91): # outside alphabets let it be as it is
number_ord = ord(s[i]) - ord(k[i]) + 65
if number_ord < 65: # adjust alphabet boundary
number_ord += 26
else:
number_ord = ord(s[i])
out_s = out_s + chr(number_ord)
return out_s
def caesar_cipher(s,move,en):
s_en = ""
s_i = ""
if not en:
for i in s.upper():
if ord(i) in range(65,91):
j = ord(i) + move
if j not in range(65,90):
j -= 26
else:
j = ord(i)
s_en += chr(j)
else:
for i in s.upper():
if ord(i) in range(65,91):
j = ord(i) - move
if j not in range(65,90):
j += 26
else:
j = ord(i)
s_en += chr(j)
return s_en
def getKey(item):
return item[1]
def trans_cipher(string,keyword,en):
k = list(enumerate(keyword.upper()))
k = sorted(k, key=itemgetter(1))
keys = [(i,k[i][0]) for i in range(len(k))]
string_en=""
if en:
for i in range(0,len(string),len(keys)):
for j in keys:
try:
string_en += string[i+j[1]]
except IndexError:
pass
else:
keys = sorted(keys,key=getKey)
for i in range(0,len(string),len(keys)):
for j in keys:
try:
string_en += string[i+j[0]]
except IndexError:
pass
return string_en
def affine_cipher(string,en):
s_en=""
if en:
try:
for i in string:
if i.isalpha():
s_en += chr((5*(ord(i.upper()) - 65) + 8) % 26 + 65).upper()
else:
s_en += i
except IndexError:
pass
else:
try:
for i in string:
if i.isalpha():
s_en += chr(21*((ord(i.upper()) - 65) - 8) % 26 + 65).upper()
else:
s_en += i
except IndexError:
pass
return s_en
# Vernam cypher demo
# T Street 2015-10-16
def makeVernamCypher( text, key ):
""" Returns the Vernam Cypher for given string and key """
answer = "" # the Cypher text
p = 0 # pointer for the key
for char in text:
answer += chr(ord(char) ^ ord(key[p]))
# print(ord(char) ^ ord(key[p]))
p += 1
if p==len(key):
p = 0
return answer
# mCipher.trans_cipher("I have a cat her name is Tit and on the mat she loves to sit","Python",1)
# mCipher.vigenere("I have a cat her name is Tit and on the mat she loves to sit","python",1)
# mCipher.caesar_cipher("I have a cat her name is Tit and on the mat she loves to sit",13,1)