Вы находитесь на странице: 1из 11

Министерство Образования и Науки Кыргызской Республики

Кыргызский Государственный Технический Университет


Факультет информационных технологий
Кафедра:”Информатики и вычислительной техники”

Отчет
Лабораторная работа
Хеш-функция

Выполнила: Чилетирова Мээрим


Группа: ИВТ-1-19
Проверила: Шаршеева К.Т.

Бишкек 2021
#include <iostream>
#include <bitset>
#include <cmath>
#include <string>
#include <sstream>
using namespace std;
string BinaryStringToText(string binaryString) {
string text = "";
stringstream sstream(binaryString);
while (sstream.good())
{ bitset<8> bits;
sstream >> bits;
text += char(bits.to_ulong()); }
return text;
}
int main()
{
setlocale(LC_ALL,"rus");
int A[32] = { 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 0, 1 };
int B[32] = { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0,
1, 0, 0, 1 };
int C[32] = { 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1,
1, 1, 1, 0 };
int D[32] = { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1,
0, 1, 1, 0 };
int E[32] = { 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1,
0, 0, 0, 0 };
int K[32] = { 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1,
0, 1, 1, 0 };
string word =
{"CHILETIROVAMEERIMNURDINOVNAINWORMATIKAIVYCHIZKITELNAYATEXNIKA
WIT"};
string encryptedword;
for (char& _char : word) {
encryptedword += bitset<8>(_char).to_string();
}
int w = encryptedword.length();
cout << "Длина слова: " << w << endl;
int encryptedWord[640];
int u=0;
for (int i=0; i<w; i++)
{
if(encryptedword[u]=='0')
encryptedWord[i]=0;
if(encryptedword[u]=='1')
encryptedWord[i]=1;
u++;
}
cout << "Код слова в двочной системе: ";
for(int j=0; j<w; j++)
{
cout << encryptedWord[j];
}
cout << endl;
int x=0, W[32];
int W76[32], W77[32], W78[32], W79[32];
cout << "r" << "\t" << "W" << endl;
int r=60;
for(int i=0; i<16; i++)
{
cout << r << "\t" ;
for(int j=0; j<32; j++)
{
W[j]=encryptedWord[i*32+j];
cout << W[j];
}
cout << endl;
r++;
}
int i=16;
r=76;
for(int x=0; x<4; x++)
{
cout << r << "\t";
for(int j=0; j<32; j++)
{
if(encryptedWord[(i-3+x)*32+j]==0 && encryptedWord[(i-8+x)*32+j]==0 &&
encryptedWord[(i-14+x)*32+j]==0 && encryptedWord[(i-16+x)*32+j]==0)
W[j]=0;
else if(encryptedWord[(i-3+x)*32+j]==0 && encryptedWord[(i-8+x)*32+j]==0
&& encryptedWord[(i-14+x)*32+j]==0 && encryptedWord[(i-16+x)*32+j]==1)
W[j]=1;
else if(encryptedWord[(i-3+x)*32+j]==0 && encryptedWord[(i-8+x)*32+j]==0
&& encryptedWord[(i-14+x)*32+j]==1 && encryptedWord[(i-16+x)*32+j]==0)
W[j]=1;
else if(encryptedWord[(i-3+x)*32+j]==0 && encryptedWord[(i-8+x)*32+j]==0
&& encryptedWord[(i-14+x)*32+j]==1 && encryptedWord[(i-16+x)*32+j]==1)
W[j]=0;
else if(encryptedWord[(i-3+x)*32+j]==0 && encryptedWord[(i-8+x)*32+j]==1
&& encryptedWord[(i-14+x)*32+j]==0 && encryptedWord[(i-16+x)*32+j]==0)
W[j]=1;
else if(encryptedWord[(i-3+x)*32+j]==0 && encryptedWord[(i-8+x)*32+j]==1
&& encryptedWord[(i-14+x)*32+j]==0 && encryptedWord[(i-16+x)*32+j]==1)
W[j]=0;
else if(encryptedWord[(i-3+x)*32+j]==0 && encryptedWord[(i-8+x)*32+j]==1
&& encryptedWord[(i-14+x)*32+j]==1 && encryptedWord[(i-16+x)*32+j]==0)
W[j]=0;
else if(encryptedWord[(i-3+x)*32+j]==0 && encryptedWord[(i-8+x)*32+j]==1
&& encryptedWord[(i-14+x)*32+j]==1 && encryptedWord[(i-16+x)*32+j]==1)
W[j]=1;
else if(encryptedWord[(i-3+x)*32+j]==1 && encryptedWord[(i-8+x)*32+j]==0
&& encryptedWord[(i-14+x)*32+j]==0 && encryptedWord[(i-16+x)*32+j]==0)
W[j]=1;
else if(encryptedWord[(i-3+x)*32+j]==1 && encryptedWord[(i-8+x)*32+j]==0
&& encryptedWord[(i-14+x)*32+j]==0 && encryptedWord[(i-16+x)*32+j]==1)
W[j]=0;
else if(encryptedWord[(i-3+x)*32+j]==1 && encryptedWord[(i-8+x)*32+j]==0
&& encryptedWord[(i-14+x)*32+j]==1 && encryptedWord[(i-16+x)*32+j]==0)
W[j]=0;
else if(encryptedWord[(i-3+x)*32+j]==1 && encryptedWord[(i-8+x)*32+j]==0
&& encryptedWord[(i-14+x)*32+j]==1 && encryptedWord[(i-16+x)*32+j]==1)
W[j]=1;
else if(encryptedWord[(i-3+x)*32+j]==1 && encryptedWord[(i-8+x)*32+j]==1
&& encryptedWord[(i-14+x)*32+j]==0 && encryptedWord[(i-16+x)*32+j]==0)
W[j]=0;
else if(encryptedWord[(i-3+x)*32+j]==1 && encryptedWord[(i-8+x)*32+j]==1
&& encryptedWord[(i-14+x)*32+j]==0 && encryptedWord[(i-16+x)*32+j]==1)
W[j]=1;
else if(encryptedWord[(i-3+x)*32+j]==1 && encryptedWord[(i-8+x)*32+j]==1
&& encryptedWord[(i-14+x)*32+j]==1 && encryptedWord[(i-16+x)*32+j]==0)
W[j]=1;
else if(encryptedWord[(i-3+x)*32+j]==1 && encryptedWord[(i-8+x)*32+j]==1
&& encryptedWord[(i-14+x)*32+j]==1 && encryptedWord[(i-16+x)*32+j]==1)
W[j]=0;
if(r==76)
{
W76[j]=W[j];
cout << W76[j];
}

else if(r==77)
{
W77[j]=W[j];
cout << W77[j];
}
else if(r==78)
{
W78[j]=W[j];
cout << W78[j];
}
else if(r==79)
{
W79[j]=W[j];
cout << W79[j];
}
}
r++;
cout << endl;
}
cout << endl;
for(int i=0; i<32; i++)
{
encryptedWord[512+i]=W76[i];
}
for(int i=0; i<32; i++)
{
encryptedWord[544+i]=W77[i];
}
for(int i=0; i<32; i++)
{
encryptedWord[576+i]=W78[i];
}
for(int i=0; i<32; i++)
{
encryptedWord[608+i]=W79[i];
}
cout << "Код слова после добавления раундов: ";
for(int i=0; i<640; i++)
{
cout << encryptedWord[i];
}
cout << endl;
int newA[32], newB[32], newC[32], newD[32], newE[32], f[32], n1[32], n2[32],
n3[32];
cout << endl;
for(int j=0; j<20; j++)
{
cout << "Раунд:" << j+1 << endl;
cout << "F=";//выполнение функции
for(int i=0; i<32; i++)
{
if(B[i]==0 && C[i]==0 && D[i]==0)
f[i]=0;
else if(B[i]==0 && C[i]==0 && D[i]==1)
f[i]=1;
else if(B[i]==0 && C[i]==1 && D[i]==0)
f[i]=1;
else if(B[i]==0 && C[i]==1 && D[i]==1)
f[i]=0;
else if(B[i]==1 && C[i]==0 && D[i]==0)
f[i]=1;
else if(B[i]==1 && C[i]==0 && D[i]==1)
f[i]=0;
else if(B[i]==1 && C[i]==1 && D[i]==0)
f[i]=0;
else if(B[i]==1 && C[i]==1 && D[i]==1)
f[i]=1;
cout << f[i];
}
cout << endl;
cout << "Первое наложение E+F =";//наложение функции и блока Е
for(int i=0; i<32; i++)
{
if(E[i]==f[i])
n1[i]=0;
else if(E[i]!=f[i])
n1[i]=1;
cout << n1[i];
}
cout << endl;

/////
int SDA[32];
cout << "Сдвиг влево на 5 SDA=";
for(int i=0; i<27; i++)
{
SDA[i]=A[i+5];
}
SDA[27]=A[0];
SDA[28]=A[1];
SDA[29]=A[2];
SDA[30]=A[3];
SDA[31]=A[4];

for(int i=0; i<32; i++)


{
cout << SDA[i];
}
cout << endl;

////
cout << "Второе наложение +A =";//наложение блока А и
вышеполученного кода
for(int i=0; i<32; i++)
{
if(SDA[i]==n1[i])
n2[i]=0;
else if(SDA[i]!=n1[i])
n2[i]=1;
cout << n2[i];
}
cout << endl;
cout << "Третье наложение +W =";//наложение части сообщения и
вышеполученного кода
for(int i=0; i<32; i++)
{
if(n2[i]==encryptedWord[j*32+i])
n3[i]=0;
else if(n2[i]!=encryptedWord[j*32+i])
n3[i]=1;
cout << n3[i];
}
cout << endl;
cout << "A=";//наложение ключа и вышеполученного кода, присвоение
блоку А
for(int i=0; i<32; i++)
{
if(n3[i]==K[i])
newA[i]=0;
else if(n3[i]!=K[i])
newA[i]=1;
cout << newA[i];
}
cout << endl;
cout << "B=";
for(int i=0; i<32; i++)
{
newB[i]=A[i];
cout << newB[i];
A[i]=newA[i];
}
cout << endl;
cout << "C=";
newC[0]=B[30];
newC[1]=B[31];
for(int i=2; i<32; i++)
{
newC[i]=B[i-2];
}
for(int i=0; i<32; i++)
{
cout << newC[i];
B[i]=newB[i];
}
cout << endl;
cout << "D=";
for(int i=0; i<32; i++)
{
newD[i]=C[i];
cout << newD[i];
C[i]=newC[i];
}
cout << endl;
cout << "E=";
for(int i=0; i<32; i++)
{
newE[i]=D[i];
cout << newE[i];
D[i]=newD[i];
E[i]=newE[i];
}
cout<< endl;
cout<<endl;
}
return 0;
}

Вам также может понравиться