Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 206e8042d8 | |||
| 2a5249d93c | |||
| 0e02eaee53 |
178
otp.c
178
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();
|
||||
@@ -142,10 +148,11 @@ int interactive_mode(void) {
|
||||
break;
|
||||
}
|
||||
case 'X':
|
||||
case 'Q':
|
||||
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 {
|
||||
@@ -261,10 +268,11 @@ int command_line_mode(int argc, char* argv[]) {
|
||||
}
|
||||
|
||||
void show_main_menu(void) {
|
||||
printf("=== Main Menu ===\n");
|
||||
printf("=== Main Menu ===\n\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 +2481,158 @@ 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;
|
||||
|
||||
// Generate default output filename
|
||||
char default_output[512];
|
||||
if (ascii_armor) {
|
||||
snprintf(default_output, sizeof(default_output), "%s.otp.asc", input_file);
|
||||
} else {
|
||||
snprintf(default_output, sizeof(default_output), "%s.otp", input_file);
|
||||
}
|
||||
|
||||
// Ask for output filename with pre-filled default
|
||||
printf("\nOutput filename [%s]: ", default_output);
|
||||
char output_file[512];
|
||||
if (!fgets(output_file, sizeof(output_file), stdin)) {
|
||||
printf("Error: Failed to read input\n");
|
||||
return 1;
|
||||
}
|
||||
output_file[strcspn(output_file, "\n")] = 0;
|
||||
|
||||
// Use default if user just pressed Enter
|
||||
const char* output_filename = (strlen(output_file) > 0) ? output_file : default_output;
|
||||
|
||||
return encrypt_file(pad_input, input_file, output_filename, 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());
|
||||
|
||||
BIN
test.txt.otp
Normal file
BIN
test.txt.otp
Normal file
Binary file not shown.
BIN
tocEncrypted
Normal file
BIN
tocEncrypted
Normal file
Binary file not shown.
Reference in New Issue
Block a user