/* Decrypt Vigenere cipher */ load('stringproc)$ /* cipher key to numbers */ kText : string_to_octets("Christmas")$ /* ciphertext to numbers */ cTextRaw : string_to_octets("of eiex us")$ /* get system value for " " character */ spaceCharCode : string_to_octets(space)[1]$ /* find the spaces */ wordBreaks : sublist_indices(cTextRaw, lambda([k], k = spaceCharCode))$ /* drop all spaces */ cTextCook : delete(spaceCharCode, cTextRaw)$ /* repeat cipher key so it is longer than ciphertext */ kTextPad : flatten(makelist(kText, floor(length(cTextCook)/length(kText))+1))$ /* make same length as ciphertext for list addition */ kTextPad : firstn(kTextPad, length(cTextCook))$ /* convert cipher key into offsets to ASCII alphabet */ kTextPadOffset : makelist(z*-1, z, makelist(if (y>=97 and y<=122) then y-97 else y, y, makelist(if (x>=65 and x<=90) then x-65 else x, x, kTextPad)))$ /* convert ciphertext into offsets to ASCII alphabet */ cTextCookOffset : makelist(if (y>=97 and y<=122) then y-97 else y, y, makelist(if (x>=65 and x<=90) then x-65 else x, x, cTextCook))$ /* add offsets and perform modulo arithmetic */ alphaStart : string_to_octets("a")[1]$ pText : makelist(alphaStart + remainder(k + 26, 26), k ,cTextCookOffset + kTextPadOffset)$ /* insert word-breaks */ pString : octets_to_string(pText)$ for k : 1 thru length(wordBreaks) do pString : sinsert(" ", pString, wordBreaks[k])$ /* plain text */ pString;