#autokey cipher #The idea is to shift the plaintext using the plaintext itself as a key, #but to prepend a keyword to the plaintext before encryption. So in a way #it resembles a one-time pad. #The true onetime pad, with len(plain)<=len(key) import vigenere def onetime(plain,key): cipher='' length=len(plain) for j in range(length): textletter=ord(plain[j])-ord('a') keyletter=ord(key[j])-ord('a') shiftvalue=(textletter+keyletter)%26 cipherletter= chr(ord('a')+shiftvalue) cipher +=cipherletter return cipher def autokey_encrypt(plain,key): return onetime(plain,key+plain) def autokey_decrypt(cipher,key): plain='' for j in range(len(key)): plain+=chr(ord('a')+(ord(cipher[j])-ord(key[j]))%26) for j in range(len(key),len(cipher)): plain+=chr(ord('a')+(ord(cipher[j])-ord(plain[j-len(key)]))%26) return plain