MD5 message digest algorithm

Source: Internet
Author: User

I learned about MD5 encryption some time ago.Algorithm, You can write it yourselfProgramI have developed a tool for verifying the md5encryption by downloading files on the Internet. Here I will contribute the source code, and I will introduce some of this tool.CodeSo I sorted it out and used it as a document for my future study.

1. MD5 (Message Digest) Message Digest algorithm

(1) the MD5 algorithm is a hash algorithm (Digest algorithm, fingerprint algorithm), not an encryption algorithm, the hash value can be calculated using MD5 for any content of any length.

(2) MD5 values can be calculated regardless of the string or file. The calculated MD5 values are the same no matter the length of the string or whether the file is large or small.

(3) irreversible. It is impossible to calculate the original string based on the existing MD5 value.

(4) non-repeatability (in theory, it can be repeated, but the probability is very small)

(5) the MD5 algorithm is irreversible in theory. Therefore, the only attack method is collision. Two different contents generate the same MD5 value, which is collision.

2. Calculate string Encryption

(1) encrypt strings

Static string md5encryptformstring (string MSG) {// creates an MD5 value calculation object MD5 MD5 = md5.create (); // calculate the byte array of the MSG string first // calculate the byte [] According to the UTF-8 of the string msg // for strings containing Chinese characters, if different encoding is used for calculating the string, for example, UTF-8 or gb2312, so the calculated byte [] is different, and then the MD5 value calculated by computehash (byte []) is also different, therefore, we recommend that you use the unified encoding UTF-8 byte [] BS = encoding when calculating the mfd5 value. utf8.getbytes (MSG); // restores the original string // string STR = encoding according to the byte [] array. utf8.getstring (BS); // start to calculate the MD5 value byte [] md5bytes = md5.computehash (BS); // release the resource md5.clear (); // obtain the string, convert MD5 to the string stringbuilder sb = new stringbuilder (); For (INT I = 0; I <md5bytes. length; I ++) {sb. append (md5bytes [I]. tostring ("X2");} return sb. tostring ();} console. writeline ("enter a string"); string MSG = console. readline (); string md5str = md5encryptformstring (MSG); console. writeline (md5str );

(2) effect display

3. encryption of computing files

(1) file encryption

Private Static string md5encryptformfile (string path)

{

// Create an MD5 object

MD5 MD5 = md5.create ();

Using (filestream FS = file. openread (PATH ))

{

Byte [] BS = md5.computehash (FS );

Md5.clear ();

Stringbuilder sb = new stringbuilder ();

For (INT I = 0; I <BS. length; I ++)

{

SB. append (BS [I]. tostring ("X2 "));

}

Return sb. tostring ();

}

}

Console. Write ("Enter the file path :");

String Path = console. Readline ();

String MD5 = md5encryptformfile (PATH );

Console. writeline (MD5 );

(2) effect display

4. MD5 encryption Calculator

(1) The two algorithms used in the basic design of the software are the above two algorithms, and the code supporting drag and drop is as follows:

String [] filename;

Private void txtmessage_dragdrop (Object sender, drageventargs E)

{

If (rbfilemd5.checked = true)

{

Filename = (string []) E. Data. getdata (dataformats. filedrop );

String [] STR = filename [0]. tostring (). Split (New char [] {'\'}, stringsplitoptions. removeemptyentries );

Txtmessage. Text = "file path:" + filename [0] + "\ r \ n" + "file name:" + STR [Str. Length-1];

}

}

Private void txtmessage_dragenter (Object sender, drageventargs E)

{

If (E. Data. getdatapresent (dataformats. filedrop, true) = true)

{

E. effect = dragdropeffects. All;

}

}

(2) I will show the following results:

1) File Usage

2) string usage

(3) Source: http://download.csdn.net/detail/hanyinglong/4666224

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.