Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/time.h>
- #define NUM_THREADS 8
- #define THREAD_PERMUTATIONS 2197
- typedef enum { false, true } bool;
- int encode_cipher[1201];
- bool found_correct = false;
- void gen_char_start_list(char *arr, int id) {
- // set first starting char ascii value
- if (id >= 5) {
- arr[0] = (97 + id * 3) + 1;
- } else {
- int s = 97 + (id * 3);
- arr[0] = s;
- }
- // set the second starting char ascii value
- if (id >= 5 && id < 7) {
- arr[1] = (97 + (id - 4) * 6);
- } else if (id == 3 || id == 4 || id == 7) {
- if (id == 7) { id = 3; }
- arr[1] = (97 + id * 6) + 1;
- } else {
- arr[1] = 97 + (id * 6);
- }
- // finally set the third starting char
- if (id == 0) {
- arr[2] = 97;
- } else if (id % 2 == 0) {
- arr[2] = 122;
- } else {
- arr[2] = 109;
- }
- }
- bool is_correct_pass(char *decr_cipher) {
- float num_e, num_o;
- num_e = num_o = 0;
- for (int i = 0; i < 1202; i++) {
- if (decr_cipher[i] == 101) { num_e++; }
- else if (decr_cipher[i] == 111) { num_o++; }
- }
- float e_per = (num_e / 969) * 100.0;
- float o_per = (num_o / 969) * 100.0;
- if (e_per > 11 && e_per < 13) {
- if (o_per > 6 && o_per < 8) {
- return true;
- } else { return false; }
- } else { return false; }
- }
- void *decode(void *thread_id) {
- char chars[3];
- int p = 0, id = (int) thread_id;
- char *decode_cipher = malloc(sizeof(char) * 1202);
- gen_char_start_list(chars, id);
- while(p < THREAD_PERMUTATIONS) {
- if (found_correct) {
- pthread_exit(NULL);
- free(decode_cipher);
- }
- for (int i = 0; i < 1202; i++) {
- char decoded_char = encode_cipher[i] ^ chars[i % 3];
- decode_cipher[i] = decoded_char;
- }
- bool correct = is_correct_pass(decode_cipher);
- if (correct == true) {
- found_correct = true;
- pthread_exit(decode_cipher);
- }
- // if both positions 1 and 2 are at the max reset them to 97 and increment position 0 by 1
- if (chars[1] == 122 && chars[2] == 122) {
- chars[0]++;
- chars[1] = chars[2] = 97;
- } else if (chars[2] == 122) { // if just the last position is at the max, reset and increment pos 1
- chars[1]++;
- chars[2] = 97;
- } else { // just increment the last pos
- chars[2]++;
- }
- p++;
- }
- free(decode_cipher);
- pthread_exit(NULL);
- }
- int main(int argc, char *agrv[]) {
- FILE *fp;
- int rc, k = 0, sum = 0;
- char *token, *final;
- char line[3202];
- void *ptr_return;
- const char *s = ",";
- struct timeval stop, start;
- fp = fopen("cipher.txt", "r");
- pthread_t threads[NUM_THREADS];
- gettimeofday(&start, NULL);
- if(fgets(line, 3203, fp) != NULL) {
- token = strtok(line, s);
- while(token != NULL) {
- encode_cipher[k] = atoi(token);
- k++;
- token = strtok(NULL, s);
- }
- }
- fclose(fp);
- for (long i = 0; i < NUM_THREADS; i++) {
- rc = pthread_create(&threads[i], NULL, decode, (void *)i);
- if (rc) {
- printf("%d", rc);
- exit(-1);
- }
- }
- for (int j = 0; j < NUM_THREADS; j++) {
- pthread_join(threads[j], &ptr_return);
- if (ptr_return != NULL) {
- final = ptr_return;
- for (int x = 0; x < 1201; x++) {
- sum += (int)final[x];
- }
- }
- }
- printf("%d\n", sum);
- free(final);
- gettimeofday(&stop, NULL);
- printf("took %ld seconds\n", stop.tv_sec - start.tv_sec);
- printf("took %d microseconds\n", stop.tv_usec - start.tv_usec);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement