This “freeze” also happens on my “Crypto Substitution” solver.
That code is small enough to include in full below.
(The Vigenere cypher code has many more lines due to animating the Vigenere table.)
If anyone is interested the code for tje “Crypto Substitution” is below.
There is no real Memory allocation, swap space etc.
But after running this code for a few minutes, and stoping the solver, the computer does the “freeze” for a minute or so.
const
Sinkov: array [1..26] of real =
( 4.404820427, 2.381601633, 3.528971492, 3.853451829, 4.972741108, 3.448928784, 2.901421594,
3.628185385, 4.402917994, 0.600056757, 1.190549784, 3.700478590, 3.313781883, 4.481117537,
4.225697658, 3.390024081, 1.358123484, 4.432930965, 4.218853897, 4.626061545, 3.363457054,
2.834519680, 2.852631430, 1.635755221, 3.067536006, 0.085157808
);
function TMainwin.GetFitness(var decstr:string):integer;
var lv:integer;
rval:real;
begin
// use sinkov stats for fitness
rval:= 0;
for lv:= 1 to length(decstr) do
begin
cyc:= ord(decstr[lv])-64;
if (cyc>=0)and(cyc<26) then rval:= rval+Sinkov[cyc];
end;
result:= round(rval*10);
end;
function RandKey:string;
var key:string;
tchr:char;
num,lop,cyc:integer;
begin
key:= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
// randomly swap 100 key chars to get random substitution key
for num:= 1 to 100 do
begin
lop:= random(26)+1;
cyc:= random(26)+1; while (cyc=lop) do cyc:= random(26)+1;
tchr:= key[lop]; key[lop]:= key[cyc]; key[cyc]:= tchr;
end;
result:= key;
end;
procedure TMainwin.ButGoclick(sender: Tobject);
var tmps,encstr,bestkey,keystr,newkey,decstr:string;
lop,cyc,num1,num2,bestscore,trialscore,score:integer;
tchr:char;
begin
lboxresult.items.clear;
encstr:= UpperCase(EncTextBox.text);
lboxresult.items.add('Encrypted string: '+encstr);
lboxresult.items.add(' ');
stopit:= false; // global variable
bestkey:= '';
bestscore:= -100;
repeat
keystr:= RandKey;
trialscore:= -100;
lop:= 1;
while (lop<1000)and(not(stopit)) do
begin
// alter key by swapping 2 random letters
newkey:= keystr;
num1:= random(26)+1;
num2:= random(26)+1; while (num2=num1) do num2:= random(26)+1;
tchr:= newkey[num1]; newkey[num1]:= newkey[num2]; newkey[num2]:= tchr;
// decrypt
decstr:= '';
for cyc:= 1 to length(encstr) do
begin
num1:= ord(encstr[cyc])-ord('A')+1;
decstr:= decstr+newkey[num1];
end;
// fitness
score:= GetFitness(decstr);
if (score>trialscore) then
begin
trialscore:= score;
keystr:= newkey;
{if (score>=bestscore) then} lboxresult.items.add(format('%6d %s - %s',[trialscore,decstr,keystr]));
lboxresult.topindex:= lboxresult.items.count-1;
lop:= 1;
Application.Processmessages;
end;
inc(lop);
end;
if (trialscore>bestscore) then
begin
bestscore:= trialscore;
bestkey:= keystr;
Editbest.text:= format('%6d %s - %s',[bestscore,decstr,bestkey]);
end;
Application.Processmessages;
until (stopit);
end;