ios-Data Encryption-MD5 encryption

Source: Internet
Author: User
Tags md5 encryption

Data encryption

iOS development about data encryption algorithm is the most used MD5 and Base64, but the developers favorite is MD5, so today is a brief introduction of MD5 in the use,

Of course about data encryption or to see what the company uses, we use what the company uses, here is only a reference or other needs.

A simple explanation

1. Description

When developing an application, the security of the data is critical, and simply submitting the user's privacy data with a POST request is still not a complete solution to the security issue.

Such as: You can use software (such as Charles) to set up a proxy server to intercept the request data to view the phone

"Blue and white porcelain" software

Therefore: When submitting the user's privacy data, must not be explicitly submitted, to encrypt processing and then submit

2. Common cryptographic algorithms

MD5 \ SHA \ DES \ 3DES \ RC2 and RC4 \ RSA \ idea \ DSA \ AES

3. Selection of cryptographic algorithms

General companies will have a set of their own encryption scheme, according to the requirements of the company interface documents to encrypt

Second, MD5

1. Brief description

MD5: Full name is message Digest algorithm 5, translated as "Message Digest algorithm 5th Edition"

Effect: Generates a unique 128-bit hash value (32 characters) for the input information

Features of 2.MD5

(1) input two different plaintext will not get the same output value

(2) According to the output value, the original plaintext cannot be obtained, that is, its process is irreversible

Application of 3.MD5

Because the MD5 encryption algorithm has good security, and free, so the encryption algorithm is widely used

Mainly used in digital signature, file integrity verification and password encryption and other aspects

4.MD5 hack

MD5 Decryption Website: http://www.cmd5.com

5.MD5 improvements

Now the MD5 is no longer absolutely safe, in this, can be slightly improved MD5 to increase the difficulty of decryption

Add Salt: Insert a random string in the fixed position of the plaintext before MD5

First encryption, after the chaos sequence: first MD5 the plaintext, and then the encryption of the MD5 string of characters to disorderly order

In short, the purpose is: hackers even if the database is compromised, can not decrypt the correct plaintext

