|
| 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 | +} |
0 commit comments