Created
April 10, 2020 18:33
-
-
Save themaddoctor/f5c91936811d1788a138482242bda3e8 to your computer and use it in GitHub Desktop.
double affine encryptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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