Skip to content

Instantly share code, notes, and snippets.

@themaddoctor
Created April 10, 2020 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themaddoctor/f5c91936811d1788a138482242bda3e8 to your computer and use it in GitHub Desktop.
Save themaddoctor/f5c91936811d1788a138482242bda3e8 to your computer and use it in GitHub Desktop.
double affine encryptor
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def encrypt(p,k):
a = k[0]
b = k[1]
if len(p)%2 == 1:
p += "X"
c = ""
for i in range(len(p)//2):
value = ALPHABET.index(p[2*i])*26 + ALPHABET.index(p[2*i+1])
value = (a*value + b)%(26*26)
c += ALPHABET[value//26] + ALPHABET[value%26]
return c
def decrypt(c,k):
a = k[0]
b = k[1]
ainv = modular_inverse(a,26*26)
p = ""
for i in range(len(c)//2):
value = ALPHABET.index(c[2*i])*26 + ALPHABET.index(c[2*i+1])
value = (ainv*(value - b))%(26*26)
p += ALPHABET[value//26] + ALPHABET[value%26]
return p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment