-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvigenere_analysis.cpp
85 lines (77 loc) · 3.03 KB
/
vigenere_analysis.cpp
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
#include <stdio.h>
#include <string.h>
#include <malloc.h>
const char *C = "UOUQOEOOMFOOFTAMTVTTAQQHFSMUOAXHXFHRSTBENBCLXDIAUHXYIAETHEUSGEKFHRTLBZGFBNWMREPWLAFBVTKMGRPULROEUUGQOEUOMMKRBRFEATBIGETNTETAFGSONNLRTAGPBLPPIASVOGXZDGIEFFOQJEMASYFEIZOZPRXMNQCYTELRFPMASNZWXQNQUHXTENSTTOHRBNWFHRUHHGSNODGMTHSAEEHBDKLFHNUFEQSUJSAQIEUOMUSNDOGEUZNAMUOAEEOAUGMYMABRXILTEQUOWUEGPSEQECUOLXERQPXDCUBNVQTBERXMMNZEMTEEFSMTEEVBYARVOTAMTFMEXBOSEETFHJIAMPRRBMLYALDOFQWUFNPQHNWELTUSGLXPOSGTAUSZPRMMLPPIEYUFUGBHEHTPTGSRUHXDEFUHXDEFQEVFTUBTFMKRTCTXAZJTRAFFPLHZGYJFXROEXHHIOHMDUQAEUHXIHVQSTZDFDOKZSBGTBYEGIEHBPEFSLARFXRHZGGIEIDOHEMTZSPPNMGMRMYMTECBNZEOSEELBIFFDEAVRUHXXAJTDXXALUHXUNFPLXZCRPFHRFVDETZDGIELBUEOSMTAGQAMUEAUMXDIGPFMTEHOWHDTUZTTWEFXHXZHRIIFEEYGMBSHGIILCUVFTNEMNLEPUTUBBTDEOPDDUNJIOPAUYEFTDDRMSUQAEUOZDUAUAGPSJFAMGNQFRTIENSYEUFRCUMFHNUTAQDEFAWAFFPMXFHVOGTRTRSDXMTUUHXGNQJSVAVRSEWOOHOTKKFEPMPTOFFBHGRAOOMDAIFLEQREFTNDNFQUSLLRTTAQWVMLTZDZBKXEUFSAMTEECETDTUPSXULYTWXTAIFTAMNSMYMAOGIEKETUBTPQKAPWGATBGTAGSPPNLOIROCXPORTMTWEPPWTDDFPFNEAYMAGPTUVSMTEABTBHEUVEHRRRTOEGTVPNBESVDKEUEQPEKIIGITAQPNMEVMSGPFMTOHHHMMNQFNMQRCSILQSBGGKQAGQIMOHNODFAMROTPUTUUHBERRHAKPTUFIKOUESEGFSGVRGMWEZAGPLBTEMTEABMXAFNDTBANFPFMKOHOOPFHRGABDOCIEEUAAZMITIAUHRARVTOGEBRBLEYYFJNLDEZFMUQRRE";
int C_len = strlen(C);
// 按密钥长度分割密文
char **get_m(int key_len)
{
char **messages = (char **)malloc(key_len * sizeof(char *));
for (int i = 0; i < key_len; i++)
{
messages[i] = (char *)malloc((C_len / key_len + 1) * sizeof(char));
memset(messages[i], 0, (C_len / key_len + 1) * sizeof(char));
}
for (int i = 0; i < C_len; i++)
{
messages[i % key_len][i / key_len] = C[i];
}
return messages;
}
void destroy(char **messages)
{
free(messages);
}
// 统计各字母出现次数
int *count(char *message)
{
int *cnt = (int *)malloc(sizeof(int) * 26);
memset(cnt, 0, sizeof(int) * 26);
for (int i = 0; i < strlen(message); i++)
{
cnt[message[i] - 'A']++;
}
return cnt;
}
// 计算重合指数
double calculate_index(char *message)
{
int *cnt = count(message);
int n = strlen(message);
int sum = 0;
for (int i = 0; i < 26; i++)
{
sum += cnt[i] * (cnt[i] - 1);
}
return (double)sum / (n * (n - 1));
}
// 输出字符串中各字母出现次数
void printcount(char *message)
{
int *cnt = count(message);
int n = strlen(message);
for (int i = 0; i < 26; i++)
{
printf("%c:%.3lf ", i + 'A', (double)cnt[i] / n);
}
printf("\n");
}
// 打印结果:重合指数
void printpart(int key_len, char **messages)
{
printf("分割后子串:\n");
for (int i = 0; i < key_len; i++)
{
printf("%d\n", i + 1, messages[i], calculate_index(messages[i]));
// printf("%s\n重合指数:%.5lf\n", messages[i], calculate_index(messages[i])); // 打印重合指数
printcount(messages[i]); // 打印字母频数
}
}
int main()
{
int key_len = 6;
char **messages = get_m(key_len);
printpart(key_len, messages);
destroy(messages);
return 0;
}