home *** CD-ROM | disk | FTP | other *** search
- Hi Seeker,
-
- ....
-
- for mostly the same reason as you did, (some material for our part 2, trashing some notes, finishing something worth to, ...) I
- spend a few times cleaning, commenting, improving, one of the program i wrote to check every possible position.
-
- So i post it here inside, in case someone wants to gather all material for part 2. If nobody have time to do it and if everybody think
- it's worth, i'll try to write something.
-
- Anyway, here is the source code :
-
- --------------------------------------------
-
- /*
- Playfair breaking tool. Allow to slide a know crib over a given cipher
- text in order to find possible position for the crib.
- Rules used to reject a given position are as follow :
- - Rule 1 : a plaintext letter can't encode as the same letter in the
- ciphertext.
- - Rule 2 : A digram can't be made of twice the same letter
- - Rule 3 : For a given letter, there can only be 8 letters in the
- same row/col. Those letters are the one found in the
- cipher (plain) digrams corresponding to a plain (cipher)
- digram containing that given letter.
- - Rule 4 : A given letter can only encode in 5 other letters. (the
- 4 ones in the same row and the one below).
- The 8 letters found in rule 3 are what I call the SetOf8. The 5 letters
- found in rule 4 are called the SetOf5.
- In order to reduce number of possible solution, the SetOf8 and the
- SetOf5 can be initialized. Usefull if you got some info through other
- cribs.
- Starting position and step increment are parameters. This allow you,
- for example, to scan only for odd (or even) position.
- Given -s 1 -i 2 (starting at 1 and stepping by 2), allow you to check
- only for odd position.
- Usage :
- crib filename cribstring letter [-s d][-i d][-8 s][-5 s]
- Where:
- - filename is the name of the cipher file.
- - cribstring is the crib you want to slide.
- - letter is the letter for which you want to check rule 3 & 4
- - d is an integer
- - s is a string
- Optional :
- -s allow you to specify a starting position different than 0
- -i allow you to step with an increment different than 1
- -8 allow you to specify an initial set of 8
- -5 allow you to specify an initial set of 5
- Example :
- crib cipher.txt BEWAREICEWEASELS E -8 UF -5 U
- will scan cipher.txt with crib 'BEWAREICEWEASELS' and build setof8
- and setof5 for the letter E. Setof8 is initialed to 'UF' and setof5
- is initialed to 'U'.
- Limitations :
- - No check is done on 'repeating patterns'.
- - Some letters may be lost on odd position or if your crib's length is odd.
- - crib max length is 128 char.
- - cipher text max length is 2048 char.
- - cipher text file can't contain 'white' space (space, CR/LR, tab..)
- - Works only with 1 crib and 1 given letter.
- - No check is done for repeating letter in the crib, ie, no X inserted
- use an even and a odd pass in this case.
- Written by Laurent, November 99, for public domain.
- No warranty expressed
- Please include this header.
- comments, corrections, improvements can be send to laurent30@hotmail.com
- Thanks.
- */
- #include "stdio.h"
- #include "stdlib.h"
- #include "string.h"
- char cipher[2048]; //cipher text
- char crib[128]; //crib text
- char letter; //letter for which setof8 and setof5 will be checked
- int nb_sol=0;
- void Usage(char *progname)
- {
- fprintf(stderr,"Usage :\n\n%s filename cribstring letter [-s d][-i d][-8 s][-5 s]\n",
- progname);
- fprintf(stderr,"Where:\n");
- fprintf(stderr,"- filename is the name of the cipher file.\n");
- fprintf(stderr,"- cribstring is the crib you want to slide.\n");
- fprintf(stderr,"- letter is the letter for which you want to check rule 3 & 4\n");
- fprintf(stderr,"- d is an integer\n");
- fprintf(stderr,"- s is a string\n");
- fprintf(stderr,"\n Optional :\n");
- fprintf(stderr,"\t-s allow you to specify a starting position different than 0\n");
- fprintf(stderr,"\t-i allow you to step with an increment different than 1\n");
- fprintf(stderr,"\t-8 allow you to specify an initial set of 8\n");
- fprintf(stderr,"\t-5 allow you to specify an initial set of 5\n");
- fprintf(stderr,"\n Example : \n");
- fprintf(stderr,"\n%s cipher.txt BEWAREICEWEASELS E -8 UF -5 U\n", progname);
- exit(1);
- }
- //add the char c to the set of char set
- void add2set(char* set, char c)
- {
- int count=0;
- //loop until we found c in the set or until we reach end of the set
- while ((set[count]!=0) && (set[count]!=c)) {count++;}
- //if c was not found, add it
- if ((set[count]==0)){set[count] = c; count++; set[count]=0;}
- }
- //This will check for a same letter in the cipher and plaintext at
- //the same position.
- bool rule1(char* cipher, const char* plain)
- {
- int j;
- for(j=0; j<strlen(plain); j++)
- {
- if (plain[j] == cipher[j]) return false; //same letter -> not good
- }
- return true;
- }
- //a digram can't be made of twice the same letter
- bool rule2(char* cipher, bool odd)
- {
- int j;
- if (odd) j=1; else j=0; // start on the digram boundary
- for (; j<strlen(cipher); j+=2)
- {
- if (cipher[j] == cipher[j+1]) return false;
- }
- return true;
- }
- //return the number of letters in the 'setof8' for the given letter
- //setof8 will contain the letters
- //odd indicate weither the text start on a even or odd boundary
- int rule3(char* setof8, char letter, char* cipher,const char* plain, bool odd)
- {
- int i;
- if (odd)
- {
- cipher=&cipher[1];
- plain=&plain[1];
- }
- for (i=0; i<strlen(plain);i++)
- {
- if (plain[i] == letter)
- {
- int j;
- j=i-(i%2); //j point to the first letter of the digram
- if (cipher[j]!=letter) add2set(setof8, cipher[j]);
- j++;
- if (cipher[j]!=letter) add2set(setof8, cipher[j]);
- }
- if (cipher[i] ==letter)
- {
- int j;
- j=i-(i%2);
- if (plain[j]!=letter) add2set(setof8, plain[j]);
- j++;
- if (plain[j]!=letter) add2set(setof8, plain[j]);
- }
- }
- return strlen(setof8);
- }
- int rule4(char* setof5, char letter, char* cipher, const char* plain)
- {
- int i;
- for (i=0; i<strlen(plain); i++)
- {
- if (plain[i] == letter)
- {
- add2set(setof5, cipher[i]);
- }
- }
- return strlen(setof5);
- }
- void scan(int startpos, int increment, char* set8, char* set5)
- {
- char tempcipher[128];
- char Set8[24]; //temporary set of 8 (max 24 different letters)
- char Set5[24];
- int t8, t5; //number of letter is setof8 and setof5
- int pos;
-
- for (pos=startpos;pos<strlen(cipher)-strlen(crib); pos+=increment)
- {
- strcpy(Set8, set8);
- strcpy(Set5, set5);
- strncpy(tempcipher, &cipher[pos],strlen(crib));
- tempcipher[strlen(crib)]=0;
- if (rule1(tempcipher, crib))
- {
- if (rule2(tempcipher, ((pos%2)==1)))
- {
- if ((t8=rule3(Set8, letter, tempcipher, crib, ((pos%2)==1))) < 9)
- {
- if ((t5=rule4(Set5, letter, tempcipher, crib))<6)
- {
- printf("%03d - %s\n",pos, tempcipher);
- printf(" %s\n",crib);
- printf("%d - %8s - Set of 8\n", strlen(Set8), Set8);
- printf("%d - %8s - Set of 5\n", strlen(Set5), Set5);
-