All pastes #2090863 Raw Edit

Miscellany

public text v1 · immutable
#2090863 ·published 2011-10-17 18:01 UTC
rendered paste body
//Rayyan Khoury


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// main method runs at start up of the program. program is an encyrpter thus takes an input that the user wants to encrypt and outputs an encrypted
// form of what the user inputted. later, the user can decrypt the input using the decryption method.

int main (int chooser, char Matrix[50][50], int inputtedNumber)
{

  // a check for whether the user inputted a number that an integer can handle. if not, it sets the input number automatically at 0

  if (!((inputtedNumber>=0)&&(inputtedNumber<=2147483000))){
    
    inputtedNumber=0;

  }

  // calls the main menu to appear on the screen

  mainMenu(0);

  // scans for the user's choise, ranging from 1 to 4; 1 is the matrix initializer, 2 is the matrix encrypter, 3 is the matrix decrypter, and 4 is
  // the exit function

  scanf("%d",&chooser);

  if ((chooser<1)||(chooser>4)){

    printf("Please enter a number between 1 and 4. Other inputs are not accepted, thank you.\n");
    main(chooser,Matrix,inputtedNumber);

  }

  // calls the identifier function which identifies what the user has chosen

  identifier(chooser,Matrix,inputtedNumber);


}

// identifier method which analyzes and chooses what function to run once the user has inputted an integer indicating his choice

int identifier (int identifier, char Matrix[50][50], int inputtedNumber){

  if (identifier==1){

    printf("Thank you for choosing to input text.\n");
    matrixInitializer(identifier,Matrix);
    main(identifier,Matrix,inputtedNumber);

  }

  if (identifier==2){

    printf("Thank you for choosing to encrypt.\n");
    inputtedNumber=matrixEncrypter(identifier,Matrix);
    main(identifier,Matrix,inputtedNumber);

  }

  if (identifier==3){

    printf("Thank you for choosing to decrypt.\n");
    matrixDecrypter(identifier,Matrix,inputtedNumber);
    main(identifier,Matrix,inputtedNumber);

  }

  if (identifier==4){

    printf("No thank you for choosing to exit.\n");
    exitter(identifier);

  }

  return identifier;
  
}

// function which initializes the main menu to appear on screen

int mainMenu(int initializer) {
  
  if(initializer!=0){

    return 0;

  }

  printf("MAIN MENU\n");
  printf("==========\n");
  printf("1. Input Text\n");
  printf("2. Encrypt\n");
  printf("3. Decrypt\n");
  printf("4. Exit\n");
  printf("Selection:_\n");

  return 0;

}

// function which intializes the input and converts the user's input in to a 50 by 50 matrix and stores it in a pointer

 int matrixInitializer(int initializer, char Matrix[50][50]){

   if (initializer=!1){

     return 0;

   }

   int i;
   int j;

   // initializes the matrix to a matrix of blank spaces

   for (i=0;i<50;i++){

     for(j=0;j<50;j++){

       Matrix[i][j]=' ';

     }

   }

   // user starts inputting his text at this point, and as fgets inputs the new line when pressing enter, if the first position within a particular
   // row in the matrix contains the new line character, the user no longer has to input any data in to the matrix. the user essentially just has to
   // click enter, and then input to the matrix will stop.

   printf("Start inputting the text that you would like to encrypt. Make sure you limit the amount of characters on a particular line to 49 characters. When you are fed up of entering text for encryption, just press enter on a blank line and you will be returned to the Main Menu. Thank you for your cooperation, and good luck encrpyting and decrypting text.\n");

   for (i=0; i<50; i++){

     fgets(Matrix[i],50,stdin);

     if (Matrix[i][0]=='\n'){

       break;

     }

   }

   printf("You are done inputting text in to the encrypter. Thank you for being such a good sport.\n");

   return i;

 }

// this is where the encryption happens

