iOS Development Web chapter-Data security

Source: Internet
Author: User
Tags vars

In the network application, the time to ensure that the user data security, so to encrypt. The MD5 algorithm is used in many countries. features of the MD5 algorithm: * The same data encryption results are the same. (32 characters) * irreversible. (Cannot reverse decrypt) * Can be used for file check/fingerprint identification. The MD5 algorithm is public, and the MD5 algorithm is already packaged in iOS. It can be written as a string classification: [OBJC]View Plaincopy
  1. -(NSString *) MD5String
  2. {
  3. Const Char char*string = self.  utf8string;
  4. int length = (int) strlen (string);
  5. unsigned char bytes[cc_md5_digest_length];
  6. CC_MD5 (string, length, bytes);
  7. return [self stringfrombytes:bytes length:cc_md5_digest_length];
  8. }


It is important to encrypt the user's login data in an iOS program. To do so, even if the data is hijacked, the original data can not be restored to the point. one, ordinary MD5 encryptionToo simple MD5 encryption can easily be cracked. Typically used when MD5 encryption is performed "Add Seasoning"The method. Simple MD5 can be cracked on this website: www.cmd5.com The following is the method for MD5 encryption: tokenA string that is added, which can be a grotesque string of any length. [OBJC]View Plaincopy
  1. -(ibaction) Login: (UIButton *) Sender {
  2. [self postlogin];
  3. }
  4. /** when submitting user data, it is relatively safe to post. Convert user data into a model at the same time * *
  5. -(void) Postlogin {
  6. //1.url
  7. nsstring *urlstr = [NSString stringwithformat:@ "http://localhost/login.php"];
  8. Nsurl *url = [Nsurl urlwithstring:urlstr];
  9. //2. Establishing Mutablerequest
  10. nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url];
  11. //3. Setting
  12. Request.  HttpMethod = @ "POST";
  13. //Request body can be found in Firebug
  14. NSString *pwd = self. userpwd. Text;
  15. //Add salt First, encrypt with MD5.  (The server simply stores salt and encryption to save the line).  In reality there is a public/private key, and the server is not simply storing the password.
  16. PWD = [pwd stringbyappendingstring:token];
  17. PWD = [pwd md5string];
  18. NSLog (@ "%@", PWD);
  19. nsstring *body = [NSString stringwithformat:@ "username=%@&password=%@", self. Username.  Text, pwd];
  20. Request.  Httpbody = [Body datausingencoding:nsutf8StringEncoding];
  21. //4. Establish a connection. (data is the fetch, same as Get)
  22. [Nsurlconnection sendasynchronousrequest:request queue:[[nsoperationqueue alloc] init] Completionhandler: ^ (nsurlresponse *response, nsdata *data, nserror *connectionerror) {
  23. nsstring *str = [[NSString alloc] initwithdata:data encoding:nsutf8StringEncoding];
  24. NSLog (@ "%@,%@", [Nsthread CurrentThread], str);
  25. //update display needs to be in the main thread
  26. [[Nsoperationqueue Mainqueue] addoperationwithblock: ^{
  27. self. Label. Text = str;
  28. NSLog (@ "%@,%@", [Nsthread CurrentThread], str);
  29. }];
  30. }];
  31. }

Ii. A more advanced approachUse the concept of public and private keys. A public key (known to all), a private key (known only to the server). The password should be changed dynamically. * User: Encrypt with token+ time, send to Server * Server: Remove user password (with private key when storing), use time + public key to compare with the password sent by the client. (The server also to check the time difference between sending passwords, less than 1 minutes) detailed notes: from Lao Liu. [OBJC]View Plaincopy
  1. -(ibaction) Login: (ID) Sender
  2. {
  3. NSString *pwd = self. Pwdtext. Text;
  4. //For MD5 encryption
  5. PWD = [pwd stringbyappendingstring:token];
  6. //Every time is the same! Example: A hacker intercepts data from a router
  7. //You will be able to obtain the password after encryption!
  8. PWD = [pwd md5string];
  9. //In the server background, a MD5 cipher string that is stored with a private key and is treated with salt
  10. PWD = [NSString stringwithformat:@ "%@%@%@", pwd, PublicKey, @ "2014062914:14:30"];
  11. //Use date to ensure that the strings generated by the encryption are not the same
  12. PWD = [pwd md5string];
  13. //content submitted to the server: new password, event that generated the password ,
  14. /** 
  15. Processing of the server:
  16. 1. Remove the user's password from the server (encrypted with the private key)
  17. 2. The server knows the shared key and compares it to the client-submitted password based on a given time (dynamically generating a new password)
  18. 3. The server also needs to check the event difference of the submitted password, which is within 1 minutes of the date submitted by the client.
  19. */
  20. NSLog (@ "%@", PWD);
  21. [self postlogonwithusername:selfusernametext. Text password:pwd];
  22. }
  23. #pragma mark-post Login
  24. -(void) Postlogonwithusername: (nsstring *) userName Password: (nsstring *) password
  25. {
  26. //1. URL
  27. nsstring *urlstr = @ "http://192.168.25.2/login.php";
  28. Nsurl *url = [Nsurl urlwithstring:urlstr];
  29. //2. Request,post method, need to establish a mutable request
  30. nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url];
  31. //1> post method, all data transfer involving user's privacy, all need to submit by post!
  32. Request.  HttpMethod = @ "POST";
  33. //2> Data Body
  34. nsstring *bodystr = [NSString stringwithformat:@ "username=%@&password=%@", username, password];
  35. //Convert string to binary data
  36. Request.  Httpbody = [Bodystr datausingencoding:nsutf8StringEncoding];
  37. //3. Send "asynchronous" requests, work on other threads, do not block current thread execution
  38. [Nsurlconnection sendasynchronousrequest:request queue:[[nsoperationqueue alloc] init] completionhandler:^ (nsurlresponse *response, nsdata *data, nserror *connectionerror) {
  39. //1> JSON, format is and nsdictionary Fast packaging format very
  40. //Convert JSON into a dictionary serialization
  41. nsdictionary *dict = [nsjsonserialization jsonobjectwithdata:data options:1 error:NULL];
  42. czuserinfo *userinfo = [Czuserinfo userinfowithdict:dict];
  43. NSLog (@ "%@%@", UserInfo. UserId, UserInfo. userName);
  44. }];
  45. NSLog (@ "=======");
  46. }


Reprint Please specify source: http://blog.csdn.net/xn4545945

iOS Development Web chapter-Data security

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.