#ifndef __PROGTEST__ #include #include #include #include #include #define MESSAGE_MAX 8192 #define AGENTS_MAX 16 #define KEY_LENGTH 32 #define SIGNATURE "God save the Queen!" typedef struct TMessage { unsigned char m_Message [MESSAGE_MAX]; int m_Length; } TMESSAGE; typedef struct TResults { int m_Agent; int m_Shift; } TRESULTS; #endif /* __PROGTEST__ */ static TMESSAGE g_Messages [8] = { { { 0x35, 0xd9, 0x38, 0xc6, 0xca, 0xf2, 0x3d, 0x76, 0xd8, 0xa6, 0x7f, 0x9e, 0x04, 0x61, 0x6d, 0x23, 0xd2, 0xe5, 0x9b, 0xa0, 0x7b, 0x52, 0xf4, 0xfd, 0x32, 0xc6 }, 26 }, //Dr. No { { 0xa6, 0x39, 0x7c, 0x8e, 0x92, 0x00, 0xbc, 0x0e, 0x6b, 0x6c, 0x0b, 0x61, 0x59, 0xc8, 0x5a, 0x8b, 0xd0, 0x3a, 0x34, 0x15, 0x44, 0x8e, 0xe2, 0x22, 0x6c, 0x21, 0xd2, 0xa4, 0xd6, 0xce, 0x46, 0x2d, 0xc0, 0xe1, 0x3f, 0x87, 0x66, 0xcf, 0x27, 0xd7, 0x82 }, 41 }, //From Russia with love { { 0xd7, 0x2a, 0x83, 0x99, 0xe7, 0x23, 0x76, 0xd8, 0x83, 0x62, 0x9e, 0x41, 0x5e, 0x39, 0x1d, 0x65, 0x41, 0x8c, 0x20, 0xcf, 0xa7, 0xdf, 0x88, 0x7d, 0x36, 0xd4, 0xf7, 0x24, 0xec, 0x00 }, 30 }, //Goldfinger - POZOR, zamerne spatny podpis { { 0x21, 0xc7, 0xca, 0xf5, 0x34, 0x65, 0x9d, 0xd7, 0x7e, 0x93, 0x04, 0x2f, 0x1d, 0x0d, 0x65, 0x4a, 0xc3, 0x5b, 0x33, 0xc8, 0xbe, 0xd5, 0x8a, 0x71, 0x2a, 0xd1, 0xf3, 0x3a, 0x8a, 0x4c, 0xff }, 31 }, //Thunderball { { 0xef, 0x36, 0x76, 0xf2, 0xb0, 0x65, 0x9f, 0x41, 0x7c, 0x2d, 0x0e, 0x65, 0x0f, 0xd9, 0x12, 0x9a, 0x98, 0x0e, 0x61, 0x21, 0x54, 0x85, 0xa6, 0x3e, 0xcf, 0xbe, 0x9b, 0x81, 0x7a, 0x34, 0xca, 0xb2, 0x3a, 0x8f, 0x30, 0xdd, 0x6e, 0xd7, 0x9d }, 39 }, //You only live twice { { 0x85, 0x6f, 0x8f, 0x41, 0x7c, 0x29, 0x0a, 0x76, 0x46, 0xce, 0x1f, 0xf5, 0xff, 0x30, 0x70, 0x64, 0x42, 0x8a, 0xf1, 0x29, 0x6d, 0xdd, 0x51, 0xdc, 0x47, 0xe0, 0x09, 0x70, 0x3c, 0xc4, 0xed, 0x28, 0xce, 0xeb, 0xf3, 0x8b, 0x66, 0x78, 0xfe, 0xf3, 0x3c, 0x83, 0x35, 0xcc, 0x37, 0x84, 0x99, 0xa6, 0x26, 0x76, 0x9b }, 51 }, //On Her Majesty's secret service { { 0xe3, 0x23, 0x76, 0x8a, 0xfd, 0x4d, 0x94, 0x05, 0x2f, 0x3f, 0x19, 0x76, 0x4a, 0x8d, 0x0e, 0x97, 0xdd, 0x7f, 0x45, 0x31, 0x54, 0x8e, 0xe9, 0x6d, 0x23, 0xc9, 0xaa, 0xd6, 0x81, 0x7a, 0x3c, 0xc0, 0xb2, 0x37, 0x94, 0x23, 0x98, 0x28, 0xcc, 0x98 }, 40 }, //Diamonds are forever { { 0xca, 0x8f, 0x8c, 0x12, 0x7c, 0x9c, 0xd7, 0x79, 0x9a, 0x17, 0x6a, 0x6c, 0x0c, 0x68, 0x4a, 0x8d, 0x2b, 0x8a, 0xdd, 0x3a, 0x7a, 0x65, 0x2b, 0xc9, 0xbd, 0xde, 0xce, 0x75, 0x36, 0xd7, 0xb2, 0x3a, 0x83, 0x32, 0x98, 0x2a }, 36 } //Live and let die }; const TMESSAGE * receiver ( void ) { TMESSAGE * res; static int cnt = 0; if ( cnt >= 8 ) return NULL; res = new TMESSAGE; *res = g_Messages[ cnt ++ ]; printf ( "Message received!\n" ); return res; } void officer ( const TMESSAGE * msg, TRESULTS * res, int resNr ) { int i; printf ( "Message processing finished!\n" ); for ( i = 0; i < resNr; i ++ ) printf ( "\tagent %d, shift %d\n", res[i] . m_Agent, res[i] . m_Shift ); if ( resNr == 0 ) printf ( "An invalid message, perhaps KGB?\n" ); delete msg; } /*************************************************************************************** * * AZ PO SEM NECHAT BYT, NESTARAT SE O TO. ZARIDI PROGTEST SAM. PROGRAMOVAT AZ BLOK NIZE: * *****************************************************************************************/ int agent; const unsigned char (* klic)[KEY_LENGTH]; const TMESSAGE *(* greceiver) ( void ); void (* gofficer) ( const TMESSAGE *, TRESULTS *, int ); void *Fun ( void * par2) { int i; unsigned char a=0,b=0,c=16,shift=0,shift2=0,d=0; const TMessage * zprava; TResults vysledek[100]; while(1) { zprava = greceiver(); if(zprava==NULL){break;;} int pv=0; for(shift=0; shiftm_Length; shift++) //cyklus na desifrovani zprav pro kazdej shift { shift2=shift; d=0; int k,m; int pis=0; for(m=0; mm_Length; i++,k++) //desifrovani u jednotlivejch shiftu { if(k>KEY_LENGTH-1) //prekroceni delky klice { k=0; } if(i==0) { a=klic[m][k]^i; b=a; a=a^(zprava -> m_Message[shift2]); // printf("%c",a); continue; } a=klic[m][k]^i^b; b=a; c=i; if(c+shift2>zprava->m_Length-1) //pokud pretece velikost zpravy { a=a^(zprava -> m_Message[d]); d++; } a=a^(zprava -> m_Message[shift2+i]); //printf("%c",a); if(a==71) { pis++; } if(a==111 && pis==1) { pis++; } if(a==100 && pis==2) { pis++; } if(a==32 && pis==3) { pis++; } if(a==115 && pis==4) { pis++; } if(a==97 && pis==5) { pis++; } if(a==118 && pis==6) { pis++; } if(a==101 && pis==7) { pis++; } if(a==32 && pis==8) { pis++; } if(a==116 && pis==9) { pis++; } if(a==104 && pis==10) { pis++; } if(a==101 && pis==11) { pis++; } if(a==32 && pis==12) { pis++; } if(a==81 && pis==13) { pis++; } if(a==117 && pis==14) { pis++; } if(a==101 && pis==15) { pis++; } if(a==101 && pis==16) { pis++; } if(a==110 && pis==17) { pis++; } if(a==33 && pis==18) { pis++; } if(a==32 && pis==19) { pis++; } if(pis==19) { pis=0; vysledek[pv].m_Agent=m; vysledek[pv].m_Shift=shift2; pv=pv+1; } } //printf("\n"); } } gofficer(zprava,vysledek,pv); } return NULL; } void SecretService ( int agents, const unsigned char (* keys)[KEY_LENGTH], int threads, const TMESSAGE *(* receiver) ( void ), void (* officer) ( const TMESSAGE *, TRESULTS *, int ) ) { greceiver=receiver; gofficer=officer; agent=agents; klic=keys; pthread_t thr[threads]; pthread_attr_t thrAttr; int i; void * dummy; pthread_attr_init ( &thrAttr ); pthread_attr_setdetachstate ( &thrAttr, PTHREAD_CREATE_JOINABLE ); for(i=0; i