#This code provides methods to encrypt and decrypt text using a cipher #based on Z_40, the group of integers under addition modulo 40. Below #Z_40 is represented as a sequence of letters. # #David Faden #dfaden@iastate.edu #February 16, 2004 #May 23, 2004 Z_40 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn" encoder = {} for i in range(0, 27): encoder[chr(ord('A') + i)] = Z_40[(3 * i) % 40] + Z_40[(3 * i + 1) % 40] + Z_40[(3 * i + 2) % 40] decoder = dict(map(lambda (k, v): (v, k), encoder.items())) def encrypt(text): text = text.upper() text = filter(lambda c: 'A' <= c <= 'Z', text) return "".join(map(lambda c:encoder[c], text)) def decrypt(text): plain_text = "" for i in range(0, len(text)/3): plain_text += decoder[text[3*i : 3*i + 3]] return plain_text if __name__ == '__main__': print encrypt("THISTEXTISPLAINAGAIN") print decrypt(encrypt("THISTEXTISPLAINAGAIN"))