int matrixEncrypter (int initializer, char Matrix[50][50]){

  if (initializer!=2){

     return 0;

       }

   int i;
   int j;
   char encryptMe;
   int initialValue;
   int encrypter=0;
   int decrypter;

   // at this point the user decides how he wants to encrypt his message. this is done using caeser encyrption, and the value
   // of the user's input shifts letters down the amount that is asked by the user. e.g. if the user wants to encrypt by the 
   // value of 2, then the letter a becomes c, and the letter b becomes d etc.

   printf("Please input an integer value for the encryption process. This can be any number between 0 and 2147483000.\n");

   scanf("%d",&encrypter);

   if((encrypter<0)||(encrypter>2147483000)){
     
     printf("You have failed to follow instructions. Repeating the process.\n");
     matrixEncrypter(initializer,Matrix);

   }

   // the matrix goes through the encryption process at this point

   decrypter=encrypter;

   for (i=0; i<50; i++){

     for (j=0; j<50;j++){

       encryptMe=Matrix[i][j];
       initialValue=(int)encryptMe;
       
       if((initialValue<=122)&&(initialValue>=97)){
	 encrypter=encrypter%26;
	 initialValue=initialValue+encrypter;
	 
	 if(initialValue>122){
	   initialValue=initialValue-26;
	 }
	 
	 encryptMe=(char)initialValue;
	 Matrix[i][j]=encryptMe;

       }

       if((initialValue<=90)&&(initialValue>=65)){
	 encrypter=encrypter%26;
	 initialValue=initialValue+encrypter;
	 
	 if(initialValue>90){
	   initialValue=initialValue-26;
	 }
	 
	 encryptMe=(char)initialValue;
	 Matrix[i][j]=encryptMe;

       }

     }

   }

   // Here the matrix is outputted in its entirety. Although not particularly useful, this print statement was inserted due to assignment
   // instructions.

   printf("Now we will output the entire matrix in encrypted form. If you have not inputted 50 lines of encryption material, then there will be a lot of spaces. Otherwise you will be seeing a lot of characters that may not necessarily be understood, however once you enter the decryption vein of this program, you will see the initial lines that you inputed in to this encrypter being outputed. Congratualtions on the good use of this program; the programmer commends you for your good work.\n");

   for (i=0; i<50; i++){

     for (j=0; j<50;j++){

       printf("%c",Matrix[j][i]);

     }

   }

   return decrypter;

 }

// Here the matrix gets decrypted using the reverse formula of the encryption method. No explanation needed as one can read above.

 int matrixDecrypter (int initializer, char Matrix[50][50], int decrypter){

   if (initializer!=3){
     
     return 0;

   }

   int i;
   int j;
   char decryptMe;
   int initialValue;

   for (i=0; i<50; i++){

     for (j=0; j<50;j++){

       decryptMe=Matrix[i][j];
       initialValue=(int)decryptMe;
       
       if((initialValue<=122)&&(initialValue>=97)){
	 decrypter=decrypter%26;
	 initialValue=initialValue-decrypter;
	 
	 if(initialValue<97){
	   initialValue=initialValue+26;
	 }
	 
	 decryptMe=(char)initialValue;
	 Matrix[i][j]=decryptMe;

       }

       if((initialValue<=90)&&(initialValue>=65)){
	 decrypter=decrypter%26;
	 initialValue=initialValue+decrypter;
	 
	 if(initialValue<95){
	   initialValue=initialValue+26;
	 }
	 
	 decryptMe=(char)initialValue;
	 Matrix[i][j]=decryptMe;

       }

     }

   }

   printf("Now we will output the entire matrix in decrypted form. If you have not inputted 50 lines of encryption material, then there will be a lot of spaces.\n");

   for (i=0; i<50; i++){

     for (j=0; j<50;j++){

       printf("%c",Matrix[j][i]);

     }

   }

 }

// This function exits the program.

 int exitter (int identifier){

   printf("Thanks for using the encryption program! Bye bye!\n");

   return identifier;

}