You are on page 1of 31

МІНІСТЕРСТВО ОСВІТИ ТА НАУКИ УКРАЇНИ

Київський національний університет імені Тараса Шевченка


Кафедра Програмних систем і технологій

Звіт з лабораторної роботи №1.1

Виконав: студент групи ІПЗ - 31


Петренко Іван Владиславович
Перевірила: викладач
Гріненко О. О.

Київ 2020
Постановка задачі.
Написати код шифрування та дешифрування масиву інформації
за допомогою трьох алгоритмів (Полібіанський квадрат,
Плейфер, Віженер).

Полібіанський квадрат. Блок-схема


Реалізація
Метод 1 (Слово для шифрування переводиться у числа, та з чисел
розшифровується)
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <conio.h>
using namespace std;
void l_ins(char[][5]);
void u_ins(char[][6]);
char* prep_l(char*);
char* prep_u(char*);
int fill_l(int[][50], char[][5], char*);
int fill_u(int[][50], char[][6], char*);
void fill_c(int[][50], int[][50], int);
void rev_c(int[][50], int[][50], int);
void print(int[][50], int);
void main()
{
setlocale(0, "");
bool f;
cout << "Language - 1, ìîâà -0\n?";
cin >> f;
char latin[5][5], ukr[6][6], *el="He is rather strict with us, but always fair.",
*um = "Коли займаєшся політикою, дій, як громадянин.";
char text[50];
if (f)
cout<<strcpy(text, el)<<endl;
else
cout<<strcpy(text,um)<<endl;
char cipher[50]{}, rez[50]{};
int ti[2][50], ci[2][50], len;
char*(*p)(char*);

if (f)
{
l_ins(latin);
p = prep_l;
}
else
{
u_ins(ukr);
p = prep_u;
}
strcpy(text, p(text));
cout << text << endl;

len = f ? fill_l(ti, latin, text) : fill_u(ti, ukr, text);


print(ti, len);

fill_c(ti, ci, len);


print(ci, len);

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


if(f)
cipher[i] = latin[ci[0][i]][ci[1][i]];
else
cipher[i] = ukr[ci[0][i]][ci[1][i]];
cout << cipher << endl;

len = f ? fill_l(ti, latin, cipher) : fill_u(ti, ukr, cipher);


print(ti, len);

rev_c(ti, ci, len);


print(ci, len);

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


rez[i] = f ? latin[ci[0][i]][ci[1][i]] : ukr[ci[0][i]][ci[1][i]];
cout << rez << endl;
}
void l_ins(char a[][5])
{
char l[26] = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
int k = 0;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
a[i][j] = l[k++];
}
void u_ins(char a[][6])
{
char u[37] = "АБВГДЕЄЗЖИІЇЙКЛМНОПРСТУФХЦЧШЩЮЯ";
int k = 0;
for (int i = 0; i < 6; i++)
for (int j = 0; j < 6; j++)
a[i][j] = u[k++];
}

char* prep_l(char*temp)
{
char s[50]{};
char * path;
char r[12] = " ,./!?;:\"\'";
path = strtok(temp, r);
do{
strcat(s, path);
} while (path = strtok(NULL, r));
path = s;
while (*path)
{
*path = toupper(*path);
if (*path == 'J')
*path = 'I';
path++;
}
return s;
}
char* prep_u(char*temp)
{
int c;
char s[50]{};
char * path;
char r[14] = "üÜ ,./!?;:\"\'";
path = strtok(temp, r);
do{
strcat(s, path);
} while (path = strtok(NULL, r));
path = s;
while (*path)
{
if (*path == 'i')
*path = 'I';
else if (*path == 'º' || *path == 'ª')
*path = 'Å';
else if (*path == '¿' || *path == '¯')
*path = 'I';
else if (*path == 'é' || *path == 'É')
*path = 'È';
else if (*path > -33)
*path -= 32;
path++;
}
return s;
}

int fill_l(int ti[][50], char l[][5], char*s)


{
int k = 0;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 50; j++)
ti[i][j] = 0;
while (*s)
{
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if (*s == l[i][j])
{
ti[0][k] = i;
ti[1][k] = j;
k++;
}
s++;
}
return k;
}
int fill_u(int ti[][50], char l[][6], char*s)
{
int k = 0;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 50; j++)
ti[i][j] = 0;
while (*s)
{
for (int i = 0; i < 6; i++)
for (int j = 0; j < 6; j++)
if (*s == l[i][j])
{
ti[0][k] = i;
ti[1][k] = j;
k++;
}
s++;
}
return k;
}
void fill_c(int t[][50], int c[][50], int l)
{
int k = 0, a[100]{};
for (int i = 0; i < 2; i++)
for (int j = 0; j < 50; j++)
c[i][j] = 0;
for (int i = 0; i < l; i++)
for (int j = 0; j < 2; j++)
a[k++] = t[j][i];
k = 0;
for (int i = 0; i < 2; i++)
for (int j = 0; j < l; j++)
c[i][j] = a[k++];
}

