How to encrypt strings in IOS/implement MD5 encryption in IOS/NSString classification in IOS, iosnsstring
After reading it, you will learn:
1. Learn the classification implementation in IOS development,
2. Writing of class methods,
3. MD5 encryption/decryption of strings.
----------------------------- Wolfhous ------------------
[1] creating a project and creating a category
[2] classification naming and class selection
[3] writing NSString classification class methods, implementation class methods, and how MD5 Encryption
[4] Test encryption results
[5] decryption: Baidu MD5 decryption directly. Click the song website and enter the decryption KEY. In my Demo,
Wolfhous normal encryption 44bf025d27eea66336e5c1133c3827f7
Wolfhous niuqiang encrypted 44fb461963aae22772a18557787c63b3
------------------------- Wolfhous -----------------------
It can be seen that normal websites cannot be decrypted. We recommend that you use this encryption when entering a password during registration during development. of course, there are more than N encrypted methods. I will list only one simple method. The source code is shown below.
# Import "NSString + md5String. h"
// Introduce necessary header files
# Import <CommonCrypto/CommonDigest. h>
@ Implementation NSString (md5String)
/** Md5 encryption */
+ (NSString *) md5String :( NSString *) str
{
Const char * myPasswd = [str UTF8String];
Unsigned char mdc [16];
CC_MD5 (myPasswd, (CC_LONG) strlen (myPasswd), mdc );
NSMutableString * md5String = [NSMutableString string];
For (int I = 0; I <16; I ++ ){
[Md5String appendFormat: @ "% 02x", mdc [I];
}
Return md5String;
}
/** Md5 NB (meaning) encryption */
+ (NSString *) md5StringNB :( NSString *) str
{
Const char * myPasswd = [str UTF8String];
Unsigned char mdc [16];
CC_MD5 (myPasswd, (CC_LONG) strlen (myPasswd), mdc );
NSMutableString * md5String = [NSMutableString string];
[Md5String appendFormat: @ "% 02x", mdc [0];
For (int I = 1; I <16; I ++ ){
[Md5String appendFormat: @ "% 02x", mdc [I] ^ mdc [0];
}
Return md5String;
}
@ End