Esempio n. 1
0
bool callFunction(std::string get, int scramblers[3+1][26], int shift[3], int rotor[3]){

    // make input string lower case
    for(size_t i = 0, n = get.length(); i < n; i++)
        get[i] = tolower(get[i]);

    if(get.compare("end") == 0)
        return false;
    if(get.compare("help") == 0)
        help();
    else if(get.compare("generate_key") == 0 or get.compare("gk") == 0)
        generateKey();
    else if(get.compare("generate_scrambler") == 0 or get.compare("gs") == 0)
        generateScrambler(scramblers);
    else if(get.compare("set_key") == 0 or get.compare("sk") == 0)
        setKey(scramblers);
    else if(get.compare("encrypt") == 0 or get.compare("e") == 0)
        encrypt(scramblers, shift, rotor);
    else if(get.compare("set_shift") == 0 or get.compare("ss") == 0)
        setShift(shift);
    else if(get.compare("set_rotor") == 0 or get.compare("sr") == 0)
        setRotor(rotor);
    else if(get.compare("reset_shift") == 0 or get.compare("rs") == 0)
        resetShift(shift);
    else if(get.compare("print_shift") == 0 or get.compare("ps") == 0)
        printShift(shift);
    else if(get.compare("print_rotor") == 0 or get.compare("pr") == 0)
        printRotor(rotor);
    else if(get.compare("encrypt_file") == 0 or get.compare("ef") == 0)
        encryptFile(scramblers, shift, rotor);
    else if(get.compare("capitalize") == 0 or get.compare("c") == 0)
        capitalize();
    else if(get.compare("set_plugboard") == 0 or get.compare("sp") == 0)
        setPlugboard();
    else if(get.compare("save") == 0 or get.compare("s") == 0)
        save(scramblers, shift, rotor);
    else if(get.compare("load") == 0 or get.compare("l") == 0)
        load(scramblers, shift, rotor);
    else
        unrecognized();


    return true;
}
Esempio n. 2
0
// isi dari setiap menu pada fungsi main.
void subMenu(int menu, char *sentence){
	int i = 0, j;
	int sub_menu;
    int flag = 0;
	char rotor_plug_settings[27];
	char sub_menu_pilihan;

	if(menu == 1){          // isi dari menu 1

		clear();
		printf("\n	  #######################################\n");
		printf("	  #                                     #\n");
		printf("  	  #    Encrypt or Decrypt a sentence	#\n");
		printf("  	  #                                     #\n");
		printf("  	  #######################################\n \n \n");

		printf("	Just typed in the sentence and it will decrypt/encrypt.\n");
		printf("	Make sure it's below 1024 words.\n\n");


		printf("	Enter a sentence : ");
		inputData(sentence,1024,0,0);       // meminta kalimat yang akan di encrypt, disimpan pada array sentence


		while (sentence[i] != '\0'){        // loop hingga akhir dari string

            sentence[i] = toupper(sentence[i]);
			if(isalpha(sentence[i])){       // meng-encrypt setiap huruf alfabet
			sentence[i] = fullEncrypt(sentence[i] - 64) + 64;       // meng-encrypt dan merubahnya lagi jadi char
			}

			i++;
		}

		clear();
		printf("\n	  #######################################\n");
		printf("	  #                                     #\n");
		printf("  	  #    Encrypt or Decrypt a sentence	#\n");
		printf("  	  #                                     #\n");
		printf("  	  #######################################\n \n \n");

		printf("	The encrypted or derypted sentence : %s",sentence);         // menampilkan kalimat yang telah di encrypt

		pause();
	}

	else if(menu == 2){         // isi dari menu 2

        char c;
        clear();
		printf("\n	  #######################################\n");
		printf("	  #                                     #\n");
		printf("  	  #    Encrypt or Decrypt a file    	#\n");
		printf("  	  #                                     #\n");
		printf("  	  #######################################\n \n \n");

		printf("	Just type in the filename and extension, it will be decrypted/encrypted.\n");
		printf("	Make sure its in the same place with the program.\n\n");


		printf("	Type in the filename: ");
		inputData(sentence,1024,0,0);       // meminta input nama file dan ekstensinya
        char output_file[128];
        FILE *wFile, *rFile;                // deklarasi variable untuk membuka file

        strcpy(output_file,sentence);       // mengubah nama input file menjadi output
        strcat(output_file, ".enigma");     // file dengan menambahakan akhiran .enigma

        wFile = fopen (output_file,"w");    // membuka file output
        rFile=fopen (sentence,"r");         // membuka file input

        if (wFile!=NULL && rFile!=NULL) {   // jika membuka kedua file berhasil

            while ((c = fgetc(rFile)) != EOF){      // looping dan mengambil setiap char dari file
                c = toupper(c);
                if(isalpha(c)){         // jika char tersebut alfabet maka lakukan
                    c = fullEncrypt(c - 64) + 64;   // enkripsi char tersebut
                    c = tolower(c);
                }
                fputc ( c , wFile );    // menulis ke output file
            }

        fclose (wFile);
        fclose (rFile);
        printf("\n\t");
        printf("Encrypt/Decrypt the file is a success!");
        }

        else{
            printf("\n\t");
            perror ("Error opening file");
        }
        pause();
	}

	else if(menu == 3){

		clear();
		printf("\n	  ############################\n");
		printf("	  #                          #\n");
		printf("  	  #    Encryption Settings   #\n");
		printf("  	  #                          #\n");
		printf("  	  ############################\n \n \n");

		printf("       ##################################\n");
		printf("       #                                #\n");
		printf("       #  1. Show Encryption Settings   #\n");
		printf("       #  2. Set Rotor Settings         #\n");
		printf("       #  3. Set Plug Board             #\n");
		printf("       #  4. Reset Settings             #\n");
		printf("       #  5. Back                       #\n");
		printf("       #                                #\n");
		printf("       ##################################\n \n");

		printf("	Enter a menu : ");

		inputData(&sub_menu_pilihan, 0, 1, 5);      // meminta pilihan menu
		sub_menu =  atoi(&sub_menu_pilihan);

		if (sub_menu == 1){     // isi dari submenu 1
			clear();
			printf("	  ################################\n");
			printf("	  #                              #\n");
			printf("  	  #    Show Encryption Settings  #\n");
			printf("  	  #                              #\n");
			printf("  	  ################################\n \n \n");

            /* memprint lokasi titik rotor dan isi dari array plugboard */
			printf("	Rotor Settings are : %c%c%c\n", checkRotor(rotor[2]) + 64,checkRotor(rotor[1]) + 64,checkRotor(rotor[0]) + 64);
			printf("	                          ABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
			printf("	Plug Board Settings are : ");

			for( i = 1; i < 27; i++){
                printf("%c",plugBoard[i] + 64);
			}
			pause();
		}

		else if (sub_menu == 2){        // isi dari submenu 2

            clear();
			printf("	  #########################\n");
			printf("	  #                       #\n");
			printf("  	  #   Set Rotor Settings  #\n");
			printf("  	  #                       #\n");
			printf("  	  #########################\n \n");

            printf("\n\t Input the settings of each rotor. example : AQZ");

            printf("\n	Enter a settings : ");
			inputData(rotor_plug_settings,4,0,0);   // input 3 buah kata untuk rotor settings
            i = 0;
			while (rotor_plug_settings[i] != '\0'){    // looping hingga akhr string
				if (!isalpha(rotor_plug_settings[i])){      // jika terdapat non-alfabat maka, update setting gagal.
					printf("\n        Your Input is Invalid. \n ");
					flag = 1;
					pause();
					break;
				}

				rotor_plug_settings[i] = toupper(rotor_plug_settings[i]);
				i++;
			}

			if( i != 3 && flag == 0){   // Jika inputnya kurang dari 3 huruf maka gagal update settings

				printf("\n		Your Input is Invalid. \n ");
				flag = 1;
				pause();
			}

			if(flag == 0) setRotor(rotor_plug_settings); // jika bberhasil maka update rotor settings
		}

		else if (sub_menu == 3){    // isi dari submenu ke 3
            clear();
			printf("	  #########################\n");
			printf("	  #                       #\n");
			printf("  	  #    Set Plug Board     #\n");
			printf("  	  #                       #\n");
			printf("  	  #########################\n\n");

			printf("\n\t Pair every alphabet below with another word. It could be");
			printf("\n\t any alphabet. If you pair A with C, C must be pair with A,");
			printf("\n\t and so on. If you don't want to pair anything, input the");
			printf("\n\t same alphabet as the one above it.");
			printf("\n\t Example : IMETCGFRAYSQBZXWLHKDVUPOJN");

			printf("\n\n\n\t                                  ABCDEFGHIJKLMNOPQRSTUVWXYZ");
			printf("\n\tTyped in the PlugBoard Settings : ");
			inputData(rotor_plug_settings,27,0,0);      // input berupa 26 huruf dan dimasukan ke array rotor_plug_settings

			while (rotor_plug_settings[i] != '\0'){     // looping hingga akhir string
				if (!isalpha(rotor_plug_settings[i])){  // // jika terdapat non-alfabat maka, update setting gagal.
					printf("\n		Your Input is Invalid.");
					flag = 1;
					pause();
					break;
				}
				rotor_plug_settings[i] = toupper(rotor_plug_settings[i]);
				i++;
			}

			if( i != 26 && flag == 0){      // Jika inputnya kurang dari 26 huruf maka gagal update settings

				printf("\n		Your Input is Invalid. \n ");
				flag = 1;
				pause();
			}

			if(flag == 0){
                for (i = 0; i < 27; i++) plugBoard[i+1] = rotor_plug_settings[i] - 64;  // jika berhasil masukan ke array plugboard dan ubah menjadi angka
			}
		}

		else if (sub_menu == 4){        // me reset semuanya ke settingan awal
            strcpy(plugBoard, "-IMETCGFRAYSQBZXWLHKDVUPOJN");
            for(i = 0 ; i < 27; i++)plugBoard[i] -= 64;
			rotor_plug_settings[0] = 'A';
			rotor_plug_settings[1] = 'A';
			rotor_plug_settings[2] = 'A';
			setRotor(rotor_plug_settings);
		}

		else if (sub_menu == 5){    // exit
		}
	}
}