/* * this is the actual interesting part. * this is top secret stuff uwu * just don't touch and you'll be fine (REMOVE ALL MAGIC NUMBERS) */ #include #include #include #include "crypt.h" #define ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=/+[]{}|.,;:'\" \\" void shuffle(char* array, int seed) { int n = strlen(array); srand(seed); for (int k = 0; k <= 3; k++) { srand(seed + k); for (int i = n - 1; i > 0; i--) { int j = rand() % (i + 1); char temp = array[i]; array[i] = array[j]; array[j] = temp; } } } void unshuffle(char* array, int seed) { int n = strlen(array); for (int k = 3; k >= 0; k--) { srand(seed + k); int swaps[n-1]; for (int i = n - 1; i > 0; i--) { int j = rand() % (i + 1); swaps[n-1-i] = j; } for (int i = 1; i < n; i++) { int j = swaps[i-1]; char tmp = array[i]; array[i] = array[j]; array[j] = tmp; } } } int get_index(char c) { char* alphabet = ALPHABET; for (int i = 0; alphabet[i] != '\0'; i++) { if (alphabet[i] == c) return i; } return -1; } int get_int_from_char(char* array) { int hash = 0; while (*array) { hash = (hash * 31) + (int)(*array); array++; } return hash; } char* mix_right(int amount, char* text) { char* temp = malloc(strlen(text) + 1); char* temp2 = malloc(strlen(text) + 1); if (temp == NULL || temp2 == NULL) { fprintf(stderr, "ALLOC FAILED, HALTING PROGRAM\n"); exit(3); } strcpy(temp, text); strcpy(temp2, text); int len = strlen(temp); amount = amount % len; for (int i = 0; i < amount; i++) { for (int j = 0; j < len; j++) { if (j - 1 < 0) { temp[j] = temp2[len - 1]; continue; } temp[j] = temp2[j - 1]; } strcpy(temp2, temp); } free(temp); return temp2; } char* decrypt(char* key, char* ciphertext) { return ""; } char* encrypt(char* key, char* plaintext) { int len = strlen(plaintext); int keylen = strlen(key); /* set alphabet */ char alphabet[strlen(ALPHABET) + 1]; strcpy(alphabet, ALPHABET); char* mixed_alphabet = mix_right(len, alphabet); shuffle(mixed_alphabet, get_int_from_char(alphabet) + keylen); strcpy(alphabet, mixed_alphabet); printf("ALPHABET:\n%s\n", alphabet); free(mixed_alphabet); /* init temp + jumble text a lil' */ char* temp_key = malloc(len + 1); char* temp = malloc(len + 1); char* jumbled_key = mix_right(keylen * 20 - keylen % 3, key); shuffle(jumbled_key, keylen); char* jumbled_text = mix_right(keylen * 3, plaintext); printf("%s, %s\n", jumbled_key, jumbled_text); strcpy(temp_key, jumbled_key); strcpy(temp, jumbled_text); free(jumbled_key); free(jumbled_text); for (int i = 0; temp_key[i] != '\0'; i++) { int index = get_index(temp_key[i]); printf("%d\n", index); temp_key[i] = alphabet[index]; } char* temp1 = alphabet; for (int counter = 0; temp[counter] != '\0'; counter++) { int index = get_index(temp[counter]); printf("%d\n", index); temp[counter] = alphabet[index]; temp1 = malloc(strlen(alphabet)); temp1 = mix_right(index + ((201 * index) % 7), temp1); strcpy(alphabet, temp1); free(temp1); printf("%s\n", alphabet); } /* give responsibility to caller */ char* encrypted = malloc(len + 1); strcpy(encrypted, temp); /* free malloc'd vars */ free(temp_key); free(temp); return encrypted; }