home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Seeker / LaurentC.txt < prev    next >
Text File  |  2000-05-25  |  9KB  |  197 lines

  1. Hi Seeker,
  2.  
  3.       .... 
  4.  
  5.       for mostly the same reason as you did, (some material for our part 2, trashing some notes, finishing something worth to, ...) I
  6.       spend a few times cleaning, commenting, improving, one of the program i wrote to check every possible position.
  7.  
  8.       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
  9.       it's worth, i'll try to write something.
  10.  
  11.       Anyway, here is the source code :
  12.  
  13.       --------------------------------------------
  14.  
  15.       /*
  16.       Playfair breaking tool. Allow to slide a know crib over a given cipher
  17.       text in order to find possible position for the crib.
  18.       Rules used to reject a given position are as follow :
  19.         - Rule 1 : a plaintext letter can't encode as the same letter in the
  20.                                ciphertext.
  21.         - Rule 2 : A digram can't be made of twice the same letter
  22.         - Rule 3 : For a given letter, there can only be 8 letters in the
  23.                    same row/col. Those letters are the one found in the
  24.                                cipher (plain) digrams corresponding to a plain (cipher)
  25.                                digram containing that given letter.
  26.         - Rule 4 : A given letter can only encode in 5 other letters. (the
  27.                                4 ones in the same row and the one below).
  28.       The 8 letters found in rule 3 are what I call the SetOf8. The 5 letters
  29.       found in rule 4 are called the SetOf5.
  30.       In order to reduce number of possible solution, the SetOf8 and the 
  31.       SetOf5 can be initialized. Usefull if you got some info through other
  32.       cribs.
  33.       Starting position and step increment are parameters. This allow you,
  34.       for example, to scan only for odd (or even) position.
  35.       Given -s 1 -i 2 (starting at 1 and stepping by 2), allow you to check 
  36.       only for odd position.
  37.       Usage :
  38.         crib filename cribstring letter [-s d][-i d][-8 s][-5 s]
  39.         Where:
  40.         - filename is the name of the cipher file.
  41.         - cribstring is the crib you want to slide.
  42.         - letter is the letter for which you want to check rule 3 & 4
  43.         - d is an integer
  44.         - s is a string
  45.         Optional :
  46.         -s allow you to specify a starting position different than 0
  47.         -i allow you to step with an increment different than 1
  48.         -8 allow you to specify an initial set of 8
  49.         -5 allow you to specify an initial set of 5
  50.       Example :
  51.         crib cipher.txt BEWAREICEWEASELS E -8 UF -5 U
  52.         will scan cipher.txt with crib 'BEWAREICEWEASELS' and build setof8
  53.         and setof5 for the letter E. Setof8 is initialed to 'UF' and setof5
  54.         is initialed to 'U'.
  55.       Limitations : 
  56.        - No check is done on 'repeating patterns'.
  57.        - Some letters may be lost on odd position or if your crib's length is odd.
  58.        - crib max length is 128 char.
  59.        - cipher text max length is 2048 char.
  60.        - cipher text file can't contain 'white' space (space, CR/LR, tab..)
  61.        - Works only with 1 crib and 1 given letter.
  62.        - No check is done for repeating letter in the crib, ie, no X inserted
  63.          use an even and a odd pass in this case.
  64.       Written by Laurent, November 99, for public domain. 
  65.       No warranty expressed
  66.       Please include this header. 
  67.       comments, corrections, improvements can be send to laurent30@hotmail.com
  68.       Thanks.
  69.       */
  70.       #include "stdio.h"
  71.       #include "stdlib.h"
  72.       #include "string.h"
  73.       char cipher[2048];              //cipher text
  74.       char crib[128];                 //crib text
  75.       char letter;                    //letter for which setof8 and setof5 will be checked
  76.       int nb_sol=0;
  77.       void Usage(char *progname) 
  78.       {
  79.          fprintf(stderr,"Usage :\n\n%s filename cribstring letter [-s d][-i d][-8 s][-5 s]\n",
  80.                  progname);
  81.          fprintf(stderr,"Where:\n");
  82.          fprintf(stderr,"- filename is the name of the cipher file.\n");
  83.          fprintf(stderr,"- cribstring is the crib you want to slide.\n");
  84.          fprintf(stderr,"- letter is the letter for which you want to check rule 3 & 4\n");
  85.          fprintf(stderr,"- d is an integer\n");
  86.          fprintf(stderr,"- s is a string\n");
  87.          fprintf(stderr,"\n  Optional :\n");
  88.          fprintf(stderr,"\t-s allow you to specify a starting position different than 0\n");
  89.          fprintf(stderr,"\t-i allow you to step with an increment different than 1\n");
  90.          fprintf(stderr,"\t-8 allow you to specify an initial set of 8\n");
  91.          fprintf(stderr,"\t-5 allow you to specify an initial set of 5\n");
  92.          fprintf(stderr,"\n Example : \n");
  93.          fprintf(stderr,"\n%s cipher.txt BEWAREICEWEASELS E -8 UF -5 U\n", progname);
  94.          exit(1);
  95.       }
  96.       //add the char c to the set of char set
  97.       void add2set(char* set, char c)
  98.       {
  99.          int count=0;
  100.          //loop until we found c in the set or until we reach end of the set
  101.          while ((set[count]!=0) && (set[count]!=c)) {count++;}
  102.          //if c was not found, add it
  103.          if ((set[count]==0)){set[count] = c; count++; set[count]=0;}
  104.       }
  105.       //This will check for a same letter in the cipher and plaintext at
  106.       //the same position.
  107.       bool rule1(char* cipher, const char* plain)
  108.       {
  109.          int j;
  110.          for(j=0; j<strlen(plain); j++)
  111.          {
  112.              if (plain[j] == cipher[j]) return false; //same letter -> not good
  113.          }
  114.          return true;
  115.       }
  116.       //a digram can't be made of twice the same letter
  117.       bool rule2(char* cipher, bool odd)
  118.       {
  119.          int j;
  120.          if (odd) j=1; else j=0;  // start on the digram boundary
  121.          for (; j<strlen(cipher); j+=2)
  122.          {
  123.              if (cipher[j] == cipher[j+1]) return false;
  124.          }
  125.          return true;
  126.       }
  127.       //return the number of letters in the 'setof8' for the given letter
  128.       //setof8 will contain the letters
  129.       //odd indicate weither the text start on a even or odd boundary
  130.       int rule3(char* setof8, char letter, char* cipher,const char* plain, bool odd)
  131.       {
  132.          int i;
  133.          if (odd) 
  134.          {
  135.              cipher=&cipher[1];
  136.              plain=&plain[1];
  137.          }
  138.          for (i=0; i<strlen(plain);i++)
  139.          {
  140.              if (plain[i] == letter)
  141.              {
  142.                  int j;
  143.                  j=i-(i%2);              //j point to the first letter of the digram
  144.                  if (cipher[j]!=letter) add2set(setof8, cipher[j]);
  145.                  j++;
  146.                  if (cipher[j]!=letter) add2set(setof8, cipher[j]);
  147.              }
  148.              if (cipher[i] ==letter)
  149.              {
  150.                  int j;
  151.                  j=i-(i%2);
  152.                  if (plain[j]!=letter) add2set(setof8, plain[j]);
  153.                  j++;
  154.                  if (plain[j]!=letter) add2set(setof8, plain[j]);
  155.              }
  156.          }
  157.          return strlen(setof8);
  158.       }
  159.       int rule4(char* setof5, char letter, char* cipher, const char* plain)
  160.       {
  161.          int i;
  162.          for (i=0; i<strlen(plain); i++)
  163.          {
  164.              if (plain[i] == letter)
  165.              {
  166.                  add2set(setof5, cipher[i]);
  167.              }
  168.          }
  169.          return strlen(setof5);
  170.       }
  171.       void scan(int startpos, int increment, char* set8, char* set5)
  172.       {
  173.          char tempcipher[128];
  174.          char Set8[24];  //temporary set of 8 (max 24 different letters)
  175.          char Set5[24];
  176.          int t8, t5;             //number of letter is setof8 and setof5
  177.          int pos;
  178.        
  179.          for (pos=startpos;pos<strlen(cipher)-strlen(crib); pos+=increment)
  180.          {
  181.              strcpy(Set8, set8);
  182.              strcpy(Set5, set5);
  183.              strncpy(tempcipher, &cipher[pos],strlen(crib));
  184.              tempcipher[strlen(crib)]=0;
  185.              if (rule1(tempcipher, crib))
  186.              {
  187.                  if (rule2(tempcipher, ((pos%2)==1)))
  188.                  {
  189.                      if ((t8=rule3(Set8, letter, tempcipher, crib, ((pos%2)==1))) < 9)
  190.                      {
  191.                          if ((t5=rule4(Set5, letter, tempcipher, crib))<6)
  192.                          {
  193.                              printf("%03d - %s\n",pos, tempcipher);           
  194.                              printf("      %s\n",crib);
  195.                              printf("%d - %8s - Set of 8\n", strlen(Set8), Set8);
  196.                              printf("%d - %8s - Set of 5\n", strlen(Set5), Set5);
  197.