Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int encrypt(char x,int y);
- int encrypt_option(void);
- int decrypt_menu(void);
- int decrypt(char x, int y);
- int main(void)
- {
- /* ***************** */
- /* MAIN MENU */
- /* ***************** */
- char *menu;
- menu = malloc(64*sizeof(char));
- while(strcmp(menu,"quit")!=0)
- {
- printf("Menu option: ");
- scanf("%s",menu);/*request menu input*/
- if(strcmp(menu,"enc") == 0)
- {
- encrypt_option();
- }
- else if(strcmp(menu,"dec") == 0)
- {
- decrypt_menu();
- }
- }
- return 0;
- }
- int encrypt_option(void)
- {
- /* ***************************** */
- /* REQUESTS STRING TO */
- /* ENCRYPT, THEN LOOPS THROUGH */
- /* encode() */
- /* ***************************** */
- int ctr, shift;
- char *intext;
- char *outtext;
- intext = malloc(256 * sizeof(char));
- outtext = malloc(256 * sizeof(char));
- printf("String: ");
- scanf("%s",intext);
- printf("Shift: ");
- scanf("%d",&shift);
- for(ctr = 0;ctr<strlen(intext);ctr++)
- {
- outtext[ctr] = encrypt(intext[ctr],shift);
- }
- printf("%s\n",outtext);
- return 0;
- }
- int encrypt(char x,int y)
- {
- /* ************************* */
- /* THIS FUNCTION ENCRYPTS */
- /* ONE CHARACTER AT A TIME */
- /* ************************* */
- /*x is a character */
- /*y is the shifting increment*/
- int offset; /*offset after max value (z, or 122)*/
- int z; /*final variable to return*/
- if ((x+y) > 122)
- {
- offset = ((x+y) - 122);
- z = 96 + offset; /*96 rather than 97 because 97 = a;*/
- }
- else
- {
- z = x + y; /*ASCII CHARACTER + SHIFT VALUE*/
- }
- return z;
- }
- int decrypt_menu(void)
- {
- /* ***************** */
- /* DECRYPTOR MENU */
- /* ***************** */
- char *dc_menu;
- dc_menu = malloc(64*sizeof(char));
- while(strcmp(dc_menu,"quit")!=0)
- {
- printf("Decryptor option: ");
- scanf("%s",dc_menu);/*request menu input*/
- if(strcmp(dc_menu,"shift") == 0)
- {
- shift_decrypt_option();
- }
- }
- return 0;
- }
- int shift_decrypt_option()
- {
- int ctr,shift;
- char *intext;
- char *outtext;
- intext = malloc(256*sizeof(char));
- outtext = malloc(256*sizeof(char));
- printf("Encrypted string: ");
- scanf("%s",intext);
- printf("Shift of previous cipher: ");
- scanf("%d",&shift);
- for(ctr = 0;ctr<strlen(intext);ctr++)
- {
- outtext[ctr] = decrypt(intext[ctr],shift);
- }
- printf("%s\n",outtext);
- return 0;
- }
- int decrypt(char x, int y)
- {
- /*x is a character */
- /*y is the shifting increment*/
- int offset,z;
- if((x-y)<97)
- {
- offset = 97 - (x-y);
- z = 123 - offset;
- }
- else
- {
- z = x - y;
- }
- return z;
- }
Add Comment
Please, Sign In to add comment