Дипломы, курсовые, рефераты, контрольные...
Срочная помощь в учёбе

Заключение. 
Программная реализация функции хеширования MD5

РефератПомощь в написанииУзнать стоимостьмоей работы

TransI (ref A, B, C, D, 4,6,61);TransI (ref D, A, B, C, 11,10,62);TransI (ref C, D, A, B, 2,15,63);TransI (ref B, C, D, A, 9,21,64); TransI (ref A, B, C, D, 0,6,49);TransI (ref D, A, B, C, 7,10,50);TransI (ref C, D, A, B, 14,15,51);TransI (ref B, C, D, A, 5,21,52); TransH (ref A, B, C, D, 13,4,41);TransH (ref D, A, B, C, 0,11,42);TransH (ref C, D, A, B, 3,16,43);TransH (ref B, C, D, A, 6,23,44); TransH (ref… Читать ещё >

Заключение. Программная реализация функции хеширования MD5 (реферат, курсовая, диплом, контрольная)

В результате выполнения данной работы было написано приложение, позволяющее получить хеш-значение с помощью алгоритма хеширования MD5. Был изучен и реализован на практике алгоритм хеширования MD5.

В программе предусмотрено 2 режимы работы: текстовый и файловый, а также вывод полученного хеш-значения на экран компьютера и в текстовый файл.

>

Исходные коды программы.

MainForm.cs.

using System;

using System.Collections.Generic;

using System. ComponentModel;

using System. Data;

using System. Drawing;

using System. Linq;

using System. Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System. IO;

using Microsoft. VisualBasic;

namespace MD5.

public partial class MainForm: Form.

MD5 md5;

public string fileName;

public MainForm ().

InitializeComponent ();

checkVisible ();

md5 = new MD5();

printHash (textTextBox.Text);

}.

private void fileButton_Click (object sender, EventArgs e).

{.

OpenFileDialog dialog = new OpenFileDialog ();

if (dialog.ShowDialog () == DialogResult. OK).

{.

fileTextBox.Text = dialog. FileName;

}.

}.

private void printHash (string str).

{.

md5.Value = str;

outputTextBox.Text = md5. FingerPrint;

}.

private void checkVisible ().

{.

fileLabel.Visible = fileRadioButton. Checked;

fileTextBox.Visible = fileRadioButton. Checked;

fileButton.Visible = fileRadioButton. Checked;

textLabel.Visible = textRadioButton. Checked;

textTextBox.Visible = textRadioButton. Checked;

fileTextBox.Text = «» ;

textTextBox.Text = «» ;

}.

private void fileRadioButton_CheckedChanged (object sender, EventArgs e).

{.

checkVisible ();

string filePath = fileTextBox. Text;

if (!File.Exists (filePath)).

{.

outputTextBox.Text = «» ;

}.

}.

private void textRadioButton_CheckedChanged (object sender, EventArgs e).

{.

checkVisible ();

printHash (textTextBox.Text);

}.

private void textTextBox_TextChanged (object sender, EventArgs e).

{.

printHash (textTextBox.Text);

}.

private void fileTextBox_TextChanged (object sender, EventArgs e).

{.

string filePath = fileTextBox. Text;

if (File.Exists (filePath)).

{.

using (StreamReader sr = new StreamReader (filePath)).

{.

printHash (sr.ReadToEnd ());

}.

}.

else.

outputTextBox.Text = «» ;

}.

private void saveButton_Click (object sender, EventArgs e).

{.

try.

{.

using (StreamWriter outputFile = new StreamWriter (outputFileTextBox.Text)).

{.

outputFile.WriteLine (outputTextBox.Text);

}.

}.

catch.

{.

MessageBox.Show («Неправильное имя файла»);

}.

}.

private void outputButton_Click (object sender, EventArgs e).

{.

FolderBrowserDialog dialog = new FolderBrowserDialog ();

if (dialog.ShowDialog () == DialogResult. OK).

{.

string path = dialog. SelectedPath;

FileNameForm nameForm = new FileNameForm (this);

nameForm.ShowDialog ();

outputFileTextBox.Text = Path. Combine (path, fileName);

}.

}.

private void оПрограммеToolStripMenuItem_Click (object sender, EventArgs e).

{.

AboutBox about = new AboutBox ();

about.ShowDialog ();

}.

private void выходToolStripMenuItem_Click (object sender, EventArgs e).

{.

Close ();

}.

}.

}.

Helper.cs.

using System;

namespace MD5.

