Miller Sum Letter Cipher

This code allows us to input a text and encode or decode it with the Miller Sum cipher, which adds twice the sum of all the letters up to and including our current letter for the encoding. For decoding, we keep track of the sum of the decoded message up to where we are, subtract twice that, and then take the new letter and multiply by the inverse of 3 modulo 26 (ie, 9).

shifter[input_,shift_] := Module[{},
temp = Mod[(ToCharacterCode[input][[1]] - 65) + shift,26]+65;
temp = FromCharacterCode[temp];
Return[temp];
];

addletters[input1_,input2_,addsubtract_] := Module[{},
temp = Mod[(ToCharacterCode[input1][[1]] - 65) + addsubtract(ToCharacterCode[input2][[1]]-65),26]+65;
temp = FromCharacterCode[temp];
Return[temp];
];

addletters["E","G",1]
addletters["I","C",-1]
Mod[ToCharacterCode["A"][[1]]-65,26]
(*bob = "D"
addletters["B",bob]*)

tempdata

(* Last argument: 1 to encode, anything else to decode *)
millersumlettercrypt[data_,codeordecode_] := Module[{},
If[codeordecode == 1, sign = 1, sign = -1];
tempdata = Characters[data];
(*tempcodeword = Characters[codeword];*)
lettersum = {};

codeddata = {};
currsumletter = "A";

If[sign == 1,
{
For[n = 1, n ≤ Length[tempdata], n++, 
{
currsumletter = addletters[tempdata[[n]],currsumletter,sign];
currsumletter = addletters[tempdata[[n]],currsumletter,sign];
lettersum = AppendTo[lettersum, currsumletter];

newletter = addletters[tempdata[[n]],currsumletter,sign];
codeddata = AppendTo[codeddata, newletter] ;
} ];
Print["The Input text: ", data];
(*Print["Auxiliary text: ", lettersum];*)
Print["Encrypted text: ",StringJoin[codeddata]];
encrypted = StringJoin[codeddata];
(* save result in encrypted to facilitate running decoder *)
}];

If[sign ==  -1,
{
runningsum = 0;
For[n = 1, n ≤ Length[tempdata], n++, 
{
currlettervalue = ToCharacterCode[tempdata[[n]]][[1]]-65;
currlettervalue = Mod[(currlettervalue - 2*runningsum)*9,26]+65;
newletter = FromCharacterCode[currlettervalue];
codeddata = AppendTo[codeddata, newletter];
runningsum = Mod[runningsum +ToCharacterCode[newletter][[1]]-65,26];
(*Print["currlettervalue = ", currlettervalue];
Print["running sum = ", runningsum];
Print[" "];*)
(*currletter = FromCharacterCode[Mod[(ToCharacterCode[currletter]-65)*9,26]+65];
Print[currletter];*)
}];
Print["Encrypted text: ", data];
Print["Decrypted text: ",StringJoin[codeddata]];
}];
];

millersumlettercrypt["HELLOCRYPTOCLASSIHOPETHOSEWHOARELOOKINGATTHISAREHAVINGFUNTHEREAREESSENTIALLYALIMITLESSNUMBEROFCODESONECANCREATEALONGTHESELINESCONGRATSONDECIPHERINGTHEPEARLSOFWISDOMFORTODAYFIRSTQUOTEFROMGLORYROADBYHEINLEINISDEMOCRACYCANTWORKMATHEMATICIANSPEASANTSANDANIMALSTHATSALLTHEREISSODEMOCRACYATHEORYBASEDONTHEASSUMPTIONTHATMATHEMATICIANSANDPEASANTSAREEQUALCANNEVERWORKWISDOMISNOTADDITIVEITSMAXIMUMISTHATOFTHEWISESTMANINAGIVENGROUPSECONDQUOTEFROMFOUNDATIONBYASIMOVITPAYSTOBEOBVIOUSESPECIALLYIFYOUHAVEAREPUTATIONFORSUBTLETY",1];
Print[" "];
millersumlettercrypt[encrypted,-1];

(* THEMUSICALMANOFLAMANCHASANCHOSINGSWHETHERTHESTONEHITSTHEPITCHERORTHEPITCHERHITSTHESTONEITSGOINGTOBEBADFORTHEPITCHER *)

Mod[2(23 +  1),26]
FromCharacterCode[19+65]
FromCharacterCode[22+65]
Mod[3 17 17 3,26]
Mod[3 17 17, 26]

millersumlettercrypt["HELLOCRYPTOCLASSIVECREATEDANDWRITTENTHISCODEWHILEFLYINGBACKTOTHESTATESONNORTHWESTFROMAMSTERDAMTODETROITIHOPEYOUALLHADAFUNANDPRODUCTIVEWEEKIHADANICESTAYINENGLANDTHETAXIDRIVERWHOTOOKMETOTHEBRISTOLAIRPORTHADAFAMILYMEMBERINVOLVEDINTHENORMANDYLANDINGSANYWAYIMABOUTTOGETACOPIOUSSNACKSOINEEDTOPUTMYLAPTOPAWAYFIFTYBONUSPOINTSTOANYONEWHOEMAILSMEBOOLABOOLABOOLABOOLABOOLABOOLABOOLABOOLAWHENWEROUGHUPPOOROLDHARVARDTHEYWILLHOLLERBOOLABOOIVENOWLANDEDSUCCESSFULLYINDETROITANDAMWAITINGFORMYCONNECTIONTOALBANYILLADDABITMORETEXTINCASEYOUTRYUSINGAFREQUENCYANALYSISILLENDWITHTHEFIRSTTWOLINESOFONEOFMYFAVORITEPOEMSNAMELYITWASINTRUTHANEAGERYOUTHWHOHALTEDMEONEDAYHEGAZEDINBLISSATMEANDTHISISWHATHEHADTOSAY","E",1]
millersumlettercrypt["LPALZBSQFYMOZZRJRMQSJNNGKNNADZQYRKOBUBJBDRUYUBJUYDOMUHNOOQATHAHLDWWPTLZMZNEXEAEWPULZLLXPIMDGGSLZCGZQEMFNUIXBZNHHSDKKNNSMZZMPEVJMGIBJEIEIMWELLOOBJLPHAAYGTXKQBBORKRVOOLTWNVQULHOCVJXHTXQEXEIJAIATHSSARGULELLOOTTFNYWIMYZDUCPKYJEILTGZGKXLCOOBECNNADLYEWWJHDDBJVVWKEXQEKOHHJXMUICUMZZBLDRZMQUXQETNGSQBBQJXMMIIGLTYRPQERLDSGOBUMFTTGESFJFMAEQQYJBNRSGUFFGUITTUIWHHIWKVVWKYJJKYMXXYMALLMAOZZVCGTPTKYSYFZODRFWKVYFFWRRILELPNJRCNUITEIZAOCNNOCQYTXKYUFFSVZCUOQSWOGLFQBZHUXBULZHAANQQCYYGZHUAFTKWUWKXKOQJRFSLZZKLLYWEPAADGGHPIUIZDWAXQYLNNFJHVPIZXRJREKKPGKAUYLNLLYYJHZHZHSDHUXTBUBUBFKSJBUNJXIQDHZNSGTXLQCAFFAOFNGKZNRDVIIUYJHPIEEWERKBVOVVIMMSWNLZTMTPWKRRCVZCOSGTXAAYFJPPOSVDQRCKCUUNZDDQTMTBTBTPWWPWAHHKDRJJH","E",-1]