Быстрая схема аутентификации и обмена ключами, устойчивая к DDoS-атаке

Дипломная работа - Компьютеры, программирование

Другие дипломы по предмету Компьютеры, программирование




ot;,savg[1]/counter[1]);[5]+=savg[1]/counter[1];[5]++;[1]=0;[1]=0;(counter[5]>10){(log2,"\n\nlegal down client time AVG= %f\n\n",savg[5]/counter[5]);[5]=0;[5]=0;

}

}

}_connect--;( &cs4 );

_endthreadex( 0 );0;

}{= rdtsc ();_tik=t2-t1;_time=cur_tik/tik_per_second*1000;(log,"\nfailed SIGN %d %d %f ms\n",paflag,num_connect, cur_time);_replay(buff,0,session_key2);(sock1,buff,22,0);(closesocket(sock1)!=0){(log1,"close sock1 eror (failed sign)");

}( &cs4 );(cur_time10){(log2,"\n\nfailed SIGN client time AVG= %f\n\n",savg[6]/counter[6]);[6]=0;[6]=0;

}

}_connect--;( &cs4 );

_endthreadex( 0 );0;

}

}{= rdtsc ();_tik=t2-t1;_time=cur_tik/tik_per_second*1000;(log,"\nfailed PKC %d %d %f ms\n",paflag,num_connect, cur_time);_replay(buff,0,session_key2);(sock1,buff,22,0);(closesocket(sock1)!=0){(log1,"close sock1 eror (failed PKC)");

}( &cs4 );(cur_time10){(log2,"\n\nfailed PKC client time AVG= %f\n\n",savg[7]/counter[7]);[7]=0;[7]=0;

}

}_connect--;( &cs4 );

_endthreadex( 0 );0;

}

}{(log1,"\nRECV from client failed\n sock1=%d\n",sock1);(closesocket(sock1)!=0){(log1,"\nclose sock1 eror (failed PKC)\n");

}( &cs4 );_connect--;( &cs4 );

_endthreadex( 0 );0;

}

}_tmain(void)

{client_sock, listener;sockaddr_in addr, client_addr;count=0;threadID;buf[1024];();( &cs1 );( &cs2 );( &cs3 );( &cs4 );( WSAStartup(0x202,(WSADATA*)&buf[0])){("WSAStart error %d\n", WSAGetLastError());(log,"WSAStart error %d\n", WSAGetLastError());-1;

}=socket(AF_INET, SOCK_STREAM, 0);(listener < 0){("WSAStart error %d\n", WSAGetLastError());(log1,"WSAStart error %d\n", WSAGetLastError());-1;

}.sin_family=AF_INET;.sin_port=htons(PORT);.sin_addr.s_addr=INADDR_ANY;(bind(listener, (struct sockaddr*)&addr, sizeof(addr))<0){("WSAStart error %d\n", WSAGetLastError());(log1,"WSAStart error %d\n", WSAGetLastError());-1;

}("bind OK\n");(listener, 0x100);

client_addr_size=sizeof(client_addr);

(client_sock=accept(listener, (sockaddr *)&client_addr, &client_addr_size)){++;("\nNEW CONNECTION %d\n",count);( &cs4 );_connect++;( &cs4 );

_beginthreadex( NULL, 0, &servprotocol, &client_sock, 0, &threadID );(config_param[0]);

}("\n\n\n\tSOCK ACCEPT ERROR!!!\n");(log1,"\n\n\n\tSOCK ACCEPT ERROR!!!\n");(30000);(client_sock);(listener);0;

}

// full_d_tcp_client.cpp : Defines the entry point for the console application.

// client part

#include "stdafx.h"

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#pragma comment (lib,"libeay32")

#pragma comment (lib,"ssleay32")

#pragma comment (lib,"wsock32");

#define BUFSIZE 1024

#define BUFFSIZE (1025*16)

#define PRIVAT_CLIENT_KEY "privat_client.key"

#define PUBLIC_CLIENT_KEY "public_client.key"

#define PUBLIC_SERVER_KEY "public_server.key"

#define IDauthkey_file "auth.id"

#define authkey_file "auth.key"

#define password "hello"

#define KEYSIZE 32

#define RSA_key_size 128

PORT=3425;SERVERADDR[]="178.49.183.9";config_flags[10], config_param[10];

namespace std;

client_ini(){*ini=NULL;=fopen("conin","rb");(ini,"%d",&PORT);(ini,"%s",&SERVERADDR);(ini,"%d",&config_flags[0]);(ini,"%d",&config_flags[1]);(ini,"%d",&config_flags[2]);(ini,"%d",&config_param[0]);

0;

}md5(unsigned char *str, unsigned char *md_value, int strlen){_MD_CTX mdctx;EVP_MD * md;int md_len;

_add_all_digests();

= EVP_get_digestbyname("md5");_DigestInit(&mdctx, md);

_DigestUpdate(&mdctx, str, strlen);

_DigestFinal(&mdctx, md_value, &md_len);

_MD_CTX_cleanup(&mdctx);

1;

}

do_AES(unsigned char *k, char *str, int strlen)

{outlen;char key[32]; /* 256- битный ключ */

unsigned char iv[8]; /* вектор инициализации */

unsigned char inbuf[BUFSIZE], outbuf[BUFSIZE];

(int i=0; i<32; i++){[i]=k[i];

}

(inbuf,str,strlen);_CIPHER_CTX ctx;EVP_CIPHER * cipher;

_CIPHER_CTX_init(&ctx);

= EVP_aes_256_cfb();

_EncryptInit(&ctx, cipher, key, iv);

_EncryptUpdate(&ctx, outbuf, &outlen, inbuf, strlen);(str,outbuf,strlen);_EncryptFinal(&ctx, outbuf, &outlen);_CIPHER_CTX_cleanup(&ctx);

1;

}

