
Реализации различных алгоритмов на Си / шифрование метод хилла
.doc#include <iostream.h> #include <conio.h> #include <math.h>
//funkcia podscheta dlinni massiva unsigned int Lenght(const char s[]) { int L=0; while(s[L++]); return (L-1); }
unsigned short mod26(int c)//functia vichisleniya po modulu 26 { if(c>0) { unsigned int c2=c/26; return c-(26*c2); } if(c<0) { unsigned int c2=abs(c)/26+1; return c+(26*c2); } return 0;
}
main() { clrscr(); unsigned short key[3][3];//matrica kodirovania
//stroka 1 key[0][0]=1; key[0][1]=2; key[0][2]=3;
//stroka 2 key[1][0]=7; key[1][1]=5; key[1][2]=4;
//stroka 3 key[2][0]=8; key[2][1]=2; key[2][2]=7;
char abc[26]="abcdefghijklmnopqrstuvwxyz";
//kodiruem slovo "studentaa" unsigned short word[9];
char text[9]="studentaa";
int w=0;
cout<<"Makarenkov M.S. ZI-01-04 variant 12(3)"<<endl; cout<<"ishodnoe slovo: studentaa"<<endl; while(w<=Lenght(text))//poka ne konchitsya massiv text { for(int i=0;i<25;i++) { //zanosim v word_num chislovoy kod bykvi if(text[w]==abc[i])word[w]=i; }
w++; }
//shifrovanie,ymnogaem matricy na vector unsigned short cript[9]={0};//polychenniy shifr
cout<<"polychenniy shifr v vide chastey razbityh po 3"<<endl<<" "<<endl; //ymnozaem matricy key na vectori slova po mod 26 for(int k=0;k<9;k+=3) { for(int i=0;i<3;i++) { cript[i+k]=mod26(key[i][0]*word[0+k]+key[i][1]*word[1+k]+key[i][2]*word[2+k]);
cout<<cript[i+k]<<" "; } cout<<endl; } cout<<" polychenniy shifr v vide stroki nomerov i bykv:"<<endl;
for(int i=0;i<9;i++) cout<<cript[i]<<"="<<abc[cript[i]]<<endl; cout<<endl<<"Pozalysta nazmite ENTER"; cin.get();
return 0;
}