IOS MD5 encryption and iosmd5 Encryption
1. MD5 Encryption
Message Digest Algorithm MD5 (the fifth version of the Message Digest Algorithm in Chinese) is a hash function widely used in the computer security field to provide Message integrity protection. The algorithm file is RFC 1321 (R. Rivest, MIT Laboratory for Computer Science and RSA Data Security Inc. Limit l 1992)
2. MD5 Encryption
Consistency Verification
A typical application of MD5 is to generate a Message Digest (Message-Digest) for a piece of information to prevent tampering. For example, many software in Unix have a file with the same file name and the file extension. md5 when downloading. This file usually contains only one line of text, with the approximate structure as follows:
MD5 (tanajiya.tar.gz) = 0ca175b9c0fda-a831d895e269332461
The digital signature of the tanajiya.tar.gz file. MD5 treats the entire file as a large text, and generates this unique MD5 information digest through its irreversible String Conversion Algorithm. In order to give readers an intuitive understanding of the MD5 application, I will briefly describe the process with an example: everyone knows, anyone on earth has his own unique fingerprint, which is often the most trustworthy way for judicial authorities to identify criminals. Similarly, MD5 can generate a unique "digital fingerprint" for any file regardless of its size, format, and quantity. If anyone makes any changes to the file, the MD5 value, that is, the corresponding "digital fingerprint", will change. We
The MD5 value is often seen in a software information of some software download sites. Its function is that after we download the software, use special software (such as Windows MD5 Check) for MD5 verification on the downloaded files to ensure that the files we obtain are the same as the files provided by the site. The MD5 algorithm is widely used in software download sites, Forum databases, and system file security.A typical application of digital certificate MD5 is to generate fingerprint (fingerprint) for a piece of Message (byte string) to prevent "tampering ". For example, you write a paragraph in a readme.txt file and generate an MD5 value for this readme.txt file and record it. Then you can spread the file to others. If someone else modifies any content in the file, when you re-calculate the MD5 value of this file, you will find that (two MD5 values are different ). If there is another third-party certification authority, MD5 can also prevent the "credit" of the file author. This is the so-called digital signature application. Secure access authentication MD5 is also widely used for login authentication of operating systems, such as Unix, various BSD system logon passwords, digital signatures, and many other aspects. For example, in Unix systems, users' passwords are stored in file systems after MD5 (or other similar algorithms) Hash operations. When a user logs on, the system performs the MD5 Hash operation on the password entered by the user, and then compares it with the MD5 value saved in the file system to determine whether the entered password is correct. In this step, the system can determine the validity of the user's logon system without knowing the user's password. This prevents the user's password from being known by users with system administrator permissions. It is difficult for MD5 to map a "Byte string" of any length to a large integer of BITs and use this bits to reverse the original string. In other words, even if you see the source program and algorithm description, it is also impossible to convert an MD5 value back to the original string. In terms of mathematical principle, it is because there are infinite numbers of original strings, which is a bit like a mathematical function without an inverse function. Therefore, if you encounter an md5 password problem, you can use the md5 () function in the system to reset a password, such as admin, overwrite the Hash value of the generated string of passwords with the original Hash value. For this reason, one of the most frequently used methods for deciphering passwords by hackers is known as "Running dictionaries. There are two ways to get the dictionary: one is the string table that is collected daily and the other is generated by means of arrangement and combination, use the MD5 program to calculate the MD5 value of these dictionary items, and then use the target MD5 value for retrieval in this dictionary. Assume that the maximum length of the password is 8 Bytes (8 Bytes), and the password can only contain letters and numbers, 26 + 26 + 10 = 62 characters in total, the number of items in the dictionary is P () + P ).... + P () is already a very astronomical number. to store this dictionary, you need a TB-level disk array, and this method has a premise, it is only possible to obtain the MD5 value of the password of the target account. This encryption technology is widely used in Unix systems, which is also an important reason why Unix systems are more robust than general operating systems. 3. Implement MD5 (1) MD5 encrypted string in iOS
Perform MD5 encryption on the string and return the encrypted string.
#import <CommonCrypto/CommonDigest.h> // Need to import for CC_MD5 access
-(NSString *) md5 :( NSString *) str {const char * cStr = [str UTF8String]; unsigned char result [16]; CC_MD5 (cStr, strlen (cStr ), result); // This is the md5 call
// Output 1 // return [NSString stringWithFormat: // @ "% 02x % 02x % 02x % 02x % 02x % 02x % 02x % 02x % 02x % 02x % 02x % 02x % 02x % 02x % 02x % 02x ", // result [0], result [1], result [2], result [3], // result [4], result [5], result [6], result [7], // result [8], result [9], result [10], result [11], // result [12], result [13], result [14], result [15] //];
// Output 2 Simplified Version
NSMutableString * ret = [NSMutableString stringWithCapacity: CC_MD5_DIGEST_LENGTH];
For (int I = 0; I <CC_MD5_DIGEST_LENGTH; I ++ ){
[Ret appendFormat: @ "% 02X", result];
}
Return ret;}