void rev_c(int t[][50], int c[][50], int l)


{
int k = 0, a[100]{};
for (int i = 0; i < 2; i++)
for (int j = 0; j < 50; j++)
c[i][j] = 0;
for (int i = 0; i < 2; i++)
for (int j = 0; j < l; j++)
a[k++] = t[i][j];
k = 0;
for (int i = 0; i < l; i++)
for (int j = 0; j < 2; j++)
c[j][i] = a[k++];
}

void print(int a[][50], int n)


{
cout << endl;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < n; j++)
cout << a[i][j] << ' ';
cout << endl;
}
}
Метод 2 (Зашифроване слово переводиться у шифр, у вигляді букв)
#include <Windows.h>
#include <cstdlib>
#include "stdafx.h"
#include <cctype>
#include <iostream>
#include <cstring>
#include <string.h>
#include <cstdio>
#include <stdio.h>
#include <iostream>
#include <locale>
#define D1 5
#define D2 5
using namespace std;

int a1[100];
int a2[100];
int k = 0;

const char POLYBIUS[D1][D2] = {


{ 'A', 'B', 'C' , 'D', 'E' },
{ 'F', 'G', 'H' , 'I', 'K' },
{ 'L', 'M', 'N' , 'O', 'P' },
{ 'Q', 'R', 'S' , 'T', 'U' },
{ 'V', 'W', 'X' , 'Y', 'Z' }
};

char encryption(const char f) {

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


for (int j = 0; j < D2; j++)
if (POLYBIUS[i][j] == f) {
a1[k] = i;
a2[k] = j;
k++;
return POLYBIUS[(i + 1) % 5][j];
}

return ' ';


}

char decryption(const char c) {

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


for (int j = 0; j < D2; ++j)
if (POLYBIUS[i][j] == c)
return POLYBIUS[(i + 4) % 5][j];
return ' ';
}

int main() {

char c[100] = "England is a capital of Great Britain"; //initial text


char d[100]; //encripted text - method 1
char e[100]; //encripted text - method 2
int l = 0; // counter

for (int i=0; i < strlen(c); i++)


c[i] = toupper(c[i]);

std::cout << "Encription " << std::endl;


for (int i = 0; c[i] != '\0'; ++i) {
d[i] = encryption(c[i]);
std::cout << d[ i ];
}
std::cout << std::endl << std::endl;

std::cout << "------------------------" << std::endl << std::endl;


std::cout << "Decription " << std::endl << std::endl;
for (int i = 0; c[i] != '\0'; ++i)
std::cout << decryption(d[i]);
std::cout << std::endl;

return 0;
}

Результат роботи
Метод 1
Метод 2

Алгоритм Плейфера
Програмна реалізація
#define _CRT_SECURE_NO_WARNINGS
#define LATIN /*UKR*/
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
struct latin
{
char alphabet[26];
char keyword[20];
char square[5][5];
bool select[25];
int size;
latin(char*);
char* operator+(char*);
char* operator-(char*);
};
struct ukr
{
char alphabet[29];
char keyword[20];
char square[4][7];
bool select[28];
int size;
ukr(char*);
char* operator+(char*);
char* operator-(char*);
};

char* prep_l(char*);
char* prep_u(char*);

void main()
{
setlocale(0, "");
bool f;
#ifdef LATIN
f = true;
#endif
#ifdef UKR
f = false;
#endif
char keyword[20]{}, *el = "Londinium is the city of blood and wine.",
*um = "Êîëè çàéìàºøüñÿ ïîëiòèêîþ, äié, ÿê ãðîìàäÿíèí.";
char text[50]{}, cipher[50]{}, rez[50]{};;
if (f)
cout << strcpy(text, prep_l(strcpy(text, el))) << endl;
else
cout << strcpy(text, prep_u(strcpy(text, um))) << endl;

if (f)
{
cout << "enter your keyword at least 20 characters without spaces: ";
cin >> keyword;
strcpy(keyword, prep_l(keyword));
}
else
{
cout << " ââåäiòü êëþ÷îâå ñëîâî íå áiëüø íiæ 20 ñèìâîëiâ áåç ïðîïóñêiâ: ";
SetConsoleCP(1251);
cin >> keyword;
SetConsoleCP(866);
strcpy(keyword, prep_u(keyword));
}
#ifdef LATIN
latin sl(keyword);
strcpy(cipher, sl + text);
#endif
#ifdef UKR
ukr su(keyword);
strcpy(cipher, su + text);
#endif
cout << cipher << endl;
#ifdef LATIN
strcpy(rez, sl - cipher);
#endif
#ifdef UKR
strcpy(rez, su - cipher);
#endif
cout << rez << endl;
}

char* prep_l(char*temp)
{
char s[50]{};
char * path;
char r[12] = " ,./!?;:\"\'";
path = strtok(temp, r);
do{
strcat(s, path);
} while (path = strtok(NULL, r));
path = s;
while (*path)
{
*path = toupper(*path);
if (*path == 'J')
*path = 'I';
path++;
}
return s;
}
char* prep_u(char*temp)
{
int c;
char s[50]{};
char * path;
char r[14] = "üÜ ,./!?;:\"\'";
path = strtok(temp, r);
do{
strcat(s, path);
} while (path = strtok(NULL, r));
path = s;
while (*path)
{
if (*path == 'i')
*path = 'I';
else if (*path == 'º' || *path == 'ª')
*path = 'Å';
else if (*path == '¿' || *path == '¯')
*path = 'I';
else if (*path == 'é' || *path == 'É')
*path = 'È';
else if (*path > -33)
*path -= 32;
path++;
}
return s;
}

latin::latin(char*s)
{
strcpy(keyword, s);
strcpy(alphabet, "ABCDEFGHIKLMNOPQRSTUVWXYZ");
size = strlen(keyword);
for (int i = 0; i < 25; i++)
select[i] = true;
int k = 0;
for (int i = 0; i < 25; i++)
for (int j = 0; j < size; j++)
while (keyword[j] == alphabet[i])
{
if (select[i])
{
select[i] = false;
break;
}
else
{
k = j;
while (keyword[k])
keyword[k] = keyword[k++ + 1];
size--;
}

}
int l = k = 0;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if (k < size)
square[i][j] = keyword[k++];
else
{
while (!select[l])l++;
square[i][j] = alphabet[l];
l++;
}
}

ukr::ukr(char*s)
{
strcpy(keyword, s);
strcpy(alphabet, "ÀÁÂÃÄÅÆÇÈIÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÞß");
size = strlen(keyword);
for (int i = 0; i < 28; i++)
select[i] = true;
int k = 0;
for (int i = 0; i < 28; i++)
for (int j = 0; j < size; j++)
while (keyword[j] == alphabet[i])
{
if (select[i])
{
select[i] = false;
break;
}
else
{
k = j;
while (keyword[k])
keyword[k] = keyword[k++ + 1];
size--;
}

}
int l = k = 0;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 7; j++)
if (k < size)
square[i][j] = keyword[k++];
else
{
while (!select[l])l++;
square[i][j] = alphabet[l];
l++;
}
}

char* latin::operator+(char*s)
{
int i1, i2, j1, j2;
char*rez = new char[strlen(s) + 1];
for (int i = 0; i <= strlen(s); i += 2)
if (s[i] == '\0' || s[i + 1] == '\0')
{
rez[i] = s[i];
rez[i + 1] = '\0';
}
else if (s[i] == s[i + 1])
{
rez[i] = 'J';
rez[i + 1] = s[i];
}
else
{
for (int k = 0; k < 5; k++)
for (int j = 0; j < 5; j++)
{
if (s[i] == square[k][j])
{
i1 = k;
j1 = j;
}
if (s[i + 1] == square[k][j])
{
i2 = k;
j2 = j;
}
}
if (i1 == i2)
{
rez[i] = square[i1][(j1 + 1) % 5];
rez[i + 1] = square[i2][(j2 + 1) % 5];
}
else if (j1 == j2)
{
rez[i] = square[(i1 + 1) % 5][j1];
rez[i + 1] = square[(i2 + 1) % 5][j2];
}
else
{
rez[i] = square[i2][j1];
rez[i + 1] = square[i1][j2];
}
}
return rez;
}
char* latin::operator-(char*s)
{
int i1, i2, j1, j2;
char*rez = new char[strlen(s) + 1];
for (int i = 0; i <= strlen(s); i += 2)
if (i == strlen(s))
{
rez[i] = '\0';
rez[i + 1] = '\0';
}
else if (s[i + 1] == '\0')
{
rez[i] = s[i];
rez[i + 1] = '\0';
}
else if (s[i] == 'J')
{
rez[i] = s[i + 1];
rez[i + 1] = s[i + 1];
}
else
{
for (int k = 0; k < 5; k++)
for (int j = 0; j < 5; j++)
{
if (s[i] == square[k][j])
{
i1 = k;
j1 = j;
}
if (s[i + 1] == square[k][j])
{
i2 = k;
j2 = j;
}
}
if (i1 == i2)
{
rez[i] = square[i1][j1 - 1 < 0 ? 4 : j1 - 1];
rez[i + 1] = square[i2][j2 - 1 < 0 ? 4 : j2 - 1];
}
else if (j1 == j2)
{
rez[i] = square[i1 - 1 < 0 ? 4 : i1 - 1][j1];
rez[i + 1] = square[i2 - 1 < 0 ? 4 : i2 - 1][j2];
}
else
{
rez[i] = square[i2][j1];
rez[i + 1] = square[i1][j2];
}
}
return rez;
}