md5_IDc(unsigned char *IDc){

EVP_MD_CTX mdctx; /* контекст для вычисления хэша */EVP_MD * md; /* структура с адресами функций алгоритма */

unsigned char md_value[EVP_MAX_MD_SIZE];

unsigned int md_len; /* размер вычисленного хэша */

int buf_len;*in;

=fopen(PUBLIC_CLIENT_KEY,"r");_add_all_digests();= EVP_get_digestbyname("md5");_DigestInit(&mdctx, md);

/* Вычисляем хэш */buf[BUFFSIZE];(;;) {long in_len = fread(buf,1,BUFFSIZE,in);(in_len <= 0) break;_len=in_len;_DigestUpdate(&mdctx, buf, (unsigned long)in_len);

}

_DigestFinal(&mdctx, md_value, &md_len);

_MD_CTX_cleanup(&mdctx);

tmp[4];tmp1[33];(int i = 0; i < md_len*2; i+=2){(tmp,"x",md_value[i/2]);(&tmp1[i],"%c",tmp[0]);(i+1<32)(&tmp1[i+1],"%c",tmp[1]);

}(IDc,tmp1,KEYSIZE+1);();1;

}to_hex(unsigned char *str_dest, unsigned char *str_src, int sstr_len){tmp[4],buf1[RSA_key_size+1];(int i=0; i<sstr_len; i+=2){(tmp,"x",str_src[i/2]);(&buf1[i],"%c",tmp[0]);(i+1<sstr_len)(&buf1[i+1],"%c",tmp[1]);

}

(str_dest,buf1,sstr_len+1);0;

}

gen_session_key(unsigned char *buff){

char buf[KEYSIZE+1],session_key[KEYSIZE+1];_bytes(buf,KEYSIZE);(buf,session_key,KEYSIZE+1);

_hex(buff,session_key, KEYSIZE);

1;

}

do_encrypt(unsigned char *ptext, unsigned char *ctext){* pubKey = NULL;outlen;* pub_key_file = NULL;

_key_file = fopen(PUBLIC_SERVER_KEY, "rb");= PEM_read_RSAPublicKey(pub_key_file, NULL, NULL, NULL);key_size = RSA_size(pubKey);

= RSA_public_encrypt(96, ptext, ctext, pubKey, RSA_PKCS1_PADDING);(outlen != RSA_size(pubKey)) exit(-1);

();0;

}

pre_rsa(unsigned char *k1, unsigned char *k2, unsigned char *IDc, unsigned char *ptext){

count=0;(int i=0; i<KEYSIZE; i++){[count]=k1[i];++;

}(int i=0; i<KEYSIZE; i++){[count]=k2[i];++;

}(int i=0; i<KEYSIZE; i++){[count]=IDc[i];++;

}0;

}

do_sign(unsigned char *m, unsigned int m_len, unsigned char *ctext, unsigned int key_size){*privKey = NULL;*priv_key_file;EVP_CIPHER *cipher = NULL;

_add_all_ciphers();= EVP_get_cipherbyname("bf-ofb");

_key_file = fopen(PRIVAT_CLIENT_KEY, "rb");= PEM_read_RSAPrivateKey(priv_key_file, NULL, NULL, "hello");

(RSA_sign(1,m,m_len,ctext,&key_size,privKey)==1){

}

0;

}

get_IDauthkey(unsigned char *IDauthkey, unsigned char *k1){*f;char buff[34];((f=fopen(IDauthkey_file,"r+"))!=NULL){(fread(IDauthkey,1,33,f)==0){(k1,IDauthkey,32);_hex(IDauthkey,IDauthkey,32);(IDauthkey,1,33,f);(f);1;

}{(f);(k1,buff,32);_hex(buff,buff,32);=fopen(IDauthkey_file,"w+");(buff,1,33,f);(f);0;

}

}{=fopen(IDauthkey_file,"w+");(k1,buff,32);_hex(buff,buff,32);(buff,1,32,f);(f);2;

}(-1);

}

get_authkey(unsigned char *authkey, unsigned char *k2){*f;char buff[34];((f=fopen(authkey_file,"r+"))!=NULL){(fread(authkey,1,33,f)==0){(k2,authkey,32);_hex(authkey,authkey,32);(authkey,1,33,f);(f);1;

}{(f);(k2,buff,32);_hex(buff,buff,32);=fopen(authkey_file,"w+");(buff,1,33,f);(f);0;

}

}{=fopen(authkey_file,"w+");(k2,buff,32);_hex(buff,buff,32);(buff,1,32,f);(f);2;

}(-1);

}

make_send_package(char *package,unsigned char *P1, unsigned char *P2, unsigned char *P3, unsigned char *P4){

buf[BUFSIZE];count=0;(buf,P1,128);(int i=0; i<RSA_key_size; i++){[count]=buf[i];++;

}(buf,P2,128);(int i=0; i<RSA_key_size; i++){[count]=buf[i];++;

}(buf,P3,32);(int i=0; i<KEYSIZE; i++){[count]=buf[i];++;

}(buf,P4,128);(int i=0; i<RSA_key_size; i++){[count]=buf[i];++;

}

0;

}

do_DECRYPT_AES(unsigned char *k, char *str, in