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
| #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h>
#define MAX_LEN 1024 #define FLAG_LEN 30
void upkeep() { setvbuf(stdin, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); }
void print_hex(char* buf, int len) { for (int i=0; i<len; i++) { printf("%02x", (unsigned char)buf[i]); } printf("\n"); }
void encrypt(char* msg, int len, char* iv) { char key[len]; FILE *file; file = fopen("/dev/urandom", "rb"); fread(key, len, 1, file); fclose(file);
for (int i=0; i<len; i++) { msg[i] = msg[i] ^ key[i] ^ iv[i % 8]; } }
void randomize(char* msg, int len, unsigned long seed, unsigned long iterations) { seed = seed * iterations + len;
if (iterations > 1) { randomize(msg, len, seed, iterations- 1); } else { encrypt(msg, len, ((char *) &seed)); } }
char menu() { char option; printf("\nSelect from the options below:\n"); printf("1. Encrypt a message\n"); printf("2. View the encrypted message\n"); printf("3. Quit\n"); printf("> "); scanf("%c", &option); while (getchar() != '\n'); return option; }
void console() { FILE* file; long seed; int index; int read_len; char buf[MAX_LEN] = ""; int len = -1;
while (1) { switch(menu()) { case '1': printf("\nPlease enter message to encrypt: "); fgets(buf, MAX_LEN-FLAG_LEN, stdin); len = strlen(buf);
file = fopen("flag.txt", "rb"); fread(buf + len, FLAG_LEN, 1, file); fclose(file); len += FLAG_LEN;
seed = ((long) rand()) << 32 + rand(); randomize(buf, len, seed, buf[0]); break; case '2': if(len == -1) { printf("Sorry, you need to encrypt a message first.\n"); break; }
index = 0; printf("\nRead a substring of the encrypted message."); printf("\nPlease enter the starting index (%d - %d): ", index, len); scanf("%d", &index); while (getchar() != '\n');
if (index > len) { printf("Error, index out of bounds.\n"); break; }
printf("Here's your encrypted string with the flag:\n"); print_hex(buf+index, len - index); break; case '3': printf("goodbye.\n"); exit(0); default: printf("There was an error processing that request, please try again.\n"); break; } }
}
int main() { srand(time(NULL)); upkeep();
printf("Welcome to my encryption engine: ENCRYPT-INATOR!\n"); printf("I'll encrypt anything you want but no guarantees you'll be able to decrypt it,\n"); printf("I haven't quite figured out how to do that yet... :(\n"); console(); }
|