{.

public enum MD5InitializerConstant: uint.

{.

A =0×67 452 301,.

B=0xEFCDAB89,.

C=0×98BADCFE,.

D=0X10325476.

}.

sealed public class Digest.

{.

public uint A;

public uint B;

public uint C;

public uint D;

public Digest ().

{.

A=(uint)MD5InitializerConstant.A;

B=(uint)MD5InitializerConstant.B;

C=(uint)MD5InitializerConstant.C;

D=(uint)MD5InitializerConstant.D;

}.

public override string ToString ().

{.

string st ;

st= MD5Helper. ReverseByte (A).ToString («X8»)+.

MD5Helper.ReverseByte (B).ToString («X8»)+.

MD5Helper.ReverseByte©.ToString («X8»)+.

MD5Helper.ReverseByte (D).ToString («X8»);

return st;

}.

}.

sealed public class MD5Helper.

{.

private MD5Helper (){}.

public static uint RotateLeft (uint uiNumber, ushort shift).

{.

return ((uiNumber >> 32-shift)|(uiNumber<

}.

public static uint ReverseByte (uint uiNumber).

{.

return (((uiNumber & 0×00ff) <<24) |.

  • (uiNumber >>24) |
  • ((uiNumber & 0×00ff0000) >>8) |
  • ((uiNumber & 0×0000ff00) <<8));

}.

}.

public class MD5ChangingEventArgs: EventArgs.

{.

public readonly byte[] NewData;

public MD5ChangingEventArgs (byte [] data).

{.

byte [] NewData = new byte[data.Length];

for (int i =0; i.

NewData[i]=data[i];

}.

public MD5ChangingEventArgs (string data).

{.

byte [] NewData = new byte[data.Length];

for (int i =0; i.

NewData[i]=(byte)data[i];

}.

}.

public class MD5ChangedEventArgs: EventArgs.

{.

public readonly byte[] NewData;

public readonly string FingerPrint;

public MD5ChangedEventArgs (byte [] data, string HashedValue).

{.

byte [] NewData = new byte[data.Length];

for (int i =0; i.

NewData[i]=data[i];

FingerPrint=HashedValue;

}.

public MD5ChangedEventArgs (string data, string HashedValue).

{.

byte [] NewData = new byte[data.Length];

for (int i =0; i.

NewData[i]=(byte)data[i];

FingerPrint=HashedValue;

}.

}.

}.

MD5.cs.

using System;

namespace MD5.

{.

public class MD5.

{.

protected readonly static uint [] T =new uint[64].

{0xd76aa478,0xe8c7b756,0×24 2070db, 0xc1bdceee,.

  • 0xf57c0faf, 0×4787c62a, 0xa8304613,0xfd469501,
  • 0x698098d8,0x8b44f7af, 0xffff5bb1,0×895cd7be,
  • 0x6b901122,0xfd987193,0xa679438e, 0×49b40821,
  • 0xf61e2562,0xc040b340,0×265e5a51,0xe9b6c7aa,
  • 0xd62f105d, 0×2 441 453,0xd8a1e681,0xe7d3fbc8,
  • 0x21e1cde6,0xc33707d6,0xf4d50d87,0×455a14ed,
  • 0xa9e3e905,0xfcefa3f8,0×676f02d9,0x8d2a4c8a,
  • 0xfffa3942,0×8771f681,0x6d9d6122,0xfde5380c,
  • 0xa4beea44,0x4bdecfa9,0xf6bb4b60,0xbebfbc70,
  • 0x289b7ec6,0xeaa127fa, 0xd4ef3085,0×4881d05,
  • 0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665,
  • 0xf4292244,0×432aff97,0xab9423a7,0xfc93a039,
  • 0x655b59c3,0x8f0ccc92,0xffeff47d, 0×85845dd1,
  • 0x6fa87e4f, 0xfe2ce6e0,0xa3014314,0x4e0811a1,
  • 0xf7537e82,0xbd3af235,0x2ad7d2bb, 0xeb86d391};

protected uint [] X = new uint [16];

protected Digest dgFingerPrint;

protectedbyte [] m_byteInput;

public delegate void ValueChanging (object sender, MD5ChangingEventArgs Changing);

public delegate void ValueChanged (object sender, MD5ChangedEventArgs Changed);

public event ValueChanging OnValueChanging;

public event ValueChanged OnValueChanged;

public string Value.

{.

get.

{.

string st ;

char [] tempCharArray= new Char[m_byteInput.Length];

for (int i =0; i.

tempCharArray[i]=(char)m_byteInput[i];

st= new String (tempCharArray);

return st;

}.

set.

{.

if (this.OnValueChanging ≠null).

this.OnValueChanging (this, new MD5ChangingEventArgs (value));

m_byteInput=new byte[value.Length];

for (int i =0; i.

m_byteInput[i]=(byte)value[i];

dgFingerPrint=CalculateMD5Value ();

if (this.OnValueChanged ≠null).

this.OnValueChanged (this, new MD5ChangedEventArgs (value, dgFingerPrint. ToString ()));

}.

}.

public byte [] ValueAsByte.

{.

get.

{.

byte [] bt = new byte[m_byteInput.Length];

for (int i =0; i.

bt[i]=m_byteInput[i];

return bt;

}.

set.

{.

if (this.OnValueChanging ≠null).

this.OnValueChanging (this, new MD5ChangingEventArgs (value));

m_byteInput=new byte[value.Length];

for (int i =0; i.

m_byteInput[i]=value[i];

dgFingerPrint=CalculateMD5Value ();

if (this.OnValueChanged ≠null).

this.OnValueChanged (this, new MD5ChangedEventArgs (value, dgFingerPrint. ToString ()));

}.

}.

public string FingerPrint.

{.

get.

{.

return dgFingerPrint. ToString ();

}.

}.

public MD5().

{.

Value="" ;

}.

protected Digest CalculateMD5Value ().

{.

byte [] bMsg;

uint N;

Digest dg =new Digest ();

bMsg=CreatePaddedBuffer ();

N=(uint)(bMsg.Length*8)/32;

for (uint i=0; i.

{.

CopyBlock (bMsg, i);

PerformTransformation (ref dg. A, ref dg. B, ref dg. C, ref dg. D);

}.

return dg;

}.

protected void TransF (ref uint a, uint b, uint c, uint d, uint k, ushort s, uint i).

{.

a = b + MD5Helper. RotateLeft ((a + ((b&c) | (~(b)&d)) + X[k] + T[i-1]), s);

}.

protected void TransG (ref uint a, uint b, uint c, uint d, uint k, ushort s, uint i).

{.

a = b + MD5Helper. RotateLeft ((a + ((b&d) | (c & ~d)) + X[k] + T[i-1]), s);

}.

protected void TransH (ref uint a, uint b, uint c, uint d, uint k, ushort s, uint i).

{.

a = b + MD5Helper. RotateLeft ((a + (b^c^d) + X[k] + T[i-1]), s);

}.

protected void TransI (ref uint a, uint b, uint c, uint d, uint k, ushort s, uint i).

{.

a = b + MD5Helper. RotateLeft ((a + (c^(b|~d))+ X[k] + T[i-1]), s);

}.

protected void PerformTransformation (ref uint A, ref uint B, ref uint C, ref uint D).

{.

uint AA, BB, CC, DD;

AA=A;

BB=B;

CC=C;

DD=D;

/* Round 1.

  • * [ABCD 0 7 1] [DABC 1 12 2] [CDAB 2 17 3] [BCDA 3 22 4]
  • * [ABCD 4 7 5] [DABC 5 12 6] [CDAB 6 17 7] [BCDA 7 22 8]
  • * [ABCD 8 7 9] [DABC 9 12 10] [CDAB 10 17 11] [BCDA 11 22 12]
  • * [ABCD 12 7 13] [DABC 13 12 14] [CDAB 14 17 15] [BCDA 15 22 16]
  • * * */

TransF (ref A, B, C, D,0,7,1);TransF (ref D, A, B, C,1,12,2);TransF (ref C, D, A, B,2,17,3);TransF (ref B, C, D, A,3,22,4);

TransF (ref A, B, C, D,4,7,5);TransF (ref D, A, B, C,5,12,6);TransF (ref C, D, A, B,6,17,7);TransF (ref B, C, D, A,7,22,8);

TransF (ref A, B, C, D,8,7,9);TransF (ref D, A, B, C,9,12,10);TransF (ref C, D, A, B,10,17,11);TransF (ref B, C, D, A,11,22,12);

TransF (ref A, B, C, D,12,7,13);TransF (ref D, A, B, C,13,12,14);TransF (ref C, D, A, B,14,17,15);TransF (ref B, C, D, A,15,22,16);

/** Round 2.

  • **[ABCD 1 5 17] [DABC 6 9 18] [CDAB 11 14 19] [BCDA 0 20 20]
  • *[ABCD 5 5 21] [DABC 10 9 22] [CDAB 15 14 23] [BCDA 4 20 24]
  • *[ABCD 9 5 25] [DABC 14 9 26] [CDAB 3 14 27] [BCDA 8 20 28]
  • *[ABCD 13 5 29] [DABC 2 9 30] [CDAB 7 14 31] [BCDA 12 20 32]
  • */

TransG (ref A, B, C, D,1,5,17);TransG (ref D, A, B, C,6,9,18);TransG (ref C, D, A, B,11,14,19);TransG (ref B, C, D, A,0,20,20);

TransG (ref A, B, C, D,5,5,21);TransG (ref D, A, B, C,10,9,22);TransG (ref C, D, A, B,15,14,23);TransG (ref B, C, D, A,4,20,24);

TransG (ref A, B, C, D,9,5,25);TransG (ref D, A, B, C,14,9,26);TransG (ref C, D, A, B,3,14,27);TransG (ref B, C, D, A,8,20,28);

TransG (ref A, B, C, D,13,5,29);TransG (ref D, A, B, C,2,9,30);TransG (ref C, D, A, B,7,14,31);TransG (ref B, C, D, A,12,20,32);

/* Round 3.

  • * [ABCD 5 4 33] [DABC 8 11 34] [CDAB 11 16 35] [BCDA 14 23 36]
  • * [ABCD 1 4 37] [DABC 4 11 38] [CDAB 7 16 39] [BCDA 10 23 40]
  • * [ABCD 13 4 41] [DABC 0 11 42] [CDAB 3 16 43] [BCDA 6 23 44]
  • * [ABCD 9 4 45] [DABC 12 11 46] [CDAB 15 16 47] [BCDA 2 23 48]
  • * */

TransH (ref A, B, C, D,5,4,33);TransH (ref D, A, B, C,8,11,34);TransH (ref C, D, A, B,11,16,35);TransH (ref B, C, D, A,14,23,36);

TransH (ref A, B, C, D,1,4,37);TransH (ref D, A, B, C,4,11,38);TransH (ref C, D, A, B,7,16,39);TransH (ref B, C, D, A,10,23,40);

TransH (ref A, B, C, D,13,4,41);TransH (ref D, A, B, C,0,11,42);TransH (ref C, D, A, B,3,16,43);TransH (ref B, C, D, A,6,23,44);

TransH (ref A, B, C, D,9,4,45);TransH (ref D, A, B, C,12,11,46);TransH (ref C, D, A, B,15,16,47);TransH (ref B, C, D, A,2,23,48);

/*Round 4.

  • *[ABCD 0 6 49] [DABC 7 10 50] [CDAB 14 15 51] [BCDA 5 21 52]
  • *[ABCD 12 6 53] [DABC 3 10 54] [CDAB 10 15 55] [BCDA 1 21 56]
  • *[ABCD 8 6 57] [DABC 15 10 58] [CDAB 6 15 59] [BCDA 13 21 60]
  • *[ABCD 4 6 61] [DABC 11 10 62] [CDAB 2 15 63] [BCDA 9 21 64]
  • * */

TransI (ref A, B, C, D,0,6,49);TransI (ref D, A, B, C,7,10,50);TransI (ref C, D, A, B,14,15,51);TransI (ref B, C, D, A,5,21,52);

TransI (ref A, B, C, D,12,6,53);TransI (ref D, A, B, C,3,10,54);TransI (ref C, D, A, B,10,15,55);TransI (ref B, C, D, A,1,21,56);

TransI (ref A, B, C, D,8,6,57);TransI (ref D, A, B, C,15,10,58);TransI (ref C, D, A, B,6,15,59);TransI (ref B, C, D, A,13,21,60);

TransI (ref A, B, C, D,4,6,61);TransI (ref D, A, B, C,11,10,62);TransI (ref C, D, A, B,2,15,63);TransI (ref B, C, D, A,9,21,64);

A=A+AA;

B=B+BB;

C=C+CC;

D=D+DD;

}.

protected byte[] CreatePaddedBuffer ().

{.

uint pad;

byte [] bMsg;

ulong sizeMsg;

uint sizeMsgBuff;

int temp=(448-((m_byteInput.Length*8)%512));

pad = (uint)((temp+512)%512);

if (pad==0).

pad=512;

sizeMsgBuff= (uint) ((m_byteInput.Length)+ (pad/8)+8);

sizeMsg=(ulong)m_byteInput.Length*8;

bMsg=new byte[sizeMsgBuff];

for (int i =0; i.

bMsg[i]=m_byteInput[i];

bMsg[m_byteInput.Length]|=0×80;

for (int i =8; i >0;i—).

bMsg[sizeMsgBuff-i]=(byte) (sizeMsg>>((8-i)*8) & 0×00ff);

return bMsg;

}.

protected void CopyBlock (byte[] bMsg, uint block).

{.

block=block<<6;

for (uint j=0; j<61;j+=4).

{.

X[j>>2]=(((uint) bMsg[block+(j+3)]) <<24) |.

  • (((uint) bMsg[block+(j+2)]) <<16) |
  • (((uint) bMsg[block+(j+1)]) <<8) |
  • (((uint) bMsg[block+(j)])) ;

}.

}.

}.

}.

FileNameForm.cs.

using System;

using System.Collections.Generic;

using System. ComponentModel;

using System. Data;

using System. Drawing;

using System. Linq;

using System. Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace MD5.

{.

public partial class FileNameForm: Form.

{.

MainForm mainForm;

public FileNameForm (MainForm mainForm).

{.

this.mainForm = mainForm;

mainForm.fileName = «» ;

InitializeComponent ();

}.

private void okButton_Click (object sender, EventArgs e).

{.

mainForm.fileName = fileNameTextBox. Text;

Close ();

}.

private void cancelButton_Click (object sender, EventArgs e).

{.

Close ();

}.

}.

}.

AboutBox.cs

using System;

using System.Collections.Generic;

using System. ComponentModel;

using System. Drawing;

using System. Linq;

using System. Reflection;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace MD5.

{.

partial class AboutBox: Form.

{.

public AboutBox ().

{.

InitializeComponent ();

this.Text = «О программе» ;

this.labelProductName.Text = «Курсовой проект» ;

this.labelVersion.Text = String. Format («Version {0}», AssemblyVersion);

this.labelCopyright.Text = «Шустикова Дарья» ;

this.labelCompanyName.Text = «А-05−13» ;

this.textBoxDescription.Text = «Программная реализация функции хеширования MD5» ;

}.

#region Assembly Attribute Accessors.

public string AssemblyTitle.

{.

get.

{.

object[] attributes = Assembly. GetExecutingAssembly ().GetCustomAttributes (typeof (AssemblyTitleAttribute), false);

if (attributes.Length > 0).

{.

AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];

if (titleAttribute.Title ≠ «»).

{.

return titleAttribute. Title;

}.

}.

return System.IO.Path.GetFileNameWithoutExtension (Assembly.GetExecutingAssembly ().CodeBase);

}.

}.

public string AssemblyVersion.

{.

get.

{.

return Assembly. GetExecutingAssembly ().GetName ().Version.ToString ();

}.

}.

public string AssemblyDescription.

{.

get.

{.

object[] attributes = Assembly. GetExecutingAssembly ().GetCustomAttributes (typeof (AssemblyDescriptionAttribute), false);

if (attributes.Length == 0).

{.

return «» ;

}.

return ((AssemblyDescriptionAttribute)attributes[0]).Description;

}.

}.

public string AssemblyProduct.

{.

get.

{.

object[] attributes = Assembly. GetExecutingAssembly ().GetCustomAttributes (typeof (AssemblyProductAttribute), false);

if (attributes.Length == 0).

{.

return «» ;

}.

return ((AssemblyProductAttribute)attributes[0]).Product;

}.

}.

public string AssemblyCopyright.

{.

get.

{.

object[] attributes = Assembly. GetExecutingAssembly ().GetCustomAttributes (typeof (AssemblyCopyrightAttribute), false);

if (attributes.Length == 0).

{.

return «» ;

}.

return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;

}.

}.

public string AssemblyCompany.

{.

get.

{.

object[] attributes = Assembly. GetExecutingAssembly ().GetCustomAttributes (typeof (AssemblyCompanyAttribute), false);

if (attributes.Length == 0).

{.

return «» ;

}.

return ((AssemblyCompanyAttribute)attributes[0]).Company;

}.

}.

#endregion.

}.

}.

Program.cs

using System;

using System.Collections.Generic;

using System. Linq;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace MD5.

{.

static class Program.

{.

/// The main entry point for the application.

[STAThread].

static void Main ().

{.

Application.EnableVisualStyles ();

Application.SetCompatibleTextRenderingDefault (false);

Application.Run (new MainForm ());

}.

}.

}.

Показать весь текст
Заполнить форму текущей работой