In general, if the app uses Web service, we need to prevent data sniffing to keep the data safe. It is common practice to use SSL to connect to prevent data capture and sniffing
In fact, it is not enough to do so. We also need to prevent man-in-the-middle attacks (do not understand Baidu itself). It is a serious problem for an attacker to connect an app to a spoofed fake server with a fake SSL certificate! So how do you prevent a man-in-the-middle attack?
First, the Web server must provide an SSL certificate, require a. crt file, and then set up the app to connect only the servers that are valid SSL certificates.
Before you start writing your code, turn the. crt file into a. cer file, and then add it to Xcode
Convert. crt files to. cer files
1. Using OpenSSL for conversion
OpenSSL x509-in your certificate. Crt-out your certificate. Cer-outform der
2. By installing CRT files, the computer exports
1) Open Keychain Access first
2) Select the CRT file certificate you installed, select "File"-"Export project"
3) Select the. cer certificate and store it.
Afnetworking HTTPS SSL encryption for data
In fact, it's simple, it takes only two steps.
First step: Add a new class
+ (afsecuritypolicy*) customsecuritypolicy{///import certificate first NSString *cerpath = [[NSBundle Mainbundle] Pathforresource : @ "Hgcang" oftype:@ "CER"];//the path of the certificate nsdata *certdata = [NSData Datawithcontentsoffile:cerpath]; Afsslpinningmodecertificate using certificate validation mode afsecuritypolicy *securitypolicy = [Afsecuritypolicy policywithpinningmode: Afsslpinningmodecertificate]; Allowinvalidcertificates whether to allow invalid certificates (that is, self-built certificates), default to No//if you need to verify the self-built certificate, you need to set to Yes Securitypolicy.allowinvalidcertificate s = YES; Validatesdomainname Whether you need to verify the domain name, the default is yes, or if the domain name of the certificate does not match the domain name you requested, you need to set the entry to no, if it is set to No, that is, the server uses a certificate issued by another trusted authority, it can also establish a connection, which is very dangerous, recommended to open. Set to No, mainly used in this case: the client is requesting a subdomain, and the certificate is a different domain name. Because the domain name on the SSL certificate is independent, if the domain name registered on the certificate is www.google.com, then mail.google.com cannot be verified; Of course, the rich can register the wildcard name *.google.com, but this is still relatively expensive. If set to No, it is recommended that you add the check logic for the corresponding domain name. Securitypolicy.validatesdomainname = NO; Securitypolicy.pinnedcertificates = @[certdata]; return securitypolicy;}
Step two: Add directly in the request method, only one line of code
+ (void) Post: (NSString *) URL params: (nsdictionary *) params success: (void (^) (ID)) Success failure: (void (^) (Nserror *)) failure{ //1. Get request Manager Afhttprequestoperationmanager *mgr = [Afhttprequestoperationmanager manager]; 2. Affirms that the result of the return is text/html type mgr.responseserializer = [Afhttpresponseserializer serializer]; Plus this line of code, HTTPS SSL authentication. //[mgr setsecuritypolicy:[self Customsecuritypolicy]]; 3. Send POST request [Mgr Post:url parameters:params success:^ (afhttprequestoperation *operation, id responseobj) { if (success) { success (responseobj); } } failure:^ (Afhttprequestoperation *operation, Nserror * Error) { if (failure) { failure (error); } }];}
Next, we crawl the data through Charles, and the captured data is encrypted.
Problems that you may encounter
1) The certificate must be pulled into the project, AFN add the verification, to see if the Certdata to obtain the certificate is empty. If blank, there is a problem with the certificate
NSData *certdata = [NSData Datawithcontentsoffile:cerpath];
2. If the HTTPS server does not have data to return, it is most likely because of a problem with the server configuration.
IOS Development Note-afnetworking HTTPS SSL authentication