Практические работы Часть 2 / Практическая работа 5 / Практическая работа 5 Отчет
.docПрактическая работа №5
Приемы работы с защищенными программами
Цель: освоить технологию работы с программой шифрования и дешифрования.
Ход работы
Задание: Шифрование текстовых строк. Алгоритм Triple DES.
Листинг Form1.cs:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
namespace TripleDES {
public partial class Form1 : Form {
protected byte[] IVector = null;
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Txt files (*.txt)|*.txt|All files (*.*)|*.*";
ofd.ShowDialog();
if (ofd.FileName != "") {
textBox1.Text = new FileInfo(ofd.FileName).OpenText().ReadToEnd();
}
}
private void button2_Click(object sender, EventArgs e) {
SaveFileDialog sfd = new SaveFileDialog();
sfd.InitialDirectory = "C:\\";
sfd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
sfd.ShowDialog();
textBox3.Text = sfd.FileName;
}
private void button3_Click(object sender, EventArgs e) {
try {
TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();
string temp = null;
for (int i = 0; i < tDESalg.IV.Length; i++) temp += tDESalg.IV[i].ToString();
textBox4.Text = temp;
label9.Text = temp;
IVector = tDESalg.IV;
string sData = textBox1.Text;
string strKey = textBox2.Text;
byte[] bKey = new byte[24];
for (int i = 0, j = 0; i < 24; i++, j++) {
if (j == strKey.Length) j = 0;
bKey[i] = (byte)strKey[j];
}
string FileName = textBox3.Text;
CryptText.EncryptTextToFile(sData, FileName, bKey, tDESalg.IV);
MessageBox.Show("Текст успешно зашифрован !!!");
}
catch (Exception exc) {
MessageBox.Show(exc.Message);
}
}
private void button4_Click(object sender, EventArgs e) {
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Txt files (*.txt)|*.txt|All files (*.*)|*.*";
ofd.ShowDialog();
if (ofd.FileName != "") textBox5.Text = new FileInfo(ofd.FileName).FullName;
}
private void button5_Click(object sender, EventArgs e) {
try {
TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();
string strKey = textBox6.Text;
byte[] bKey = new byte[24];
for (int i = 0, j = 0; i < 24; i++, j++) {
if (j == strKey.Length) j = 0;
bKey[i] = (byte)strKey[j];
}
string FileName = textBox5.Text;
textBox7.Clear();
textBox7.Text += CryptText.DecryptTextFromFile(FileName, bKey, IVector);
MessageBox.Show("Текст успешно дешифрован !!!");
}
catch (Exception exc) {
MessageBox.Show(exc.Message);
}
}
private void Form1_Load(object sender, EventArgs e) {
textBox3.Text = @"C:\Encoded.txt";
textBox2.Text = "key";
textBox6.Text = "key";
}
}
}
Листинг CryptText.cs:
using System;
using System.Security.Cryptography;
using System.IO;
using System.Windows.Forms;
namespace TripleDES {
public class CryptText {
public CryptText() { }
public static void EncryptTextToFile(string Data, string FileName,
byte[] Key, byte[] IV) {
try {
FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate);
CryptoStream cStream = new CryptoStream(fStream,
new TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV),
CryptoStreamMode.Write);
StreamWriter sWriter = new StreamWriter(cStream);
sWriter.WriteLine(Data);
sWriter.Close();
cStream.Close();
fStream.Close();
}
catch (CryptographicException e) {
MessageBox.Show("A Cryptographic error occurred: {0}", e.Message);
}
catch (UnauthorizedAccessException e) {
MessageBox.Show("A file access error occurred: {0}", e.Message);
}
}
public static string DecryptTextFromFile(string FileName, byte[] Key, byte[] IV) {
try {
FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate);
CryptoStream cStream = new CryptoStream(fStream,
new TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV),
CryptoStreamMode.Read);
StreamReader sReader = new StreamReader(cStream);
string val = sReader.ReadToEnd();
sReader.Close();
cStream.Close();
fStream.Close();
return val;
}
catch (CryptographicException e) {
MessageBox.Show("A Cryptographic error occurred: {0}", e.Message);
return null;
}
catch (UnauthorizedAccessException e) {
MessageBox.Show("A file access error occurred: {0}", e.Message);
return null;
}
}
}
}
Результаты выполнения программы:
|
|
|