char* ukr::operator+(char*s)
{
int i1, i2, j1, j2;
char*rez = new char[strlen(s) + 1];
for (int i = 0; i <= strlen(s); i += 2)
if (s[i] == '\0' || s[i + 1] == '\0')
{
rez[i] = s[i];
rez[i + 1] = '\0';
}
else if (s[i] == s[i + 1])
{
rez[i] = 'É';
rez[i + 1] = s[i];
}
else
{
for (int k = 0; k < 4; k++)
for (int j = 0; j < 7; j++)
{
if (s[i] == square[k][j])
{
i1 = k;
j1 = j;
}
if (s[i + 1] == square[k][j])
{
i2 = k;
j2 = j;
}
}
if (i1 == i2)
{
rez[i] = square[i1][j1 < 6 ? j1 + 1 : 0];
rez[i + 1] = square[i2][j2 < 6 ? j2 + 1 : 0];
}
else if (j1 == j2)
{
rez[i] = square[i1 < 3 ? i1 + 1 : 0][j1];
rez[i + 1] = square[i2 < 3 ? i2 + 1 : 0][j2];
}
else
{
rez[i] = square[i2][j1];
rez[i + 1] = square[i1][j2];
}
}
return rez;
}

char* ukr::operator-(char*s)
{
int i1, i2, j1, j2;
char*rez = new char[strlen(s) + 1];
for (int i = 0; i <= strlen(s); i += 2)
if (i == strlen(s))
{
rez[i] = '\0';
rez[i + 1] = '\0';
}
else if (s[i + 1] == '\0')
{
rez[i] = s[i];
rez[i + 1] = '\0';
}
else if (s[i] == 'É')
{
rez[i] = s[i + 1];
rez[i + 1] = s[i + 1];
}
else
{
for (int k = 0; k < 4; k++)
for (int j = 0; j < 7; j++)
{
if (s[i] == square[k][j])
{
i1 = k;
j1 = j;
}
if (s[i + 1] == square[k][j])
{
i2 = k;
j2 = j;
}
}
if (i1 == i2)
{
rez[i] = square[i1][j1 - 1 < 0 ? 6 : j1 - 1];
rez[i + 1] = square[i2][j2 - 1 < 0 ? 6 : j2 - 1];
}
else if (j1 == j2)
{
rez[i] = square[i1 - 1 < 0 ? 3 : i1 - 1][j1];
rez[i + 1] = square[i2 - 1 < 0 ? 3 : i2 - 1][j2];
}
else
{
rez[i] = square[i2][j1];
rez[i + 1] = square[i1][j2];
}
}
return rez;
}

Результат роботи
Алгоритм Віженера
Програмна реалізація
#define LATIN /*UKR*/
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
struct latin
{
char alphabet[26];
char keyword[20];
int index;
latin(char*);
char* operator+(char*);
char* operator-(char*);
};
struct ukr
{
char alphabet[29];
char keyword[20];
int index;
ukr(char*);
char* operator+(char*);
char* operator-(char*);
};

char* prep_l(char*);
char* prep_u(char*);

