ciphertext="LBYKWGZHJGCXBXWQOOWVLZIUDLRORZUDSSLLUGCMGODAVRINYHWHGMFVRUKDANKWPGKKRGINQYPVKKVGCWVLZIUDLRWSWERSSTHTKOJVLZIUDLRWSWERSSAQKTOHHDLBASXJSVBJOWRSVWPYJYAJPVSXJSVBJOWRSVWPYJQSUSPOWFAWECCXUWVXTSRTGIPQZIVLZIUDLROLVVOVBXGMUZEVJZMJKHRNHZADLVFJJGPSEWXAYOLNVQWVRMAYEMHYVRMVEGGIEWDTNQSVFJLKBIPLWWJOEIWQEGGIEWDTNQSVFJLKBIPLWPGYXUWUECIMAKKWTDXUWSMTSSQODAUYJNJOQMOXUWSZGCIALSMTSSQLKIVCSZWRNKDWAGLAKOWGSXBJYVVLLMUSRFAVBGNSAAWADOMAYUMEOMIWGNQBKBGGWTPSEWYQNSRGZHAWZIEDDBKFIQWJZGOSSURURKVVKRVQXPL"

AIs=ToCharacterCode["A"][[1]]-1
aIs=ToCharacterCode["a"][[1]]-1

trigram[j_]:=Module[{j1,j2,j3,answer},
j1=Floor[j/26^2];
j2=Floor[(j-26^2j1)/26];
j3=(j-26^2j1-26 j2);
answer=StringJoin[FromCharacterCode[j1+AIs],FromCharacterCode[j2+AIs],FromCharacterCode[j3+AIs]];
answer
]

