IOS--MD5 encryption

Source: Internet
Author: User
Tags gz file md5 encryption

the MD5 algorithm has the following characteristics:

1, compressibility: Any length of data, calculated the length of the MD5 value is fixed.

 2, easy to calculate: It is easy to calculate the MD5 value from the original data. 

3, anti-modification: Any changes to the original data, even if only 1 bytes modified, the resulting MD5 value is very different. 

4, strong anti-collision: known raw data and its MD5 value, it is very difficult to find a data with the same MD5 value (that is, falsification of data). MD5 Application Conformance Verification A typical application is to generate information summaries (message-digest) for a piece of information (message) to prevent tampering. For example, there are many softwares under Unix that have a file name with the same filename and file extension. md5 files in this file, usually with a single line of text, roughly structured like: MD5 (tanajiya.tar.gz) = 38b8c2c1093dd0fec383a9d9ac940515 This is the digital signature of the tanajiya.tar.gz file. MD5 the entire file as a large text message, through its irreversible string transform algorithm, produces this unique MD5 message digest. In order to make readers ' friends have an intuitive understanding of the application of MD5, the author gives a brief description of the work process with an analogy and an example: everyone knows that anyone on earth has their own unique fingerprints, which often becomes the most trustworthy way for the judicial authorities to identify criminals; similarly, MD5 can produce an equally unique "digital fingerprint" for any file (regardless of size, format, number), and if anyone makes any changes to the file, its MD5 value, or the corresponding "digital fingerprint", will change. We often see its MD5 value in some software download site, the role of it is that we can download the software, the downloaded file with specialized software (such as Windows MD5 Check, etc.) to do a MD5 check, To ensure that the files we obtain are the same files as the files provided by the site.The scheme of file checking using MD5 algorithm is widely used in software download station, forum database, System file security and so on. A typical application of digital signature MD5 is to generate fingerprint (fingerprints) on a message (byte string) to prevent "tampering". For example, you will write a paragraph in a message called Readme.txt file, and on this Readme.txt generate a MD5 value and record, and then you can spread this file to others, if others modify any of the contents of the file, you will see this file recalculation MD5 (two MD5 values are not the same). If there is a third-party certification body, with MD5 can also prevent the file author's "Repudiation", which is called Digital signature application. Secure access authentication MD5 is also widely used in operating system login authentication, such as UNIX, all kinds of BSD system login password, digital signature and many other aspects. For example, in Unix system, the user's password is stored in the file system by MD5 (or other similar algorithm) after hash operation. When the user logs in, the system MD5 the user's password into a hash, and then compares the MD5 value stored in the file system to determine if the password was entered correctly. Through such steps, the system can determine the legality of the user login system without knowing the user's password. This avoids the user's password being known to users who have system administrator privileges. MD5 map any length of "byte string" to a large integer of 128bit, and it is difficult to reverse the original string through the 128bit, in other words, even if you see the source program and algorithm description, you can not transform a MD5 value back to the original string, mathematically speaking, This is because the original string has an infinite number, which is somewhat like a mathematical function that does not have an inverse function. Therefore, to encounter the problem of MD5 password, the better way is: You can use the MD5 () function in this system to reset a password, such as admin, the generated hash of a string of passwords to overwrite the original hash value on the line. It is for this reason that the most commonly used hacker to decipher a password is a method called "Running a dictionary". There are two ways to get a dictionary, one is the daily collection of strings used for the password table, and the other is generated by the arrangement of the combination of methods, the first use MD5 program to calculate the MD5 value of these dictionary items, and then use the target MD5 value in the dictionary to retrieve. We assume that the maximum length of the password is 8 bytes (8 Bytes), and that the password can only be letters and numbers, a total of 26+26+10=62 bytes, and the number of items in the sorted dictionary is P (62,1) +p (62,2) .... +p (62,8), which is already a very astronomical figure, Storing this dictionary requires a terabytes of disk array, and there is a prerequisite to getting the target accountThe password MD5 value is only possible. This encryption technique is widely used in UNIX systems, which is one of the important reasons why UNIX systems are more robust than general operating systems.  OC Code implementation:

// string conversion 32-bit MD5 string lowercase
-(NSString *) stringWith32BitMD5Lower {
     return [[self stringWith32BitMD5] lowercaseString];

// string conversion 32-bit MD5 string uppercase
-(NSString *) stringWith32BitMD5Upper {
      return [[self stringWith32BitMD5] uppercaseString];

// string convert 32-bit MD5 string
-(NSString *) stringWith32BitMD5 {
     // 1. convert string to c language string
     const char * cString = [self UTF8String];
     CC_LONG length = (CC_LONG) strlen (cString);
     unsigned char bytes [CC_MD5_DIGEST_LENGTH];
     // 2. Convert the string of c to MD5
     CC_MD5 (cString, length, bytes);
     // 3. Convert the string of c to oc
     NSMutableString * finalString = [NSMutableString stringWithCapacity: CC_MD5_DIGEST_LENGTH * 2];
     for (int i = 0; i <16; i ++) {
         // two digits, insufficient digits in front, fill with 0
         [finalString appendFormat: @ "% 02x", bytes [i]];
     return finalString;
-(NSString *) stringWith16BitMD5 {
     NSRange range = {8, 16};
     return [[self stringWith32BitMD5] substringWithRange: range];

// string conversion 32-bit MD5 string lowercase
-(NSString *) stringWith16BitMD5Lower {
     return [[self stringWith16BitMD5] lowercaseString];

// string conversion 32-bit MD5 string uppercase
-(NSString *) stringWith16BitMD5Upper {
     return [[self stringWith16BitMD5] uppercaseString];

IOS--MD5 encryption

Related Article

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: 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.