diff --git a/otp.c b/otp.c index d082165..b0609cb 100644 --- a/otp.c +++ b/otp.c @@ -95,6 +95,9 @@ void show_main_menu(void); int handle_generate_menu(void); int handle_encrypt_menu(void); int handle_decrypt_menu(void); +int handle_text_encrypt(void); +int handle_file_encrypt(void); +int handle_smart_decrypt(void); void print_usage(const char* program_name); @@ -116,14 +119,17 @@ int interactive_mode(void) { char choice = toupper(input[0]); switch (choice) { - case 'G': - handle_generate_menu(); + case 'T': + handle_text_encrypt(); break; - case 'E': - handle_encrypt_menu(); + case 'F': + handle_file_encrypt(); break; case 'D': - handle_decrypt_menu(); + handle_smart_decrypt(); + break; + case 'G': + handle_generate_menu(); break; case 'L': list_available_pads(); @@ -145,7 +151,7 @@ int interactive_mode(void) { printf("Goodbye!\n"); return 0; default: - printf("Invalid option. Please select G, E, D, L, S, or X.\n"); + printf("Invalid option. Please select T, F, D, G, L, S, or X.\n"); continue; } } else { @@ -262,9 +268,10 @@ int command_line_mode(int argc, char* argv[]) { void show_main_menu(void) { printf("=== Main Menu ===\n"); + printf("\033[4mT\033[0mext encrypt\n"); + printf("\033[4mF\033[0mile encrypt\n"); + printf("\033[4mD\033[0mecrypt\n"); printf("\033[4mG\033[0menerate new pad\n"); - printf("\033[4mE\033[0mncrypt data (text/file)\n"); - printf("\033[4mD\033[0mecrypt data (text/file)\n"); printf("\033[4mL\033[0mist available pads\n"); printf("\033[4mS\033[0mhow pad information\n"); printf("E\033[4mx\033[0mit\n"); @@ -2473,6 +2480,138 @@ int launch_file_manager(const char* start_directory, char* selected_file, size_t return 1; // Fall back to manual entry } +int handle_text_encrypt(void) { + printf("\n=== Text Encrypt ===\n"); + + // Launch text editor directly + char text_buffer[MAX_INPUT_SIZE]; + if (launch_text_editor(NULL, text_buffer, sizeof(text_buffer)) != 0) { + printf("Error: Could not launch text editor\n"); + return 1; + } + + if (strlen(text_buffer) == 0) { + printf("No text entered - canceling encryption\n"); + return 1; + } + + // List available pads and get selection + int pad_count = list_available_pads(); + if (pad_count == 0) { + printf("No pads available. Generate a pad first.\n"); + return 1; + } + + printf("\nEnter pad selection (number, checksum, or prefix): "); + char pad_input[MAX_HASH_LENGTH]; + if (!fgets(pad_input, sizeof(pad_input), stdin)) { + printf("Error: Failed to read pad selection\n"); + return 1; + } + pad_input[strcspn(pad_input, "\n")] = 0; + + return encrypt_text(pad_input, text_buffer); +} + +int handle_file_encrypt(void) { + printf("\n=== File Encrypt ===\n"); + + // Launch file manager directly + char input_file[512]; + if (launch_file_manager(".", input_file, sizeof(input_file)) != 0) { + printf("Error: Could not launch file manager\n"); + return 1; + } + + // Check if file exists + if (access(input_file, R_OK) != 0) { + printf("Error: File '%s' not found or cannot be read\n", input_file); + return 1; + } + + // List available pads + int pad_count = list_available_pads(); + if (pad_count == 0) { + printf("No pads available. Generate a pad first.\n"); + return 1; + } + + printf("\nEnter pad selection (number, checksum, or prefix): "); + char pad_input[MAX_HASH_LENGTH]; + if (!fgets(pad_input, sizeof(pad_input), stdin)) { + printf("Error: Failed to read pad selection\n"); + return 1; + } + pad_input[strcspn(pad_input, "\n")] = 0; + + // Ask for output format + printf("\nSelect output format:\n"); + printf("1. Binary (.otp) - preserves file permissions\n"); + printf("2. ASCII (.otp.asc) - text-safe format\n"); + printf("Enter choice (1-2): "); + + char format_input[10]; + if (!fgets(format_input, sizeof(format_input), stdin)) { + printf("Error: Failed to read input\n"); + return 1; + } + + int ascii_armor = (atoi(format_input) == 2) ? 1 : 0; + + return encrypt_file(pad_input, input_file, NULL, ascii_armor); +} + +int handle_smart_decrypt(void) { + printf("\n=== Smart Decrypt ===\n"); + printf("Enter encrypted data (paste ASCII armor) or press Enter to browse files:\n"); + + char input_line[MAX_LINE_LENGTH]; + if (!fgets(input_line, sizeof(input_line), stdin)) { + printf("Error: Failed to read input\n"); + return 1; + } + + // Remove newline + input_line[strcspn(input_line, "\n")] = 0; + + if (strlen(input_line) == 0) { + // Empty input - launch file manager + char selected_file[512]; + if (launch_file_manager(".", selected_file, sizeof(selected_file)) != 0) { + printf("Error: Could not launch file manager\n"); + return 1; + } + + // Decrypt selected file + return decrypt_file(selected_file, NULL); + } else if (strncmp(input_line, "-----BEGIN OTP MESSAGE-----", 27) == 0) { + // Looks like ASCII armor - collect the full message + char full_message[MAX_INPUT_SIZE * 4] = {0}; + strcat(full_message, input_line); + strcat(full_message, "\n"); + + printf("Continue pasting the message (end with -----END OTP MESSAGE-----):\n"); + + char line[MAX_LINE_LENGTH]; + while (fgets(line, sizeof(line), stdin)) { + strncat(full_message, line, sizeof(full_message) - strlen(full_message) - 1); + if (strstr(line, "-----END OTP MESSAGE-----")) { + break; + } + } + + return decrypt_text(NULL, full_message); + } else { + // Check if it looks like a file path + if (access(input_line, R_OK) == 0) { + return decrypt_file(input_line, NULL); + } else { + printf("Input not recognized as ASCII armor or valid file path.\n"); + return 1; + } + } +} + void print_usage(const char* program_name) { printf("OTP Cipher - One Time Pad Implementation %s\n", get_version()); printf("%s\n", get_build_info()); diff --git a/tocEncrypted b/tocEncrypted new file mode 100644 index 0000000..aace2de Binary files /dev/null and b/tocEncrypted differ