Форум программистов, компьютерный форум, киберфорум
Наши страницы

Запустить исходник Диффи-Хеллмана - C++

Войти
Регистрация
Восстановить пароль
Другие темы раздела
C++ С чего начинать? http://www.cyberforum.ru/cpp-beginners/thread1016967.html
Помогите пожалуйста. Я в С++ полный ноль. С чего начинать. Требуется определить, пересекаются ли два интервала числовой оси с известными координатами концов: и , для чего определить функцию Yes....
C++ Задача по одномерному массиву Доброго времени суток! Прошу помочь с решением вот такой задачи: С клавиатуры ввести одномерный массив, найти минимальный элемент массива, а затем вывести на печать только те элементы, которые... http://www.cyberforum.ru/cpp-beginners/thread1016952.html
C++ Реализация метода спирального координатного спуска(теория внутри)
На вход программе должны подаваться 8 значений-координаты городов. она должна считать где между этими городами будет железнодорожная развилка, так, чтобы протяженность дорог до городов была...
C++ Подскажите формулу расчета координат перемещения объекта в 2d пространстве
Разрабатываю 2d игру на javascript. Хотел бы прокачаться в линейной алгебре. Думаю в этой ветке я быстрее найду ответы на свои вопросы чем на подфорумах веб-разработки Вопрос такой: Некий...
C++ код перебросить на делфи http://www.cyberforum.ru/cpp-beginners/thread1016919.html
#define NIBBLE2HEX(c) ((c) > 9 ? (c) + 'A' - 10 : (c) + '0') > int checksum = 0; > int inx; > for(inx = 1; inx < buf_inx; inx++) > { > checksum ^= buf; > } > buf = '*'; > buf =...
C++ Таймер То-ли я кривой, то-ли лыжы не едут. Чё за фигня? Таймер не работает... POINT previousPoint, currentPoint; bool mouseDown; HPEN myPen; int redValue, greenValue, blueValue; COLORREF pixelColor;... подробнее

Показать сообщение отдельно
Жасулан
1 / 1 / 0
Регистрация: 22.10.2013
Сообщений: 68

Запустить исходник Диффи-Хеллмана - C++

24.11.2013, 10:47. Просмотров 2268. Ответов 3
Метки (Все метки)

