(* Decrypt Vignere cipher *) (* cipher key to numbers *) kText = ToCharacterCode("Christmas"); (* ciphertext to numbers *) cTextRaw = ToCharacterCode("of eiex us"); (* get system value for " " symbol *) spaceCharCode = Part(ToCharacterCode(" "),-1); (* find the spaces *) wordBreaks = Flatten(Position(cTextRaw,spaceCharCode)); (* drop all spaces *) cTextCook = Cases(cTextRaw, x_ :> x/; x!= spaceCharCode); (* repeat cipher key so it is longer than ciphertext *) kTextPad = Flatten(Table(kText, IntegerPart(Length(cTextCook)/Length(kText))+1)); (* make same length as ciphertext for list addition *) kTextPad = Part(kTextPad, 1;; Length(cTextCook)); (* convert cipher key into offsets to ASCII alphabet *) kTextPadOffset = Times(-1,Map(Function(x, Which(x>=65 && x <= 90, x - 65, x>=97 && x <= 122, x - 97, true,0)), kTextPad)); (* convert ciphertext into offsets to ASCII alphabet *) cTextCookOffset = Map(Function(x, Which(x>=65 && x <= 90, x - 65, x>=97 && x <= 122, x - 97, true,0)), cTextCook); (* add offsets and perform modulo arithmetic *) alphaStart = Part(ToCharacterCode("a"), -1); pText = Replace(cTextCookOffset + kTextPadOffset, x_ :> alphaStart + Mod(x + 26, 26)); (* insert word-breaks *) pString = FromCharacterCode(pText); Do( pString = StringInsert(pString," ", k), {k, wordBreaks}); (* plain text *) pString