Try CoCalc Published Files Email HelpDocumentationSign In
Path: Vernam.sagews
License: none
Image: ubuntu2004
Project: Math307Attacks
Edit | Raw | Embed | Download
def VigenereEnc(PlainText,Key): PlainText = PlainText.upper() Key = Key.upper() outstr = "" for i in xrange(len(PlainText)): plainc = PlainText[i] keyc = Key[i%(len(Key))] plainnum = ord(plainc)-65 keynum = ord(keyc)-65 outstr += chr((plainnum+keynum)%26+65) return outstr def VigenereDec(CipherText,Key): CipherText = CipherText.upper() Key = Key.upper() outstr = "" for i in xrange(len(CipherText)): cipherc = CipherText[i] keyc = Key[i%(len(Key))] ciphernum = ord(cipherc)-65 keynum = ord(keyc)-65 outstr += chr((ciphernum-keynum)%26+65) return outstr def VernamKeyGen(PlainText): PlainText=PlainText.upper() PlainText.replace (" ", "") Key = "" for i in xrange(len(PlainText)): #print randrange(0,26)+65 Key+= chr(randrange(0,26)+65) return Key def VernamEnc(PlainText,Key=""): PlainText=PlainText.upper() if Key=="": Key=VernamKeyGen(PlainText) if (len(PlainText)<len(Key)): return "Key not long enough run VernamKeyGen with Plaintext to get key" CipherText = VigenereEnc(PlainText,Key) return [CipherText,Key] def VernamDec(CipherText,Key): CipherText=CipherText.upper() load("Vigenere.sage") if (len(CipherText)<len(Key)): return "Key not long enough run VernamKeyGen with Plaintext to get key" PlainText = VigenereDec(CipherText,Key) return PlainText
#VigenereEnc("test","test") output =VernamEnc("test") print output
['CKFD', 'JGNK']
VigenereEnc("test","abcd")
'TFUW'