Симетричні блокові шифри на основі мережі Фейстеля

Інформація про навчальний заклад

ВУЗ:
Національний університет Львівська політехніка
Інститут:
Не вказано
Факультет:
Не вказано
Кафедра:
Кафедра ЕОМ

Інформація про роботу

Рік:
2019
Тип роботи:
Лабораторна робота
Предмет:
Захист інформації в комп’ютерних системах
Варіант:
2 3 8 10 12 15 17

Частина тексту файла (без зображень, графіків і формул):

Міністерство освіти і науки України Національний університет „Львівська політехніка” Кафедра ЕОМ Звіт з лабораторної роботи №5 з дисципліни:”Захист інформації в комп’ютерних системах ” на тему:” Симетричні блокові шифри на основі мережі Фейстеля ” Львів 2019 / Хід роботи / Рис.1 Первинне повідомлення / Рис.2 Шифроване повідомлення / Рис.3 Кінцевий BMP-файл / Рис.4 Результат роботи програми Висновок:Під час виконання лабораторної роботи була розроблена програма,яка реалізує симетричний алгоритм на основі мережі Фейстеля Код програми Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace protect_inf_LR1 { public partial class Form1 : Form { private const int sizeOfBlock = 128; private const int sizeOfChar = 16; private const int shiftKey = 2; private const int quantityOfRounds = 16; string[] Blocks; public Form1() { InitializeComponent(); } private void buttonEncrypt_Click(object sender, EventArgs e) { if (textBoxEncodeKeyWord.Text.Length > 0) { string s = ""; string key = textBoxEncodeKeyWord.Text; StreamReader sr = new StreamReader("in.txt"); while (!sr.EndOfStream) { s += sr.ReadLine(); } sr.Close(); s = StringToRightLength(s); CutStringIntoBlocks(s); key = CorrectKeyWord(key, s.Length / (2 * Blocks.Length)); textBoxEncodeKeyWord.Text = key; key = StringToBinaryFormat(key); for (int j = 0; j < quantityOfRounds; j++) { for (int i = 0; i < Blocks.Length; i++) Blocks[i] = EncodeDES_One_Round(Blocks[i], key); key = KeyToNextRound(key); } key = KeyToPrevRound(key); textBoxDecodeKeyWord.Text = StringFromBinaryToNormalFormat(key); string result = ""; for (int i = 0; i < Blocks.Length; i++) result += Blocks[i]; StreamWriter sw = new StreamWriter("out1.txt"); sw.WriteLine(StringFromBinaryToNormalFormat(result)); sw.Close(); Process.Start("out1.txt"); } else MessageBox.Show("Введите ключевое слово!"); } private void buttonDecipher_Click(object sender, EventArgs e) { if (textBoxDecodeKeyWord.Text.Length > 0) { string s = ""; string key = StringToBinaryFormat(textBoxDecodeKeyWord.Text); StreamReader sr = new StreamReader("out1.txt"); while (!sr.EndOfStream) { s += sr.ReadLine(); } sr.Close(); s = StringToBinaryFormat(s); CutBinaryStringIntoBlocks(s); for (int j = 0; j < quantityOfRounds; j++) { for (int i = 0; i < Blocks.Length; i++) Blocks[i] = DecodeDES_One_Round(Blocks[i], key); key = KeyToPrevRound(key); } key = KeyToNextRound(key); textBoxEncodeKeyWord.Text = StringFromBinaryToNormalFormat(key); string result = ""; for (int i = 0; i < Blocks.Length; i++) result += Blocks[i]; StreamWriter sw = new StreamWriter("out2.txt"); sw.WriteLine(StringFromBinaryToNormalFormat(result)); sw.Close(); Process.Start("out2.txt"); } else MessageBox.Show("Введите ключевое слово!"); } private string StringToRightLength(string input) { while (((input.Length * sizeOfChar) % sizeOfBlock) != 0) input += "#"; return input; } private void CutStringIntoBlocks(string input) { Blocks = new string[(input.Length * sizeOfChar) / sizeOfBlock]; int lengthOfBlock = input.Length / Blocks.Length; for (int i = 0; i < Blocks.Length; i++) { Blocks[i] = input.Substring(i * lengthOfBlock, lengthOfBlock); Blocks[i] = StringToBinaryFormat(Blocks[i]); } } private void CutBinaryStringIntoBlocks(string input) { Blocks = new string[input.Length / sizeOfBlock]; int lengthOfBlock = input.Length / Blocks.Length; for (int i = 0; i < Blocks.Length; i++) Blocks[i] = input.Substring(i * lengthOfBlock, lengthOfBlock); } private string StringToBinaryFormat(string input) { string output = ""; for (int i = 0; i < input.Length; i++) { string char_binary = Convert.ToString(input[i], 2); while (char_binary.Length < sizeOfChar) char_binary = "0" + char_binary; output += char_binary; } return output; } private string CorrectKeyWord(string input, int lengthKey) { if (input.Length > lengthKey) input = input.Substring(0, lengthKey); else while (input.Length < lengthKey) input = "0" + input; return input; } private string EncodeDES_One_Round(string input, string key) { string L = input.Substring(0, input.Length / 2); string R = input.Substring(input.Length / 2, input.Length / 2); return (R + XOR(L, f(R, key))); } private string DecodeDES_One_Round(string input, string key) { string L = input.Substring(0, input.Length / 2); string R = input.Substring(input.Length / 2, input.Length / 2); return (XOR(f(L, key), R) + L); } private string XOR(string s1, string s2) { string result = ""; for (int i = 0; i < s1.Length; i++) { bool a = Convert.ToBoolean(Convert.ToInt32(s1[i].ToString())); bool b = Convert.ToBoolean(Convert.ToInt32(s2[i].ToString())); if (a ^ b) result += "1"; else result += "0"; } return result; } private string f(string s1, string s2) { return XOR(s1, s2); } private string KeyToNextRound(string key) { for (int i = 0; i < shiftKey; i++) { key = key[key.Length - 1] + key; key = key.Remove(key.Length - 1); } return key; } private string KeyToPrevRound(string key) { for (int i = 0; i < shiftKey; i++) { key = key + key[0]; key = key.Remove(0, 1); } return key; } private string StringFromBinaryToNormalFormat(string input) { string output = ""; while (input.Length > 0) { string char_binary = input.Substring(0, sizeOfChar); input = input.Remove(0, sizeOfChar); int a = 0; int degree = char_binary.Length - 1; foreach (char c in char_binary) a += Convert.ToInt32(c.ToString()) * (int)Math.Pow(2, degree--); output += ((char)a).ToString(); } return output; } private void Form1_Load(object sender, EventArgs e) { } } } Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace protect_inf_LR1 { static class Program { /// <summary> /// Главная точка входа для приложения. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
Антиботан аватар за замовчуванням

23.10.2019 20:26

Коментарі

Ви не можете залишити коментар. Для цього, будь ласка, увійдіть або зареєструйтесь.

Ділись своїми роботами та отримуй миттєві бонуси!

Нічого не вибрано
0%

Оголошення від адміністратора

Антиботан аватар за замовчуванням

Подякувати Студентському архіву довільною сумою

Admin

26.02.2023 12:38

Дякуємо, що користуєтесь нашим архівом!