@qtip2001/

Affine

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
# Quentin Bishop
# 11 Oct 2018

def prepare_string(s, alphabet):
  for char in s:
    if char not in alphabet:
      s.replace(char,"")

  return s.lower()

def c2i(c, alphabet):
  if c in alphabet:
    return alphabet.index(c)
  else:
    print("Error")

def i2c(i, alphabet):
    return alphabet[i]

def mod_inverse(a, m):
  for x in range(m):
    if (x*a)%m == 1:
      return x
  print("Error")

def affine_encode(plaintext, alphabet, a, b):
  l = []
  for x in plaintext:
    letter = c2i(x, alphabet)
    encrypted = (a*letter + b) % len(alphabet)
    l.append(i2c(encrypted, alphabet))
  return "".join(l)

def affine_decode(ciphertext, alphabet, a, b):
  l = []
  for x in ciphertext:
    letter = c2i(x, alphabet)
    decrypted = mod_inverse(a,len(alphabet))*(letter-b)%len(alphabet)
    l.append(i2c(decrypted, alphabet))
  return "".join(l)

def gcd(a, b):
  while b != 0:
      a, b = b, a % b
  return a

def coprime(a, b):
  return gcd(a, b) == 1

def a_vals(alpha, m):
  total = 1
  for x in range(2, alpha):
    if coprime(x, alpha):
      total += 1
  return total

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

def i2d(i, alphabet):
  b = i2c(i%len(alphabet), alphabet)
  a = i2c(i//(len(alphabet)), alphabet)
  return(a,b)

def affine_encode_digraphs(plaintext, alphabet, a, b):
  l = []
  if len(plaintext)%2 == 1:
    plaintext = plaintext+"x"

  for x in range(len(plaintext)//2):
    d = (plaintext[2*x],plaintext[2*x+1])
    letter = d2i(d, alphabet)
    encrypted = (a*letter + b) % (len(alphabet)*len(alphabet))
    l.append(i2d(encrypted, alphabet)[0])
    l.append(i2d(encrypted, alphabet)[1])
  return "".join(l) 

def affine_decode_digraphs(ciphertext, alphabet, a, b):
  l = []
  for x in range(len(ciphertext)//2):
    d = (ciphertext[2*x],ciphertext[2*x+1])
    letter = d2i(d, alphabet)

    decrypted = mod_inverse(a,(len(alphabet)*len(alphabet)))*(letter-b)%(len(alphabet)*len(alphabet))
    l.append(i2d(decrypted, alphabet)[0])
    l.append(i2d(decrypted, alphabet)[1])
  return "".join(l)

#text = "sdshrkuvviguosfahfsvshrsz"
alpha = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz.!?, ':"
text = "I'm playing time against my troubles"
#text = input("Enter your string: ")
#crib = prepare_string(input("Enter your crib: "), alpha)
print(affine_encode_digraphs(text,alpha,137,241))

'''print(text)

common = "the and you that was for are with his they one have this from had hot word but what some can out other were all there when use your how said each she which their time will way about many then them write would like these her long make thing see him "
commonwords = common.split(" ")

lst = []

for x in range(26):
  for y in range(26):
    if(coprime(x, 26)):
      temp = affine_decode(text, alpha, x, y)
      count = 0
      for b in commonwords:
        if b in temp:
          count+= 1
      lst.append((count, temp))
lst.sort()
print(lst[-1])'''