Skip to content

Commit 1ac11f8

Browse files
committed
Stashing inprogress work
I'm moving to a new laptop and want to save inprogress work. Signed-off-by: Daniel Bevenius <daniel.bevenius@gmail.com>
1 parent 96a833b commit 1ac11f8

4 files changed

Lines changed: 156 additions & 3 deletions

File tree

src/challenge_06.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#define _GNU_SOURCE
2+
#include <stdio.h>
3+
4+
int main(int argc, char** argv) {
5+
printf("Cryptopals Set 1, challenge 6\n");
6+
7+
char* filename = "src/6.txt";
8+
FILE* file = fopen(filename, "r");
9+
if (file == NULL) {
10+
printf("Could not locate file: %s\n", filename);
11+
return -1;
12+
}
13+
char* line = NULL;
14+
size_t len = 0;
15+
int read = 0;
16+
while (((read = getline(&line, &len, file)) != -1)) {
17+
if (line[read-1] == '\n') {
18+
line[read-1] = '\0';
19+
}
20+
printf("%s\n", line);
21+
}
22+
23+
fclose(file);
24+
25+
return 0;
26+
}

src/vigenere_break.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
#include "vigenere.h"
6+
#include "freq.h"
7+
8+
int main(int argc, char** argv) {
9+
char* ciphertext = "tovnwieanoweachfrjrncofbuosqrnfawmcbrnbasepxnfawmeweahooerbhbpyh";
10+
int len = strlen(ciphertext);
11+
printf("ciphertext: %s\n", ciphertext);
12+
printf("len: %d\n", len);
13+
14+
// Count the occurances of letters in the ciphertext.
15+
int shifted_count[26] = {0};
16+
for (int i = 0; i < len; i++) {
17+
int nr = ciphertext[i] - 97;
18+
//printf("nr: %d, %c\n", nr, ciphertext[i]);
19+
shifted_count[nr]++;
20+
}
21+
double shifted_freq[26] = {0.0};
22+
for (int i = 0; i < 26; i++) {
23+
shifted_freq[i] = shifted_count[i] * sampled_freq[i];
24+
printf("%c: %d, %f\n", i + 97, shifted_count[i], shifted_freq[i]);
25+
}
26+
27+
double dot_product = 0.0;
28+
for (int i = 0; i < freq_len; i++) {
29+
dot_product += sampled_freq[i] * sampled_freq[i];
30+
}
31+
printf("standard_freq dot_product: %f\n", dot_product);
32+
33+
dot_product = 0.0;
34+
for (int i = 0; i < freq_len; i++) {
35+
dot_product += shifted_freq[i] * shifted_freq[i];
36+
}
37+
printf("shifted_freq dot_product: %f\n", dot_product);
38+
39+
double combined_dot_product = 0.0;
40+
for (int i = 0; i < freq_len; i++) {
41+
combined_dot_product += sampled_freq[i] * shifted_freq[i];
42+
}
43+
printf("sampled_freq dot shifted_freq: %f\n", combined_dot_product);
44+
45+
return 0;
46+
}

src/vigenere_decrypt.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stdlib.h>
33

44
#include "vigenere.h"
5+
#include "freq.h"
56

67
int main(int argc, char** argv) {
78
if (argc != 3) {
@@ -14,5 +15,55 @@ int main(int argc, char** argv) {
1415
char* plaintext = vigenere_decrypt(ciphertext, keyword);
1516
printf("plaintext: %s\n", plaintext);
1617

18+
double prob_a_and_a = sampled_freq[0] * sampled_freq[0];
19+
printf("%f\n", prob_a_and_a);
20+
21+
double dot_product = 0.0;
22+
for (int i = 0; i < freq_len; i++) {
23+
dot_product += sampled_freq[i] * sampled_freq[i];
24+
}
25+
printf("standard_freq dot_product: %f\n", dot_product);
26+
27+
double s[] = {
28+
0.001988, // x
29+
0.022836, // y
30+
0.000629, // z
31+
0.077432, // a
32+
0.014022, // b
33+
0.026657, // c
34+
0.049208, // d
35+
0.134645, // e
36+
0.025036, // f
37+
0.017007, // g
38+
0.057198, // h
39+
0.062948, // i
40+
0.001268, // j
41+
0.005085, // k
42+
0.037062, // l
43+
0.030277, // m
44+
0.071253, // n
45+
0.073800, // o
46+
0.017513, // p
47+
0.000950, // q
48+
0.061072, // r
49+
0.061263, // s
50+
0.087605, // t
51+
0.030427, // u
52+
0.011137, // v
53+
0.021681, // w
54+
};
55+
56+
dot_product = 0.0;
57+
for (int i = 0; i < freq_len; i++) {
58+
dot_product += s[i] * s[i];
59+
}
60+
printf("shifted freq dot_product: %f\n", dot_product);
61+
62+
double combined_dot_product = 0.0;
63+
for (int i = 0; i < freq_len; i++) {
64+
combined_dot_product += sampled_freq[i] * s[i];
65+
}
66+
printf("sampled_freq dot shifted_freq: %f\n", combined_dot_product);
67+
1768
return 0;
1869
}

src/vigenere_encrypt.c

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,46 @@
11
#include <stdio.h>
22
#include <stdlib.h>
3+
#include <string.h>
34

45
#include "vigenere.h"
56

67
int main(int argc, char** argv) {
7-
if (argc != 3) {
8+
char* plaintext = NULL;
9+
char* keyword = NULL;
10+
11+
if (argc == 3) {
12+
plaintext = argv[1];
13+
keyword = argv[2];
14+
} else if (argc == 2) {
15+
char* line = NULL;
16+
size_t len = 0;
17+
int read = 0;
18+
19+
int size = sizeof(char) * 2048;
20+
plaintext = malloc(size);
21+
int ch;
22+
int count = 0;
23+
while (((read = getline(&line, &len, stdin)) != -1)) {
24+
//printf("count: %d\n", count);
25+
if (count+read > size) {
26+
printf("going to realloc...\n");
27+
plaintext = realloc(plaintext, size+size);
28+
size += size;
29+
}
30+
memcpy(plaintext+count, line, read);
31+
count += read;
32+
}
33+
*(plaintext+count-1) = '\0';
34+
free(line);
35+
//printf("count: %d\n", count);
36+
//printf("%s\n", plaintext);
37+
keyword = argv[1];
38+
} else {
839
printf("Usage: vigenere_encrypt <plaintext> <keyword>\n");
940
exit(-1);
1041
}
1142

12-
char* plaintext = argv[1];
13-
char* keyword = argv[2];
43+
printf("keyword: %s\n", keyword);
1444
char* ciphertext = vigenere_encrypt(plaintext, keyword);
1545
printf("ciphertext: %s\n", ciphertext);
1646

0 commit comments

Comments
 (0)