@SureshPatil/

Affine Cipher

Python

No description

fork
loading
Files
  • main.py
main.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from math import gcd
from collections import Counter
# import sympy

def c2i(c, alphabet):
  return alphabet.index(c)
    #"""Returns the index of c in the string alphabet, starting at 0"""
    # Copy your method from subcipher.py here

def i2c(i, alphabet):
  return alphabet[i]
    #"""Returns the character at index i in alphabet"""
    # Copy your method from subcipher.py here

def prepare_string(s, alphabet):
  newS = ""
  for char in s:
    if char in alphabet: 
      newS += char
  return newS
    #"""removes characters from s not in alphabet, returns new string"""
    # Copy your method from subcipher.py here

def mod_inverse(a, m):
  for x in range(1,m):
    if((a*x)%m == 1):
      return x
  print("No modular inverse found!")
  return -1

def isNotCoprime(a, lenAlpha):
  if lenAlpha % a == 0:
    return True
  for x in range(2, a):
    if lenAlpha % x == 0 and a % x == 0:
      return True
  return False

def numCoprime(x):
  num = x
  for i in range(1, x):
    if isNotCoprime(i, x):
      num -= 1
  return num

def coprimes(x):
  num = x
  coprimes = []
  for i in range(1, x):
    if isNotCoprime(i, x):
      num -= 1
    else:
      coprimes.append(i)
  return coprimes

def affine_encode(plaintext, alphabet, a, b):
  if(isNotCoprime(a, len(alphabet))):
    print("Invalid a-value!")
    return -1
  else:
    ciphertext = ''
    for letter in plaintext:
      x = c2i(letter, alphabet)
      encX = (a*x + b) % len(alphabet)
      ciphertext += i2c(encX, alphabet)
    return ciphertext

def affine_decode(ciphertext, alphabet, a, b):
  if(isNotCoprime(a, len(alphabet))):
    print("Invalid a-value!")
    return -1
  else:
    plaintext = ''
    for letter in ciphertext:
      encX = c2i(letter, alphabet)
      aInv = mod_inverse(a, len(alphabet))
      x = (aInv*(encX - b) + len(alphabet)) % len(alphabet)
      plaintext += i2c(x, alphabet)
    return plaintext

# def phi(n):
#     y = n
#     for i in range(2,n+1):
#         if sympy.isprime(i) and n % i == 0:
#             y *= 1 - 1.0/i
#     return int(y)
  
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# newAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"

# # Ex. 1
# plaintext_1 = prepare_string("THISISATEST", alpha)
# ciphertext_1 = affine_encode(plaintext_1, alpha, 17, 4)
# print(ciphertext_1)

# # Ex. 2
# ciphertext_2 = prepare_string("WIGZEYYPTUPUYP", alpha)
# plaintext_2 = affine_decode(ciphertext_2, alpha, 17, 4)
# print(plaintext_2)

# print("New Alpha beyond this point! ------------------------------")
# #Ex. 3
# plaintext_3 = prepare_string("C01DB100D3DC0D3R5!", newAlpha)
# ciphertext_3 = affine_encode(plaintext_3, newAlpha, 26, 5)
# print(ciphertext_3)
# plaintext_3 = affine_decode(ciphertext_3, newAlpha, 26, 5)
# print(plaintext_3)

# print("Table beyond this point! ------------------------------")

# for x in range(20, 61):
#   numA = numCoprime(x)
#   numB = x
#   numCombs = numA*numB - 1
#   print(x, numA, numB, numCombs)

# print(numCoprime(99999))

def d2i(d, alphabet):
  return c2i(d[0], alphabet)*len(alphabet) + c2i(d[1], alphabet)

def i2d(i, alphabet):
  d = ''
  d += i2c(i//len(alphabet), alphabet)
  d += i2c(i - (i//len(alphabet))*len(alphabet), alphabet)
  return d

def affine_encode_digraphs(plaintext, alphabet, a, b):
  if(len(plaintext) % 2  != 0):
    plaintext += 'X'
  ciphertext = ''
  for index in range(0, len(plaintext), 2):
    x = d2i(plaintext[index : index + 2], alphabet)
    encX = (a*x + b) % (len(alphabet) * len(alphabet))
    ciphertext += i2d(encX, alphabet)
  return ciphertext

def affine_decode_digraphs(ciphertext, alphabet, a, b):
  plaintext = ''
  for index in range(0, len(ciphertext), 2):
    encX = d2i(ciphertext[index : index + 2], alphabet)
    aInv = mod_inverse(a, len(alphabet)*len(alphabet))
    x = (aInv*(encX - b)) % (len(alphabet) * len(alphabet))
    plaintext += i2d(x, alphabet)
  return plaintext

# # Ex. 4
# plaintext_4 = prepare_string("THISISANOTHERTEST", alpha)
# ciphertext_4 = affine_encode_digraphs(plaintext_4, alpha, 81, 119)
# print(ciphertext_4) 

# # Ex. 5
# ciphertext_5 = prepare_string("QFHIIRTBUUYNNUURJPXDYWFG", alpha)
# plaintext_5 = affine_decode_digraphs(ciphertext_5, alpha, 81, 119)
# print(plaintext_5) 
# print(phi(26*26*26))
# print(26*26)
# print(phi(26*26) * 26*26 - 1)

# Ex. 2
# ciphertext_2 = prepare_string("XeiackzmpowbytywyvofTyterywxabkpauyrgtpoubargkqm", alpha)
# possibilities = []
# crib = "FAMOUS"
# for i in coprimes(26):
#   for j in range(26):
#     plaintext_2 = affine_decode(ciphertext_2, alpha, i, j)
#     possibilities.append(plaintext_2)

# for poss in possibilities:
#   if crib in poss and poss.index(crib) == 0:
#     print(poss)

newAlpha = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz.!?, ;:'

plaintext_4 = "I am the tide, the oceans sing"
ciphertext_4 = affine_encode_digraphs(plaintext_4, newAlpha, 137, 241)
print(ciphertext_4) 

ciphertext_5 = "NzPbuTULVBcGGgvjDFCss;qzZsTzZOYqyzvViQ"
plaintext_5 = affine_decode_digraphs(ciphertext_5, newAlpha, 91, 1091)
print(plaintext_5) 

# print(plaintext_2)