<mosaic.cnfolio.com>
PROGRAM
  1. #include <stdio.h>
  2. /* Psuedo Code
  3.    Set counter to zero
  4.    WHILE Counter is less than 400
  5.          Input each sperate character into a list
  6.          Add one to the counter
  7.    END WHILE
  8.    Set counter to zero
  9.    WHILE Counter is less than 6
  10.          IF the counter place in the list equals a new line
  11.        THEN
  12.                Set countertwo to counter minus one
  13.                multiplier equal one
  14.                WHILE countertwo is greater than or equal to zero
  15.                      take the character at countertwo place in the list
  16.                      multiply by multipler
  17.                      multiplier is multiplied by 10
  18.                      add to the cipherkeylength
  19.                      Minus one from countertwo
  20.                escape the while loop
  21.          END IF
  22.    END WHILE
  23.    set counter to the number in the list after the newline
  24.    sizeofcipherkey equals zero
  25.    WHILE counter is less than 30
  26.          IF counter place in the list equals a new line
  27.          THEN
  28.                newcounter equals counter add one
  29.                escape the while loop
  30.          END IF
  31.          counter minus first counter place in cipherkeylist equal counter place in the list
  32.          Add one to the sizeofcipherkey
  33.          Add one to the counter
  34.    END WHILE
  35.    Set counter to newcounter
  36.    WHILE Counter is less than 6 plus newcounter
  37.          IF the counter place in the list equals a new line
  38.        THEN
  39.                Set countertwo to counter minus one
  40.                multiplier equal one
  41.                WHILE countertwo is greater than or equal to zero
  42.                      take the character at countertwo place in the list
  43.                      multiply by multipler
  44.                      multiplier is multiplied by 10
  45.                      add to the textlength
  46.                      Minus one from countertwo
  47.                escape the while loop
  48.          END IF
  49.    END WHILE
  50.    sizeoftext equal zero
  51.    WHILE counter is less than 256
  52.          IF counter place in the list equals a new line
  53.          THEN
  54.                newcounter equals counter add one
  55.                escape the while loop
  56.          END IF
  57.          counter minus first counter place in textlist equal counter place in the list
  58.          Add one to the sizeoftext
  59.          Add one to the counter
  60.    END WHILE
  61.    Set counter to zero
  62.    WHILE counter is less than the sizeofcipherkey
  63.          IF counter equals sizeofcipherkey minus one
  64.          THEN
  65.                condition equals 0
  66.          ELSE
  67.                conditon equals counter plus 1
  68.          Set countertwo to zero
  69.          WHILE countertwo is less than sizeoftext
  70.                IF countertwo in textlist equals counter in cipherkeylist
  71.                THEN
  72.                      countertwo in text2list equals conditon in cipherkey list
  73.                END IF
  74.                stepnumber equals counter plus one
  75.                output "step stepnumber substitute counter in ciperkeylist with condition in cipherkeylist
  76.                Set countertwo to zero
  77.                WHILE countertwo is less than textlength
  78.                      output " countertwo in text2list"
  79.                      Add on to countertwo
  80.                END WHILE
  81.         END WHILE
  82.    END WHILE
  83.          
  84. */
  85. int main (void)
  86. {
  87.   char doubleck[16], lowercasept[256], lowercase[16], plaintext[256];
  88.   char cipherkey[16], c, cknum4, plaintext2[256], all[400];
  89.   int greg, dan, bill, diff, runencoder, szpt, cknum, cknumd[6], cknumd2, cknumd1 , textlength, textlengthd[6];
  90.   int jeff, i, j, k, h, b, szck, test, f, digits, pointer;
  91.    
  92.   /*Read in everything to an array.*/
  93.   runencoder = 0;
  94.   greg = 0;
  95.   jeff = 0;
  96.   for(f=0;f<300;f++)
  97.   {
  98.     scanf("%c", &all[f]);
  99.   }
  100.   /*cipher key length*/
  101.   f = 1;
  102.   cknum = 0;
  103.   for(i=0;i<6;i++)
  104.   {
  105.     if(all[i]==10) // Finds the new line
  106.     {
  107.       digits=i+1; // Identifys where in the all array the cipher key starts
  108.       for(j=(i-1);j>=0;j--) // adds each digit in to an array
  109.       {
  110.         cknumd[j]=all[j] - '0';
  111.         cknumd[j]= cknumd[j] * f;
  112.         f = f * 10;
  113.         cknum = cknum + cknumd[j];
  114.       }
  115.       break;
  116.     }
  117.     if(all[i] < 48 || all[i] > 57)
  118.     {
  119.       runencoder = 1;
  120.       printf("The cipher key length you have entered is not an integer.\n");
  121.       greg = 1;
  122.     }
  123.   }
  124.   /*Is the cipher key the required length*/
  125.   if(greg != 1)
  126.   {
  127.     if(cknum < 2)
  128.     {
  129.       printf("The cipher key length must be from 2-16.\n%d is to small.", cknum);
  130.       runencoder = 1;
  131.     }
  132.     if(cknum > 16)
  133.     {
  134.       printf("The cipher key length must be from 2-16.\n%d is to big.", cknum);
  135.       runencoder = 1;
  136.     }
  137.   }
  138.  
  139.   /*Cipher key read in.*/
  140.   szck = 0;
  141.   for(i=digits;i<30;i++)
  142.   {
  143.     if(all[i]==10)
  144.     {
  145.       break;
  146.     }
  147.     cipherkey[i-digits]=all[i];
  148.     szck++;
  149.   }
  150.   pointer = (szck + digits + 1);
  151.   /*is the ciperkey all lowercase letters*/
  152.   for(i=0;i<szck;i++)
  153.   {
  154.     if(cipherkey[i] < 97 || cipherkey[i] > 122)
  155.     {
  156.       printf("%c is not a lowercase letter.\n", cipherkey[i]);
  157.       runencoder = 1;
  158.       jeff = 1;
  159.       lowercase[i] = '^';
  160.     }
  161.     else
  162.     {
  163.       lowercase[i] = ' ';
  164.     }
  165.   }
  166.   if(jeff == 1)
  167.   {
  168.     for(i=0;i<szck;i++)
  169.     {
  170.       printf("%c", cipherkey[i]);
  171.     }
  172.     printf("\n");
  173.     for(i=0;i<szck;i++)
  174.     {
  175.       printf("%c", lowercase[i]);
  176.     }
  177.     printf("\n");
  178.   }
  179.   /*double letter check.*/
  180.   for(i=0;i<szck;i++)
  181.   {
  182.     doubleck[i] = ' ';
  183.     for(j=0;j<szck;j++)
  184.     {
  185.       if(cipherkey[i] == cipherkey[j] && i != j)
  186.       {
  187.         runencoder = 1;
  188.         for(k=0;k<i;k++)
  189.         {
  190.           if(cipherkey[i] == cipherkey[k])
  191.           {
  192.             printf("%c has already been used.\n", cipherkey[i]);
  193.             doubleck[i] = '^';
  194.             dan = 1;
  195.           }
  196.         }
  197.       }
  198.     }
  199.   }
  200.   if(dan == 1)
  201.   {
  202.     for(i=0;i<szck;i++)
  203.     {
  204.       printf("%c", cipherkey[i]);
  205.     }
  206.     printf("\n");
  207.     for(i=0;i<szck;i++)
  208.     {
  209.       printf("%c", doubleck[i]);
  210.     }
  211.     printf("\n");
  212.   }
  213.   /*Plaintextlength */
  214.   f = 1;
  215.   textlength = 0;
  216.   for(i=pointer;i<(pointer+6);i++)
  217.   {
  218.     if(all[i]==10) // Finds the new line
  219.     {
  220.       digits=i+1;
  221.       for(j=(i-pointer)-1;j>=0;j--) // adds each digit in to an array.
  222.       {
  223.         textlengthd[j]=all[j+pointer] - '0';
  224.         textlengthd[j]=textlengthd[j] * f;
  225.         f = f * 10;
  226.         textlength = textlength + textlengthd[j];
  227.       }
  228.       break;
  229.     }
  230.   }
  231.  
  232.   pointer = digits;
  233.   /*Is the plaintext the required length*/
  234.   if(textlength < 4)
  235.   {
  236.     printf("The plaintext length must be from 4-256.\n%d is to small.\nEND PROGRAM", textlength);
  237.     runencoder = 1;
  238.   }
  239.   if(textlength > 256)
  240.   {
  241.     printf("The plaintext length must be from 4-256.\n%d is to big.\nEND PROGRAM", textlength);
  242.     runencoder = 1;
  243.   }
  244.   /*Plaintext read in.*/
  245.   szpt = 0;
  246.   for(i=0;i<textlength;i++)
  247.   {
  248.     plaintext[i]=all[i+pointer];
  249.     plaintext2[i]=all[i+pointer];
  250.     szpt++;
  251.   }
  252.   /*is the plaintext all lowercase letters*/
  253.   bill = 0;
  254.   for(i=0;i<szpt;i++)
  255.   {
  256.     if((plaintext[i] < 97 || plaintext[i] > 122) && plaintext[i] != 32)
  257.     {
  258.       printf("%c is not a lowercase letter.\n", plaintext[i]);
  259.       runencoder = 1;
  260.       bill = 1;
  261.       lowercasept[i] = '^';
  262.     }
  263.     else
  264.     {
  265.       lowercasept[i] = ' ';
  266.     }
  267.   }
  268.   if(bill !=0)
  269.   {
  270.     for(i=0;i<szpt;i++)
  271.     {
  272.       printf("%c", plaintext[i]);
  273.     }
  274.     printf("\n");
  275.     for(i=0;i<szpt;i++)
  276.     {
  277.       printf("%c", lowercasept[i]);
  278.     }
  279.     printf("\n");
  280.   }
  281.   /*is the cknum the same as the size of the ciperkey szck.*/
  282.   diff = 0;
  283.   if(greg != 1)
  284.   {
  285.     if(szck != cknum)
  286.     {
  287.       runencoder = 1;
  288.       printf("The cipherkey number you have entered does not match the size of cipherkey.\n");
  289.       if(szck < cknum)
  290.       {
  291.         diff = cknum - szck;
  292.         if(diff == 1)
  293.         {
  294.           printf("The cipherkey size is %d character smaller than the cipherkey number.\n", diff);
  295.         }
  296.         else
  297.         {
  298.           printf("The cipherkey size is %d characters smaller than the cipherkey number.\n", diff);
  299.         }
  300.       }
  301.       if(szck > cknum)
  302.       {
  303.         diff = szck - cknum;
  304.         if(diff == 1)
  305.         {
  306.           printf("The cipherkey size is %d character bigger than the cipherkey number.\n", diff);
  307.         }
  308.         else
  309.         {
  310.           printf("The cipherkey size is %d characters bigger than the cipherkey number.\n", diff);
  311.         }
  312.       }
  313.       printf("\n");
  314.     }
  315.     /*is the textlength the same as the size of the plaintext szpt.*/
  316.     if(szpt != textlength)
  317.     {
  318.       runencoder = 1;
  319.       printf("The text length you have entered does not match the size of plain text.\n");
  320.       if(szpt < textlength)
  321.       {
  322.         diff = textlength - szpt;
  323.         if(diff == 1)
  324.         {
  325.           printf("The plain text size is %d character smaller than the text length.\n", diff);
  326.         }
  327.         else
  328.         {
  329.           printf("The plain text size is %d characters smaller than the text length.\n", diff);
  330.         }
  331.       }
  332.       if(szpt > textlength)
  333.       {
  334.         diff = szpt - textlength;
  335.         if(diff == 1)
  336.         {
  337.           printf("The plain text size is %d character bigger than the text length.\n", diff);
  338.         }
  339.         else
  340.         {
  341.           printf("The plain text size is %d characters bigger than the text length.\n", diff);
  342.         }
  343.       }
  344.     }
  345.   }
  346.   /*all conditions met for encodeing*/
  347.   if(runencoder == 0)
  348.   {
  349.     /*Rearrange the plain text and display the output.*/
  350.    
  351.     for(i=0;i<szck;i++)
  352.     {
  353.       if(i==(szck-1))
  354.       {
  355.         k = 0;
  356.       }
  357.       else
  358.       {
  359.         k = i + 1;
  360.       }
  361.       for(j = 0;j < szpt;j++)
  362.       {
  363.         if(plaintext[j] == cipherkey[i])
  364.         {
  365.           plaintext2[j] = cipherkey[k];
  366.         }
  367.       }
  368.       b = i + 1;
  369.       printf("Step %d substitute %c with %c.\n", b, cipherkey[i], cipherkey[k]);
  370.       for(h = 0;h < szpt;h++)
  371.       {
  372.         printf("%c", plaintext2[h]);
  373.       }
  374.       printf("\n");
  375.     }
  376.   }
  377.   else
  378.   {
  379.     printf("END PROGRAM");
  380.   }
  381. }