How to encrypt strings in IOS/implement MD5 encryption in IOS/NSString classification in IOS, iosnsstring

Source: Internet
Author: User

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

 

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