,   CyberForum.ru

- C++

 
:  :  - 40,   - 4.78
san4es
0 / 0 / 0
: 25.01.2010
: 4
26.01.2010, 02:11     #1
,
C++
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Kaziski.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include "unidef.h"
 
 
//int _tmain(int argc, _TCHAR* argv[])
//{
//  return 0;
//}
// 
typedef struct 
{
long values[40][40];
long total;
}
BIGRAMM;
#include "etalon.h"
BIGRAMM current;
 
//   (   )
//          
unsigned char alpha[33] = {'','','','','','','','','','','','',
                           '','','','','','','','','','','','','','','','','',
                           '','','',''};
 
// -  
unsigned char key[33] = {'','','','','','','','','','','','','',
                         '','','','','','','','','','','','','','','','',
                         '','','',''};
// -  
unsigned char right_unkey[33] = {'','','','','','','','','','','',
                                 '','','','','','','','','','','','','','','',
                                 '','',
                                 '','','','',''};
unsigned char unkey[33] = {'','','','','','','','','','','','','','',
                           '','','','','','','','','','','','','','','',
                           '','','',''};
 
//   
void encrypt(unsigned char *buf, int len, char *key) {
for (int i = 0; i < len; i++)
if (buf[i] >= 0xC0 && buf[i] <= 0xE0) buf[i] = key[buf[i]-0xC0];
}
 
void decrypt(unsigned char *buf, int len, char *key) {
for (int i = 0; i < len; i++)
if (buf[i] >= 0xC0 && buf[i] <= 0xE0) buf[i] = unkey[buf[i]-0xC0];
}
 
//  
void calc_gramm(unsigned char *buf, int len) {
for (int i = 0; i < len-1; i++) {
current.values[ buf[i]-0xC0 ][ buf[i+1]-0xC0 ]++;
current.total++;
}
}
//      
double calc_measure() 
{
double result = 0;
 
if (!current.total) current.total++;
for (int i = 0; i < 40; i++)
for (int j = 0; j < 40; j++)
result += fabs(((double) current.values[i][j]/current.total) -
((double) etalon.values[i][j]/etalon.total));
 
return result;
}
 
//  
void findkey_jakobsen(char *buf, int len, char *guesskey) {
char work_key[33];
double V, V_;
int c, pos, sym;
char *temp, s;
 
temp = (char *) malloc(len);
memcpy(work_key, guesskey, sizeof(work_key));
strncpy(temp, buf, len);
memset(&current, 0, sizeof(current));
calc_gramm(temp, len);
V = calc_measure();
randomize();
c = 0;
for (;;) {
//    
pos = rand() % 33;
sym = (pos + 1 + rand()) % 33;
s = work_key[pos];
work_key[pos] = work_key[sym];
work_key[sym] = s;
 
//  
strncpy(temp, buf, len);
decrypt(temp, len, work_key);
memset(&current, 0, sizeof(current));
calc_gramm(temp, len);
 
//    
V_ = calc_measure();
if (++c % 1000 == 0) printf("%f < %f, %d, %d\n", V_, V, pos, sym);
if (V_ < V) {
// !  !
printf("WORK_KEY == %s, KEY = %s (V == %f, V_ == %f)",
work_key, guesskey, V, V_);
printf("!!!\n", work_key);
memcpy(guesskey, work_key, sizeof(work_key));
V = V_;
}
else
//    
memcpy(work_key, guesskey, sizeof(work_key));
}
printf("%f", V);
}
 
#define MAKE_ETALON 0
int main(int argc, char* argv[])
{
char text[] = "     ,  "
"  ,   .   "
"     , "
"       ("
"     ).  "
"      "
"    .   "
"  (- ),     "
" ,      "
"   .";
int len = strlen(text);
for (int i = 0; i < len; i++)
if (text[i] < '' || text[i] > '') text[i] = 0xE0;
 
//  
if (MAKE_ETALON) {
FILE *f = fopen("collect.txt", "rb");
char buf[32768];
while (!feof(f)) {
int r = fread(buf, 1, sizeof(buf), f);
for (int i = 0; i < r; i++) if (buf[i] < '' || buf[i] > '')
buf[i] = 0xE0;
calc_gramm(buf, r);
}
fclose(f);
f = fopen("etalon.h", "wt");
fprintf(f, "BIGRAMM etalon = {\n");
for (int i = 0; i < 40; i++) {
for (int j = 0; j < 40; j++)
fprintf(f, "%d ", current.values[i][j]);
fprintf(f, "\n");
}
fprintf(f, "%d };\n", current.total);
fclose(f);
return 0;
}
 
encrypt(text, len, key);
printf("%s\n", text);
 
findkey_jakobsen(text, len, unkey);
 
decrypt(text, len, unkey);
printf("%s\n", text);
 
return 0;
}
, RSA, Kardano,Vigenera.
Similar
41792 / 34177 / 6122
: 12.04.2006
: 57,940
26.01.2010, 02:11    
:

Delphi
Delphi
Delphi
Delphi
Delphi
Delphi
Delphi Delphi
Delphi !



:
.
 
0 / 0 / 0
: 08.04.2011
: 30
08.04.2011, 12:44     #2
- " RSA, Kardano,Vigenera"
! , !
Yandex
08.04.2011, 12:44    

: 07:07. GMT +3.
- , ,
-
Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2016, vBulletin Solutions, Inc.
@Mail.ru