void main()
{
setlocale(0, "");
bool f;
#ifdef LATIN
f = true;
#endif
#ifdef UKR
f = false;
#endif
char keyword[20]{}, *el = "He is rather strict with us, but always fair.",
*um = "Êîëè çàéìàºøüñÿ ïîëiòèêîþ, äié, ÿê ãðîìàäÿíèí.";
char text[50]{}, cipher[50]{}, rez[50]{};;
if (f)
cout << strcpy(text, prep_l(strcpy(text, el))) << endl;
else
cout << strcpy(text, prep_u(strcpy(text, um))) << endl;

if (f)
{
cout << "enter your keyword at least 20 characters: ";
cin >> keyword;
strcpy(keyword, prep_l(keyword));
}
else
{
cout << " ââåäiòü êëþ÷îâå ñëîâî íå áiëüø íiæ 20 ñèìâîëiâ: ";
SetConsoleCP(1251);
cin >> keyword;
SetConsoleCP(866);
strcpy(keyword, prep_u(keyword));
}
#ifdef LATIN
latin sl(keyword);
strcpy(cipher, sl + text);
#endif
#ifdef UKR
ukr su(keyword);
strcpy(cipher, su + text);
#endif
cout << cipher << endl;
#ifdef LATIN
strcpy(rez, sl - cipher);
#endif
#ifdef UKR
strcpy(rez, su - cipher);
#endif
cout << rez << endl;
}

char* prep_l(char*temp)
{
char s[50]{};
char * path;
char r[12] = " ,./!?;:\"\'";
path = strtok(temp, r);
do {
strcat(s, path);
} while (path = strtok(NULL, r));
path = s;
while (*path)
{
*path = toupper(*path);
if (*path == 'J')
*path = 'I';
path++;
}
return s;
}
char* prep_u(char*temp)
{
int c;
char s[50]{};
char * path;
char r[14] = "üÜ ,./!?;:\"\'";
path = strtok(temp, r);
do {
strcat(s, path);
} while (path = strtok(NULL, r));
path = s;
while (*path)
{
if (*path == 'i')
*path = 'I';
else if (*path == 'º' || *path == 'ª')
*path = 'Å';
else if (*path == '¿' || *path == '¯')
*path = 'I';
else if (*path == 'é' || *path == 'É')
*path = 'È';
else if (*path > -33)
*path -= 32;
path++;
}
return s;
}

latin::latin(char*s)
{
strcpy(keyword, s);
strcpy(alphabet, "ABCDEFGHIKLMNOPQRSTUVWXYZ");
index = 0;
}

ukr::ukr(char*s)
{
strcpy(keyword, s);
strcpy(alphabet, "ÀÁÂÃÄÅÆÇÈIÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÞß");
index = 0;
}

char* latin::operator+(char*s)
{
int j;
char*rez = new char[strlen(s) + 1];
for (int i = 0; i < strlen(s); i++)
{
for (j = 0; j < 25; j++)
if (s[i] == alphabet[j])
break;
rez[i] = alphabet[(j + keyword[index] - 65) % 25];
index++;
if (index == strlen(keyword))
index = 0;
}
rez[strlen(s)] = '\0';
return rez;
}
char* latin::operator-(char*s)
{
int j, n;
index = 0;
char*rez = new char[strlen(s) + 1];
for (int i = 0; i < strlen(s); i++)
{
for (j = 0; j < 25; j++)
if (s[i] == alphabet[j])
break;
rez[i] = alphabet[(n = j - keyword[index] + 65) < 0 ? 25 + n : n];
index++;
if (index == strlen(keyword))
index = 0;
}
rez[strlen(s)] = '\0';
return rez;
}

char* ukr::operator+(char*s)
{
int j, k;
char*rez = new char[strlen(s) + 1];
for (int i = 0; i < strlen(s); i++)
{
for (j = 0; j < 28; j++)
if (s[i] == alphabet[j])
break;
for (k = 0; k < 28; k++)
if (keyword[index] == alphabet[k])
break;
rez[i] = alphabet[(j + k) % 28];
index++;
if (index == strlen(keyword))
index = 0;
}
rez[strlen(s)] = '\0';
return rez;
}

char* ukr::operator-(char*s)
{
int j, k, n;
index = 0;
char*rez = new char[strlen(s) + 1];
for (int i = 0; i < strlen(s); i++)
{
for (j = 0; j < 28; j++)
if (s[i] == alphabet[j])
break;
for (k = 0; k < 28; k++)
if (keyword[index] == alphabet[k])
break;
rez[i] = alphabet[(n = j - k) < 0 ? 28 + n : n];
index++;
if (index == strlen(keyword))
index = 0;
}
rez[strlen(s)] = '\0';
return rez;
}
Результат роботи
Висновок
У даній лабораторній роботі було створено 3 алгоритми шифрування та
дешифрування інформації. Було вивчено теорію, як працюють дані
алгоритми, створено програмну.

You might also like