вот код реализации диффи хеллмана,но я не могу запустить ее: помогите разобратся))
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* dh.c
* Example source file for D-H algorithm in key exchange
* Example fields: IPsec, MG<->MGC authentication
* Example usage: ./dh 10000 8888 523
* dhKey = g^a mod(p)
*      g – unsigned long, like 2-4294967295
*      a – unsigned int, like 2-65535
*      p – unsigned int, prime number, like 2-65535
* Build cmd: gcc -o dh -lm dh.c
* Input sequence: g a p
* 2011/04/20
* [email]dave.tian@alcatel-lucentc.om[/email]
* [url]http://daveti.blog.com[/url]
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/times.h>
#include <unistd.h>
unsigned long dh1( unsigned long, unsigned int, unsigned int);
unsigned long dh2( unsigned long, unsigned int, unsigned int);
int main( int argc, char **argv)
{
if ( argc != 4)
{
printf(“Error\n”);
return 1;
}
// Get the input from cli
unsigned long g = strtoul( argv[ 1], NULL, 10);
unsigned int a = strtoul( argv[ 2], NULL, 10);
unsigned int p = strtoul( argv[ 3], NULL, 10);
printf(“g = %lu\na = %u\np = %u\n”, g, a, p);
// Check if p is a prime number
// C++ may have a function called isPrime? – not sure of that:)
unsigned int i = 0;
unsigned int r = (unsigned int)sqrt( p);
for( i = 2; i <= r; i++)
{
if ( p%i == 0)
{
printf(“‘p’ is not a prime: %u\n”, p);
return 1;
}
}
// D-H algorithm: g^a mod(p);
// Time measurement could be added here to compare
// the different performance between the 2 implementations
// However, memory consideration comparison is missed here
// between the 2 functions…Personally I assume recurrsion
// function should consume more dynamic memory because of
// large number of stacks, which may cause memory overflow.
unsigned long dhKey = 0;
struct tms time_buf_head, time_buf_end;
clock_t time_head, time_end;
long tck = sysconf (_SC_CLK_TCK);
time_head = times( &time_buf_head );
dhKey = dh1( g, a, p);
time_end = times( &time_buf_end );
printf(“dh1(), user time: %f, sys time: %f, child user time: %f, child sys time: %f\n”,
((time_buf_end.tms_utime – time_buf_head.tms_utime) / (double)tck),
((time_buf_end.tms_stime – time_buf_head.tms_stime) / (double)tck),
((time_buf_end.tms_cutime – time_buf_head.tms_cutime) / (double)tck),
((time_buf_end.tms_cstime – time_buf_head.tms_cstime) / (double)tck) );
printf(“dh1 = %lu\n”, dhKey);
time_head = times( &time_buf_head );
dhKey = dh2( g, a, p);
time_end = times( &time_buf_end );
printf(“dh2(), user time: %f, sys time: %f, child user time: %f, child sys time: %f\n”,
((time_buf_end.tms_utime – time_buf_head.tms_utime) / (double)tck),
((time_buf_end.tms_stime – time_buf_head.tms_stime) / (double)tck),
((time_buf_end.tms_cutime – time_buf_head.tms_cutime) / (double)tck),
((time_buf_end.tms_cstime – time_buf_head.tms_cstime) / (double)tck) );
printf(“dh2 = %lu\n”, dhKey);
return 0;
}
unsigned long dh1( unsigned long g, unsigned int a, unsigned int p)
{
// This is a recurssion version of D-H algorithm
// implemented by daveti on 2011/04/20
if ( a == 0)
{
printf(“Error\n”);
return 0;
}
if ( a == 1)
{
return g%p;
}
// (a*b)%c == ((a%c)*(b%c))%c
return ((g%p)*(dh1( g, a-1, p)))%p;
}
unsigned long dh2( unsigned long g, unsigned int a, unsigned int p)
{
// This is non-recurssion version of D-H algorithm
// implemented by daveti on 2011/04/21
// g^4%p = (g%p*((g%p*((g%p*(g%p))%p))%p))%p
// let z = g%p
// g^4%p = (z*((z*((z*z)%p))%p))%p
//       = (((((z*z)%p)*z)%p)*z)%p
if ( a == 0)
{
printf(“Error\n”);
return 0;
}
unsigned long z = g%p;
if ( a == 1)
{
return z;
}
unsigned int idx = 0;
unsigned long tmp = z;
for ( idx = 2; idx <= a; idx++)
{
tmp = (tmp*z)%p;
}
return tmp;
}
/*
* dh.c
* Example source file for D-H algorithm in key exchange
* Example fields: IPsec, MG<->MGC authentication
* Example usage: ./dh 10000 8888 523
* dhKey = g^a mod(p)
*      g – unsigned long, like 2-4294967295
*      a – unsigned int, like 2-65535
*      p – unsigned int, prime number, like 2-65535
* Build cmd: gcc -o dh -lm dh.c
* Input sequence: g a p
* 2011/04/20
* [email]dave.tian@alcatel-lucentc.om[/email]
* [url]http://daveti.blog.com[/url]
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/times.h>
#include <unistd.h>
unsigned long dh1( unsigned long, unsigned int, unsigned int);
unsigned long dh2( unsigned long, unsigned int, unsigned int);
int main( int argc, char **argv)
{
if ( argc != 4)
{
printf(“Error\n”);
return 1;
}
// Get the input from cli
unsigned long g = strtoul( argv[ 1], NULL, 10);
unsigned int a = strtoul( argv[ 2], NULL, 10);
unsigned int p = strtoul( argv[ 3], NULL, 10);
printf(“g = %lu\na = %u\np = %u\n”, g, a, p);
// Check if p is a prime number
// C++ may have a function called isPrime? – not sure of that:)
unsigned int i = 0;
unsigned int r = (unsigned int)sqrt( p);
for( i = 2; i <= r; i++)
{
if ( p%i == 0)
{
printf(“‘p’ is not a prime: %u\n”, p);
return 1;
}
}
// D-H algorithm: g^a mod(p);
// Time measurement could be added here to compare
// the different performance between the 2 implementations
// However, memory consideration comparison is missed here
// between the 2 functions…Personally I assume recurrsion
// function should consume more dynamic memory because of
// large number of stacks, which may cause memory overflow.
unsigned long dhKey = 0;
struct tms time_buf_head, time_buf_end;
clock_t time_head, time_end;
long tck = sysconf (_SC_CLK_TCK);
time_head = times( &time_buf_head );
dhKey = dh1( g, a, p);
time_end = times( &time_buf_end );
printf(“dh1(), user time: %f, sys time: %f, child user time: %f, child sys time: %f\n”,
((time_buf_end.tms_utime – time_buf_head.tms_utime) / (double)tck),
((time_buf_end.tms_stime – time_buf_head.tms_stime) / (double)tck),
((time_buf_end.tms_cutime – time_buf_head.tms_cutime) / (double)tck),
((time_buf_end.tms_cstime – time_buf_head.tms_cstime) / (double)tck) );
printf(“dh1 = %lu\n”, dhKey);
time_head = times( &time_buf_head );
dhKey = dh2( g, a, p);
time_end = times( &time_buf_end );
printf(“dh2(), user time: %f, sys time: %f, child user time: %f, child sys time: %f\n”,
((time_buf_end.tms_utime – time_buf_head.tms_utime) / (double)tck),
((time_buf_end.tms_stime – time_buf_head.tms_stime) / (double)tck),
((time_buf_end.tms_cutime – time_buf_head.tms_cutime) / (double)tck),
((time_buf_end.tms_cstime – time_buf_head.tms_cstime) / (double)tck) );
printf(“dh2 = %lu\n”, dhKey);
return 0;
}
unsigned long dh1( unsigned long g, unsigned int a, unsigned int p)
{
// This is a recurssion version of D-H algorithm
// implemented by daveti on 2011/04/20
if ( a == 0)
{
printf(“Error\n”);
return 0;
}
if ( a == 1)
{
return g%p;
}
// (a*b)%c == ((a%c)*(b%c))%c
return ((g%p)*(dh1( g, a-1, p)))%p;
}
unsigned long dh2( unsigned long g, unsigned int a, unsigned int p)
{
// This is non-recurssion version of D-H algorithm
// implemented by daveti on 2011/04/21
// g^4%p = (g%p*((g%p*((g%p*(g%p))%p))%p))%p
// let z = g%p
// g^4%p = (z*((z*((z*z)%p))%p))%p
//       = (((((z*z)%p)*z)%p)*z)%p
if ( a == 0)
{
printf(“Error\n”);
return 0;
}
unsigned long z = g%p;
if ( a == 1)
{
return z;
}
unsigned int idx = 0;
unsigned long tmp = z;
for ( idx = 2; idx <= a; idx++)
{
tmp = (tmp*z)%p;
}
return tmp;
}
 
[size="1"][color="grey"][I]Добавлено через 13 минут[/I][/color][/size]
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/times.h>
#include <unistd.h>
unsigned long dh1( unsigned long, unsigned int, unsigned int);
unsigned long dh2( unsigned long, unsigned int, unsigned int);
int main( int argc, char **argv)
{
if ( argc != 4)
{
printf(“Error\n”);
return 1;
}
// Get the input from cli
unsigned long g = strtoul( argv[ 1], NULL, 10);
unsigned int a = strtoul( argv[ 2], NULL, 10);
unsigned int p = strtoul( argv[ 3], NULL, 10);
printf(“g = %lu\na = %u\np = %u\n”, g, a, p);
// Check if p is a prime number
// C++ may have a function called isPrime? – not sure of that:)
unsigned int i = 0;
unsigned int r = (unsigned int)sqrt( p);
for( i = 2; i <= r; i++)
{
if ( p%i == 0)
{
printf(“‘p’ is not a prime: %u\n”, p);
return 1;
}
}
// D-H algorithm: g^a mod(p);
// Time measurement could be added here to compare
// the different performance between the 2 implementations
// However, memory consideration comparison is missed here
// between the 2 functions…Personally I assume recurrsion
// function should consume more dynamic memory because of
// large number of stacks, which may cause memory overflow.
unsigned long dhKey = 0;
struct tms time_buf_head, time_buf_end;
clock_t time_head, time_end;
long tck = sysconf (_SC_CLK_TCK);
time_head = times( &time_buf_head );
dhKey = dh1( g, a, p);
time_end = times( &time_buf_end );
printf(“dh1(), user time: %f, sys time: %f, child user time: %f, child sys time: %f\n”,
((time_buf_end.tms_utime – time_buf_head.tms_utime) / (double)tck),
((time_buf_end.tms_stime – time_buf_head.tms_stime) / (double)tck),
((time_buf_end.tms_cutime – time_buf_head.tms_cutime) / (double)tck),
((time_buf_end.tms_cstime – time_buf_head.tms_cstime) / (double)tck) );
printf(“dh1 = %lu\n”, dhKey);
time_head = times( &time_buf_head );
dhKey = dh2( g, a, p);
time_end = times( &time_buf_end );
printf(“dh2(), user time: %f, sys time: %f, child user time: %f, child sys time: %f\n”,
((time_buf_end.tms_utime – time_buf_head.tms_utime) / (double)tck),
((time_buf_end.tms_stime – time_buf_head.tms_stime) / (double)tck),
((time_buf_end.tms_cutime – time_buf_head.tms_cutime) / (double)tck),
((time_buf_end.tms_cstime – time_buf_head.tms_cstime) / (double)tck) );
printf(“dh2 = %lu\n”, dhKey);
return 0;
}
unsigned long dh1( unsigned long g, unsigned int a, unsigned int p)
{
// This is a recurssion version of D-H algorithm
// implemented by daveti on 2011/04/20
if ( a == 0)
{
printf(“Error\n”);
return 0;
}
if ( a == 1)
{
return g%p;
}
// (a*b)%c == ((a%c)*(b%c))%c
return ((g%p)*(dh1( g, a-1, p)))%p;
}
unsigned long dh2( unsigned long g, unsigned int a, unsigned int p)
{
// This is non-recurssion version of D-H algorithm
// implemented by daveti on 2011/04/21
// g^4%p = (g%p*((g%p*((g%p*(g%p))%p))%p))%p
// let z = g%p
// g^4%p = (z*((z*((z*z)%p))%p))%p
//       = (((((z*z)%p)*z)%p)*z)%p
if ( a == 0)
{
printf(“Error\n”);
return 0;
}
unsigned long z = g%p;
if ( a == 1)
{
return z;
}
unsigned int idx = 0;
unsigned long tmp = z;
for ( idx = 2; idx <= a; idx++)
{
tmp = (tmp*z)%p;
}
return tmp;
}
0
Надоела реклама? Зарегистрируйтесь и она исчезнет полностью.
 
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Рейтинг@Mail.ru