code example:

  1-(ibaction) Login {2//1. Username 3 NSString *usernametext = Self.username.text;  4 if (usernametext.length = = 0) {5 [Mbprogresshud showerror:@ "Please enter user name"];  6 return; 7} 8 9//2. Password NSString *pwdtext = Self.pwd.text;     if (Pwdtext.length = = 0) {[Mbprogresshud showerror:@ "Please enter password"]; 14} 15 16 Added mask [Mbprogresshud showmessage:@ "is desperately logged in ...."]; 18 19//3. Send user name and password to server (go HTTP protocol) 20//Create a URL: request path Nsurl *url = [Nsurl urlwithstring:@ "http://192.1 68.15.172:8080/mjserver/login "]; 22 23//Create a request for nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url]; 25 26//5 Seconds after count request Timeout (default 60s timeout) Request.timeoutinterval = 15; Request. HttpMethod = @ "POST"; #warning encrypt the pwdtext by pwdtext = [self md5reorder:pwdtext]; 33 34//Set request body nsstring *param = [NSString stringwithformat:@ "username=%@&pwd=%@ ", Usernametext, Pwdtext]; Notoginseng NSLog (@ "%@", param); NSString//---nsdata-Request. Httpbody = [param datausingencoding:nsutf8stringencoding]; 41 42//Set the request header information for the "setvalue:@" IPhone 6 "forhttpheaderfield:@" user-agent "; 44 45//Send a sync request (send request on main thread)//queue: Store Completionhandler this task nsoperationqueue *queue = [Nsoperatio Nqueue Mainqueue]; [Nsurlconnection sendasynchronousrequest:request queue:queue completionhandler:49 ^ (NSURLResponse *response, NSData *data, Nserror *connectionerror) {50//Hide Mask Wuyi [Mbprogresshud Hidehud]; 52 53// This block will automatically call when the request is complete (Connectionerror | | data = nil) {//General request timed out will come to this [Mbprogresshud Showerr or:@ "request Failed"]; The return; 57} 58 59//Parse the JSON data returned by the server nsdictionary *dict = [Nsjsonserialization jsonobjectwithd Ata:data options:nsjsonreadingmutableleaves Error: nil]; NSString *error = dict[@ "Error"]; if (error) {Mbprogresshud showerror:error]; or else {nsstring *succe SS = dict[@ "Success"]; [Mbprogresshud showsuccess:success]; 67} 68}]; /** * MD5 ($pass. $salt) * * * * * @param text plaintext * * @return Encrypted ciphertext * */-(NSString *) MD     5Salt: (NSString *) text 79 {80//sprinkle salt: Randomly insert arbitrary string into clear text nsstring *salt = [text stringbyappendingstring:@ "AAA"]; 82 return [Salt md5string]; /** * MD5 (MD5 ($pass)) * * * * * * * @param text in clear text * * * * * * @return Encrypted ciphertext * * *-(NSString *) dou BleMD5: (NSString *) text, 94 return [[Text md5string] md5string]; 95} 96 97/** 98 * First encrypted, after the chaos *100 * @para M text PlainText 101 *102 * @return Encrypted ciphertext 103 */104-(NSString *) Md5reorder: (NSString *) text105 {106 NSString *pwd = [Tex T md5string];107 108//encrypted PWD = = 3f853778a951fd2cdf34dfd16504c5d8109 Nsstring *prefix = [pwd substringfromindex:2];110 nsstring *subfix = [pwd substringtoindex:2];111 112//random order Result = = 853778a951fd2cdf34dfd16504c5d83f113 NSString *result = [prefix stringbyappendingstring:subfix];114] NSL OG (@ "\ntext=%@\npwd=%@\nresult=%@", text, pwd, result); 117 return result;118}

III. registration and validation of data processing procedures

1. Security procedures for submitting private data – Registration

2. Security procedures for submitting private data – Login

Finally paste a section of MD5 Source:
 1-(NSString *) md5string 2 {3 const 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} 9-(NSString *) sha1string11 {A. const char *string = self. utf8string;13 int length = (int) strlen (string), unsigned char bytes[cc_sha1_digest_length];15 cc_sha1 (string , length, bytes), return [self stringfrombytes:bytes length:cc_sha1_digest_length];17}18-(NSString *) sha256str ING20 {+ const char *string = self. utf8string;22 int length = (int) strlen (string), unsigned char bytes[cc_sha256_digest_length];24 cc_sha256 (St Ring, length, bytes); return [self stringfrombytes:bytes length:cc_sha256_digest_length];26}27-(NSString *) sha 512string29 {a const char *string = self. utf8string;31 int length = (int) strlen (string); unsigned char Bytes[cc_sha512_digest_length];33 cc_sha512 (String, LENGTH, bytes); return [self stringfrombytes:bytes length:cc_sha512_dige st_length];35}36 Panax Notoginseng-(NSString *) Hmacsha1stringwithkey: (NSString *) key38 {$ NSData *keydata = [key Datausingencodin g:nsutf8stringencoding];40 NSData *messagedata = [self datausingencoding:nsutf8stringencoding];41 NSMutableData *m Utabledata = [Nsmutabledata datawithlength:cc_sha1_digest_length];42 cchmac (kCCHmacAlgSHA1, Keydata.bytes, Keydata.length, Messagedata.bytes, Messagedata.length, mutabledata.mutablebytes); return [self stringfrombytes: ( unsigned char *) mutabledata.bytes length:mutabledata.length];44}45-(NSString *) Hmacsha256stringwithkey: (NSString *) key47 {NSData *keydata = [key datausingencoding:nsutf8stringencoding];49 nsdata *messagedata = [Self datausin gencoding:nsutf8stringencoding];50 nsmutabledata *mutabledata = [Nsmutabledata datawithlength:cc_sha256_digest_ Length];51 Cchmac (kCCHmacAlgSHA256, keydata.bYtes, Keydata.length, Messagedata.bytes, Messagedata.length, mutabledata.mutablebytes); return [self stringFromByte S: (unsigned char *) mutabledata.bytes length:mutabledata.length];53}54-(NSString *) Hmacsha512stringwithkey: (  NSString *) key56 {NSData *keydata = [key datausingencoding:nsutf8stringencoding];58 nsdata *messagedata = [self datausingencoding:nsutf8stringencoding];59 nsmutabledata *mutabledata = [Nsmutabledata dataWithLength:CC_SHA512_ Digest_length];60 Cchmac (kCCHmacAlgSHA512, Keydata.bytes, Keydata.length, Messagedata.bytes, Messagedata.length, mutabledata.mutablebytes); return [self stringfrombytes: (unsigned char *) mutabledata.bytes Length: mutabledata.length];62}63 #pragma mark-helpers65-(NSString *) Stringfrombytes: (unsigned char *) bytes length: (int ) length67 {nsmutablestring *mutablestring = @ "". mutablecopy;69 for (int i = 0; i < length; i++) [M Utablestring appendformat:@ "%02x", bytes[i]];71 return [NSString stringwithstring:mutablestring];72} 

ios-Data Encryption-MD5 encryption

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.