cipherTrigrams=Table[{trigram[j],StringCount[ciphertext,trigram[j]]},{j,0,26^3-1}];
cipherTrigramSorted=Sort[cipherTrigrams,#1[[2]]>#2[[2]]&];
cipherTrigramSorted//Transpose//MatrixForm

StringPosition[ciphertext,"XUW"]

313-180//FactorInteger

327-313//FactorInteger

348-327//FactorInteger

StringPosition[ciphertext,"UDL"]

87-24//FactorInteger

108-87//FactorInteger

199-108//FactorInteger

keyL=7;

shifts=ConstantArray[0,keyL]

cipherNumbs=ToCharacterCode[ciphertext]-AIs

cNumbs=Table[Table[cipherNumbs[[keyL j + k]],{j,0,(Length[cipherNumbs]-k)/keyL}],{k,1,keyL}];

cNumbs[[1]]

englishHisto=BarChart[engl={8.15,1.44,2.76,3.79,13.11,2.92,1.99,5.26,6.35,0.13,0.42,3.39,2.54,7.10,8,1.98,0.12,6.83,6.10,10.47,2.46,0.92,1.54,0.17,1.98,0.08}/100.]

sh=1;
englishHisto
Histogram[cNumbs[[sh]],{1},AxesOrigin->{1,0}]
cipherFreq=Table[{FromCharacterCode[j+AIs], Count[cNumbs[[sh]],j]},{j,1,26}];
cipherFreqSort=Sort[cipherFreq,#1[[2]]>#2[[2]]&];
cipherFreqSorted=Table[{cipherFreqSort[[j,1]],N[cipherFreqSort[[j,2]]/Length[cNumbs[[sh]]],2]},{j,1,26}];
cipherFreqSorted//Transpose//MatrixForm

shifts[[1]]=3;

sh=2;
englishHisto
Histogram[cNumbs[[sh]],{1},AxesOrigin->{1,0}]
cipherFreq=Table[{FromCharacterCode[j+AIs], Count[cNumbs[[sh]],j]},{j,1,26}];
cipherFreqSort=Sort[cipherFreq,#1[[2]]>#2[[2]]&];
cipherFreqSorted=Table[{cipherFreqSort[[j,1]],N[cipherFreqSort[[j,2]]/Length[cNumbs[[sh]]],2]},{j,1,26}];
cipherFreqSorted//Transpose//MatrixForm

shifts[[2]]=(ToCharacterCode["M"]-ToCharacterCode["E"])[[1]]

sh=3;
englishHisto
Histogram[cNumbs[[sh]],{1},AxesOrigin->{1,0}]
cipherFreq=Table[{FromCharacterCode[j+AIs], Count[cNumbs[[sh]],j]},{j,1,26}];
cipherFreqSort=Sort[cipherFreq,#1[[2]]>#2[[2]]&];
cipherFreqSorted=Table[{cipherFreqSort[[j,1]],N[cipherFreqSort[[j,2]]/Length[cNumbs[[sh]]],2]},{j,1,26}];
cipherFreqSorted//Transpose//MatrixForm

shifts[[3]]=(ToCharacterCode["G"]-ToCharacterCode["E"])[[1]]

sh=4;
englishHisto
Histogram[cNumbs[[sh]],{1},AxesOrigin->{1,0}]
cipherFreq=Table[{FromCharacterCode[j+AIs], Count[cNumbs[[sh]],j]},{j,1,26}];
cipherFreqSort=Sort[cipherFreq,#1[[2]]>#2[[2]]&];
cipherFreqSorted=Table[{cipherFreqSort[[j,1]],N[cipherFreqSort[[j,2]]/Length[cNumbs[[sh]]],2]},{j,1,26}];
cipherFreqSorted//Transpose//MatrixForm

shifts[[4]]=(ToCharacterCode["O"]-ToCharacterCode["E"])[[1]]

sh=5;
englishHisto
Histogram[cNumbs[[sh]],{1},AxesOrigin->{1,0}]
cipherFreq=Table[{FromCharacterCode[j+AIs], Count[cNumbs[[sh]],j]},{j,1,26}];
cipherFreqSort=Sort[cipherFreq,#1[[2]]>#2[[2]]&];
cipherFreqSorted=Table[{cipherFreqSort[[j,1]],N[cipherFreqSort[[j,2]]/Length[cNumbs[[sh]]],2]},{j,1,26}];
cipherFreqSorted//Transpose//MatrixForm

shifts[[5]]=(ToCharacterCode["E"]-ToCharacterCode["A"])[[1]]

sh=6;
englishHisto
Histogram[cNumbs[[sh]],{1},AxesOrigin->{1,0}]
cipherFreq=Table[{FromCharacterCode[j+AIs], Count[cNumbs[[sh]],j]},{j,1,26}];
cipherFreqSort=Sort[cipherFreq,#1[[2]]>#2[[2]]&];
cipherFreqSorted=Table[{cipherFreqSort[[j,1]],N[cipherFreqSort[[j,2]]/Length[cNumbs[[sh]]],2]},{j,1,26}];
cipherFreqSorted//Transpose//MatrixForm

shifts[[6]]=(ToCharacterCode["N"]-ToCharacterCode["A"])[[1]]

sh=7;
englishHisto
Histogram[cNumbs[[sh]],{1},AxesOrigin->{1,0}]
cipherFreq=Table[{FromCharacterCode[j+AIs], Count[cNumbs[[sh]],j]},{j,1,26}];
cipherFreqSort=Sort[cipherFreq,#1[[2]]>#2[[2]]&];
cipherFreqSorted=Table[{cipherFreqSort[[j,1]],N[cipherFreqSort[[j,2]]/Length[cNumbs[[sh]]],2]},{j,1,26}];
cipherFreqSorted//Transpose//MatrixForm

shifts[[7]]=(ToCharacterCode["S"]-ToCharacterCode["A"])[[1]]

Length/@cNumbs

AppendTo[cNumbs[[7]],0];

plainText=FromCharacterCode[Transpose[Mod[cNumbs-shifts-1,26]+1+aIs]]

plaintext=StringJoin[plainText]

key=FromCharacterCode[shifts+aIs+1]

sh=1;
englishHisto
Histogram[cNumbs[[sh]],{1},AxesOrigin->{1,0}]
cipherFreq=Table[{FromCharacterCode[j+AIs], Count[cNumbs[[sh]],j]},{j,1,26}];
cipherFreqSort=Sort[cipherFreq,#1[[2]]>#2[[2]]&];
cipherFreqSorted=Table[{cipherFreqSort[[j,1]],N[cipherFreqSort[[j,2]]/Length[cNumbs[[sh]]],2]},{j,1,26}];
cipherFreqSorted//Transpose//MatrixForm

freq=(Take[cipherFreq,All,{2}]//Flatten)/Length[cNumbs[[sh]]]//N

shift[seq_,amt_]:=Flatten[Append[Take[seq,-(Length[seq]-amt)],Take[seq,amt]]]

BarChart[shift[freq,0]]
BarChart[shift[freq,3]]
englishHisto

Table[{j,Total[shift[freq,j] engl]},{j,1,26}]//ListPlot

shifts[[1]]=3;

sh=2;
englishHisto
Histogram[cNumbs[[sh]],{1},AxesOrigin->{1,0}]
cipherFreq=Table[{FromCharacterCode[j+AIs], Count[cNumbs[[sh]],j]},{j,1,26}];
cipherFreqSort=Sort[cipherFreq,#1[[2]]>#2[[2]]&];
cipherFreqSorted=Table[{cipherFreqSort[[j,1]],N[cipherFreqSort[[j,2]]/Length[cNumbs[[sh]]],2]},{j,1,26}];
cipherFreqSorted//Transpose//MatrixForm

freq=(Take[cipherFreq,All,{2}]//Flatten)/Length[cNumbs[[sh]]]//N
Table[{j,Total[shift[freq,j] engl]},{j,1,26}]//ListPlot

shifts[[2]]=8

sh=3;
englishHisto
Histogram[cNumbs[[sh]],{1},AxesOrigin->{1,0}]
cipherFreq=Table[{FromCharacterCode[j+AIs], Count[cNumbs[[sh]],j]},{j,1,26}];
cipherFreqSort=Sort[cipherFreq,#1[[2]]>#2[[2]]&];
cipherFreqSorted=Table[{cipherFreqSort[[j,1]],N[cipherFreqSort[[j,2]]/Length[cNumbs[[sh]]],2]},{j,1,26}];
cipherFreqSorted//Transpose//MatrixForm

freq=(Take[cipherFreq,All,{2}]//Flatten)/Length[cNumbs[[sh]]]//N
Table[{j,Total[shift[freq,j] engl]},{j,1,26}]//ListPlot

shifts[[3]]=2

sh=4;
englishHisto
Histogram[cNumbs[[sh]],{1},AxesOrigin->{1,0}]
cipherFreq=Table[{FromCharacterCode[j+AIs], Count[cNumbs[[sh]],j]},{j,1,26}];
cipherFreqSort=Sort[cipherFreq,#1[[2]]>#2[[2]]&];
cipherFreqSorted=Table[{cipherFreqSort[[j,1]],N[cipherFreqSort[[j,2]]/Length[cNumbs[[sh]]],2]},{j,1,26}];
cipherFreqSorted//Transpose//MatrixForm

freq=(Take[cipherFreq,All,{2}]//Flatten)/Length[cNumbs[[sh]]]//N
Table[{j,Total[shift[freq,j] engl]},{j,1,26}]//ListPlot

shifts[[4]]=(ToCharacterCode["O"]-ToCharacterCode["E"])[[1]]

sh=5;
englishHisto
Histogram[cNumbs[[sh]],{1},AxesOrigin->{1,0}]
cipherFreq=Table[{FromCharacterCode[j+AIs], Count[cNumbs[[sh]],j]},{j,1,26}];
cipherFreqSort=Sort[cipherFreq,#1[[2]]>#2[[2]]&];
cipherFreqSorted=Table[{cipherFreqSort[[j,1]],N[cipherFreqSort[[j,2]]/Length[cNumbs[[sh]]],2]},{j,1,26}];
cipherFreqSorted//Transpose//MatrixForm

freq=(Take[cipherFreq,All,{2}]//Flatten)/Length[cNumbs[[sh]]]//N
Table[{j,Total[shift[freq,j] engl]},{j,1,26}]//ListPlot

shifts[[5]]=(ToCharacterCode["E"]-ToCharacterCode["A"])[[1]]

sh=6;
englishHisto
Histogram[cNumbs[[sh]],{1},AxesOrigin->{1,0}]
cipherFreq=Table[{FromCharacterCode[j+AIs], Count[cNumbs[[sh]],j]},{j,1,26}];
cipherFreqSort=Sort[cipherFreq,#1[[2]]>#2[[2]]&];
cipherFreqSorted=Table[{cipherFreqSort[[j,1]],N[cipherFreqSort[[j,2]]/Length[cNumbs[[sh]]],2]},{j,1,26}];
cipherFreqSorted//Transpose//MatrixForm

freq=(Take[cipherFreq,All,{2}]//Flatten)/Length[cNumbs[[sh]]]//N
Table[{j,Total[shift[freq,j] engl]},{j,1,26}]//ListPlot

shifts[[6]]=(ToCharacterCode["N"]-ToCharacterCode["A"])[[1]]

sh=7;
englishHisto
Histogram[cNumbs[[sh]],{1},AxesOrigin->{1,0}]
cipherFreq=Table[{FromCharacterCode[j+AIs], Count[cNumbs[[sh]],j]},{j,1,26}];
cipherFreqSort=Sort[cipherFreq,#1[[2]]>#2[[2]]&];
cipherFreqSorted=Table[{cipherFreqSort[[j,1]],N[cipherFreqSort[[j,2]]/Length[cNumbs[[sh]]],2]},{j,1,26}];
cipherFreqSorted//Transpose//MatrixForm

freq=(Take[cipherFreq,All,{2}]//Flatten)/Length[cNumbs[[sh]]]//N
Table[{j,Total[shift[freq,j] engl]},{j,1,26}]//ListPlot

shifts[[7]]=(ToCharacterCode["S"]-ToCharacterCode["A"])[[1]]

Length/@cNumbs

plainText=FromCharacterCode[Transpose[Mod[cNumbs-shifts-1,26]+1+aIs]]

plaintext=StringJoin[plainText]

key=FromCharacterCode[shifts+aIs+1]