// Playfair solving by Simulated Annealing, written by ANCHISES. // This version loads ciphertext from a file cipher.txt that must be present // in the same folder as the program. // This version will run until you stop it. #include #include #include #include char codex[3000],code[2000],plain[2000],ian[500]; char alphabet[30],alpha[30],primus; char base[30],ma[30],mot[1000]; int plainex[2000]; int r[30]; int len,flag,buff,len_mot; int run,timer; int score,parentscore,bestscore; short a[26][26][26][26]; float e=2.718; float ratio,ds,temp,prob,limit; trans[4][25] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24, // norm 0,5,10,15,20,1,6,11,16,21,2,7,12,17,22,3,8,13,18,23,4,9,14,19,24,//NW-SE 20,21,22,23,24,15,16,17,18,19,10,11,12,13,14,5,6,7,8,9,0,1,2,3,4,//top-bot 4,3,2,1,0,9,8,7,6,5,14,13,12,11,10,19,18,17,16,15,24,23,22,21,20 //l to r }; FILE *InFile; void tetragraphs(void); void randomise_alpha(void); void test_transforms(void); void decrypt(void); void get_score(void); int main() { int j,k,m,p,q,t,x,y,z; int start1,start2,store[5]; strcpy(alphabet,"ABCDEFGHIKLMNOPQRSTUVWXYZ"); tetragraphs(); // load log tetragraphs p=-1; ifstream infile ("cipher.txt"); if (!infile) { cout<<"No file!"; getch(); return 0; } while(!infile.eof()) { infile.getline(mot,sizeof(mot)); len_mot=strlen(mot); for(m=0;m32) { t++;code[t]=codex[j]; } len=t+1; cout<<"cipher= "; for(j=0;j4;temp=temp-1) { timer=0; do{ //...make the child... x=random(50)+1; // integer from 1 to 51 switch(x) { case 1: for(m=0;m<25;m++) ma[m]=base[trans[x][m]]; break; case 2: for(m=0;m<25;m++) ma[m]=base[trans[x][m]]; break; case 3: for(m=0;m<25;m++) ma[m]=base[trans[x][m]]; break; case 4: for(m=0;m<25;m++) ma[m]=base[m]; // swap 2 rows start1=random(5)*5; start2=random(5)*5; for(m=0;m<5;m++) store[m]=ma[start1+m]; for(m=0;m<5;m++) ma[start1+m]=ma[start2+m]; for(m=0;m<5;m++) ma[start2+m]=store[m]; break; case 5: for(m=0;m<25;m++) ma[m]=base[m]; //swap 2 columns start1=random(5); start2=random(5); for(m=0;m<5;m++) store[m]=ma[start1+m*5]; for(m=0;m<5;m++) ma[start1+m*5]=ma[start2+m*5]; for(m=0;m<5;m++) ma[start2+m*5]=store[m]; break; default: for(m=0;m<25;m++) ma[m]=base[m]; p=random(25);q=random(25); buff=ma[p];ma[p]=ma[q];ma[q]=buff; } //...decrypt & score the child... decrypt(); get_score(); ds=score-parentscore; //cout<0) flag=1; //child is better than parent else // ds is negative, so child worse than parent { ratio=ds/temp; prob=pow(e,ratio); limit=1+random(1000);limit=limit/1000; if(prob>limit) flag=1; // child passes test } if(flag==1) //child > parent, or is worse { // but passes test for(m=0;m<25;m++) base[m]=ma[m]; // new parent parentscore=score; } timer++; }while(timer<50000); //drop temp after 50000 } // end for temp } while(run>0); cout<bestscore) { bestscore=score; cout<<"bestscore="<