C-Code Shift Cipher
You need these following files to make it work:
This is the Dictionary so that it can check the words in the file
Cipher file that you can use to change to plain text
Cipher file much of same #2
Cipher file much of same #3
Cipher file much of same #4
Input file to change to cipher text
Input file much of the same #2
Input file much of the same #3
Here is the source code for the file: Shift Cipher
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | /* Project #4 Project Description: In this project, you will implement three basic cryptography procedures for shift cipher. Cryptography refers to the use of encryption (or encoder) to convert plain text information into unintelligible gibberish (cipher text) for security purposes. A corresponding decryption algorithm (or decoder) will convert the cipher text back to the plain text with the correct decryption key. A code breaker tries to decode the cipher text without the key. Shift cipher is one of the simplest cryptography systems. It encrypts the plain text message by shifting each character for fixed number of characters down the relevant alphabet. This fixed number is called the encryption key. For example, consider the English alphabet, when the encryption key is 2, a, b, c, …, x, y, z will be shifted to c, d, e, …, z, a, b, respectively. A shift cipher with encryption key 2 will convert the plain text “project” into cipher text “rtqlgev”. The decoder in this case will be shifting each letter 2 position to the left, that is, replacing a, b, c, …, x,y,z in the cipher text by y, z, a, …, v, w, x, respectively. It suffices for a code breaker in this case to guess the value of the encryption/decryption key. You need to write a single program to perform these three basic cryptography procedures at the user’s choice. Assuming that a.out is the executable of your program: a.out 1 input output key encodes input file with key and write cipher text to output a.out 2 input output key decodes input file with key and write plain text to output a.out 3 input output breaks the cipher text input and write plain text to output The code breaker procedure tries all the possible key values and matches the words in the corresponding plain text with the words in a dictionary “mydictional.txt”. The key that produces the maximal number of matches will be considered as the key and will be used to generate the plain text file output. */ #include<stdlib.h> #include<stdio.h> #include<string.h> #define TRUE 1 #define FALSE 0 #define my_letters 72 int main (int argc, char *argv[]) { /*-------------------Declaration of the variables------------------------------------------*/ FILE *input, *output, *dictionary; int i=FALSE, j=FALSE, k=FALSE, l=FALSE, v=FALSE; char ch, dict; char my_letter_array[my_letters]; char *ptr, *library; int key, correct_key=FALSE, number_matched=0; int user_input; int counter=FALSE, matches=FALSE; int compare=FALSE, cnt=FALSE; if(argv[4] != NULL) { key = atoi(argv[4]); } if(argv[1] != NULL) { user_input = atoi(argv[1]); } /*-------------------Command Line Arguments------------------------------------------------*/ if(argc < 4 || argc > 5) // Checks to see if the usage is correct. { printf("\nUsage: (exectuable) (user_choice) (input_file) (output_file) (key_code)\n"); printf("\n*****(key_code **only** if user_input is 1 or 2)*****\n"); // Prints this is the information if not inputed corectly. return(FALSE); } input = fopen(argv[2],"r"); // Open the input file. if ((user_input < TRUE) || (user_input > 3)) { printf("\nThat is not a valid encryption/decryption setting! (Must be between 1-3)\n"); return(FALSE); } if(argv[4] == NULL && user_input != 3) //Makes sure that a key is entered if user_input is 1 or 2. { printf("\nA key value is needed in order to proceed (key = (0-72))\n"); return(FALSE); } if ((key < FALSE || key > 72) && user_input != 3) //Makes sure that the key is not a neg or greater than 72. { printf("\nA key value is needed in order to proceed (key = (0-72))\n"); return(FALSE); } input = fopen(argv[2],"r"); // Open the input file. if( input == NULL) // Checks whether there is an input file. { printf("\nFile %s cannot open!\n", argv[2]); // Prints this if not. return(FALSE); } output = fopen(argv[3], "w"); // Opens the output file. if( output == NULL) // Checks whether there is and output file. { printf("\nFile %s cannot open!\n", argv[3]); // Prints this if not. return(FALSE); } dictionary = fopen("mydictional.txt", "r"); // Opens the dictionary file to check the words. if( dictionary == NULL) // Checks whether there is a dictionary file. { printf("\nFile %s cannot open!\n", dictionary); // Prints this if not. } /*------------------Make the 72-letter alphabet--------------------------------------------*/ for(i=97; i<123; i++) { my_letter_array[counter++] = i; } for(i=65; i<91; i++) { my_letter_array[counter++] = i; } for(i=48; i<58; i++) { my_letter_array[counter++] = i; } my_letter_array[counter++] = ';'; my_letter_array[counter++] = ':'; my_letter_array[counter++] = ','; my_letter_array[counter++] = '.'; my_letter_array[counter++] = '\''; my_letter_array[counter++] = '"'; my_letter_array[counter++] = '('; my_letter_array[counter++] = ')'; my_letter_array[counter++] = '\n'; my_letter_array[counter++] = ' '; /*------------------Memory allocation of the pointers--------------------------------------------*/ if (( ptr = (char *) malloc(sizeof(char))) == NULL) { printf("no memory ...\n"); return(FALSE); } if (( library = (char *) malloc(sizeof(char))) == NULL) { printf("no memory ...\n"); return(FALSE); } /*------------------Scan and print to output file for option 1-----------------------------------*/ if(user_input == TRUE) { while ((fscanf(input, "%c", &ch) == TRUE) && (!feof(input))) { ptr[i++] = ch; for (k=FALSE; k<my_letters; k++) { if (ch == my_letter_array[k]) { if (k+key>(my_letters-1)) //Encrypts the char by the key. { ch = my_letter_array[(k+key)%my_letters]; //If the key put the char past my_letter_array it will return to the start. break; } else { ch = my_letter_array[k+key]; //Else just increase the ch by the array. break; } } } fprintf(output, "%c", ch); //print each ch to the output. ptr = (char *) realloc(ptr, (i+1)*sizeof(char)); } printf("\n\n******This is what is in the input_file:******\n\n"); for (j=FALSE; j<i; j++) { printf("%c", ptr[j]); //This is just to show what was scanned in to the ptr pointer. } printf("\n"); free(ptr); printf("\n\n******The encryption has taken place******\n\n"); } /*------------------Scan and print to output file for option 2-----------------------------------*/ if(user_input == 2) { while ((fscanf(input, "%c", &ch) == 1) && (!feof(input))) { ptr[i++] = ch; for (k=FALSE; k<my_letters; k++) { if (ch == my_letter_array[k]) { if (k-key<FALSE) //Decrypts the char by the key. { ch = my_letter_array[my_letters-abs(k-key)]; //If the key subtracts past the first array it will start from the end. break; } else { ch = my_letter_array[k-key]; //Else just decrease the ch by the array. break; } } } fprintf(output, "%c", ch); ptr = (char *) realloc(ptr, (i+1)*sizeof(char)); } printf("\n\n******This is what is in the input_file:******\n\n"); for (j=FALSE; j<i; j++) { printf("%c", ptr[j]); //This was just to show what was scanned in to the ptr pointer. } printf("\n"); free(ptr); printf("\n\n******The decryption has taken place******\n\n"); } /*------------------Scan and print and determine the key that is needed to decifer the code----------*/ i=FALSE, k=FALSE, j=FALSE; if(user_input == 3) { printf("%s%11s\n", "Key", "Matches"); for(key=1; key<my_letters; key++) //This changes the key from 1 to 71. { input = fopen(argv[2], "r"); //Opens the input file to read again. while ((fscanf(input, "%c", &ch) == TRUE) && (!feof(input))) //Scans the input file until the end of file. { for (k=FALSE; k<my_letters; k++) //Same algorithm as the user_input 2 for shifting the ch. { if (ch == my_letter_array[k]) { if (k-key<FALSE) { ch = my_letter_array[my_letters-abs(k-key)]; break; } else { ch = my_letter_array[(k-key)]; break; } } } switch(ch) //If any ch is equal to the below statements make it a string. { case ';': case ':': case ',': case '.': case '\'': case '"': case '(': case ')': case '\n': case ' ': { ptr[j] = '\0'; dictionary = fopen("mydictional.txt", "r"); while ((fscanf(dictionary, "%c", &dict) == TRUE) && (!feof(dictionary))) //until the end of file. { if(dict == '\n') //if the ch is \n line make the library string. { library[i++] = '\0'; compare = strcmp(ptr, library); //Compare the library string and the ptr string. if (compare == FALSE) //This checks if they are the same and increments the amount of mathces. { matches++; break; //If it matches break out of the loop. } i=FALSE; //Reset i to 0 so that it will overwrite the data. } else { library[i++] = dict; //If the dict is not a newline char make the library string. } library = (char *) realloc(library, (strlen(library)+1)); //Reallocate the memory to library. } j=-1; //Reset i to -1 so that it will overwrite the data. fclose(dictionary); //Close the dictionary so no seg faults. } default: { ptr[j] = ch; //If the ch is not a punctuation make the ptr string. j++; ptr = (char *) realloc(ptr, (strlen(ptr)+1)); //Reallocate the memory to ptr. } } } printf("%-2d %-2d\n", key, matches); //Show what the matches are from the key. if (matches > number_matched) //If the matches is greater than the number_matched. { number_matched = matches; //Finds the largest number of matches. correct_key = key; //And then tells what the correct_key is. } matches=FALSE; //Set matches to 0. fclose(input); //Close the input file. } printf("\nCorrect Key is %d\n", correct_key); //Tell the user the correct key. } free(ptr); free(library); fclose(input); fclose(output); fclose(dictionary); return FALSE; } |
Popularity: 5%
Related Posts
- Website Music Player I am finally able to sit down and tell you...
- Render update info on the plugins panel Ok So I wanted to tell everyone about how when...
- My own RSS for music files from PHP If you have read the previous post about the Website...
- FATAL ERROR MEMORY ISSUES Wordpress Ok So I needed to install a new version of...
- Best Products for Healthy Fingernails In tough economic times, one of the first things that...
- FILES Creating and Opening Documents There are several ways to create...
- The power of find command in Linux - advanced. Generally whoever uses Linux, would know about the find command....
- studiomaster diamond compact 4-2 Mixer Pictures from Paul Front Panel Back Panel Main Board...


Firefox
about 1 month ago
Hey reliable Info. It is much useful Later you Think about it. Increasingly obliging. Aloha.
about 4 months ago
yea man cool fundamentally it works on borland! nice work.
about 4 months ago
library “stdlib.h”
about 4 months ago
Yea the code doesnt work you need to add “#include” to the libraries. You finished your “for loops” with a semicolon. Did you even bother compiling this mountain of dung? Also its not reading from the file “dictionary.txt” its trying to find “mydictional.txt” which when renamed still will not open. Overall -as you see it now- this code is for the bin.
about 4 months ago
Ok so I have fixed the code and it works with putty gcc compiler so if it does not work with others please let me know which compiler you are using.
Tom