iOS code encryption common encryption method

Source: Internet
Author: User
Tags decrypt
<span id="Label3"></p><p><p>In Today's interview, was asked what iOS is the use of encryption and decryption operations, My answer is this: aes,md5,base 64, and then the encryption algorithm for a brief introduction and overview of several algorithms and the differences between the pros and cons. however, the answer received is: these are not iOS encryption! I suddenly have no language, this is not the way to use iOS encryption? Then asked the Interviewer: MD5 is a summary .... What does encryption mean? Encryption is the Client's data encryption and server-side use of secret key to decrypt data for data security Considerations. To say that encryption should be RSA. fortunately, prior to know rsa, just understand is not very thorough and clear ...</p></p><p><p>Back or the encryption and decryption of iOS hold a skeptical attitude, or to find information on their own online learning to learn it!</p></p><p><p><span style="font-size: 15px;"><strong>iOS code encryption commonly used encryption method, common iOS code encryption commonly used encryption algorithm includes MD5 encryption, AES encryption, BASE64 encryption:</strong></span></p></p> <ol class="exp-conent-orderlist"> <li class="exp-content-list list-item-1"><li class="exp-content-list list-item-1"><p>MD5 iOS Code encryption</p><p>Create the MD5 class with the following code</p><p>#import <Foundation/Foundation.h></p><p></p><p>@interface Cjmd5:nsobject</p><p>+ (nsstring *) md5hexdigest: (nsstring *) input;</p><p>@end</p></li></li> <li class="exp-content-list list-item-2"><li class="exp-content-list list-item-2">  <p> #import "CJMD5.h" </p> <p> #import <commoncrypto/ Commondigest.h> </p> <p>   </p> <p> @implementation CJMD5 </p> <p> + (nsstring *) md5hexdigest: (nsstring *) input{ </p> <p>      </p> <p>     const char* str = [input utf8string]; </p> <p>     unsigned char result[cc_md5_digest_length]; </p> <p>     CC_MD5 (str, strlen (str), result); </p> <p>     nsmutablestring *ret = [nsmutablestring stringwithcapacity:cc_md5_digest_length]; </p> <p>      </p> <p>     for (int i = 0; i<cc_md5_digest_length; i++) {</p> <p>         [ret appendformat:@]%0 2 x ", result]; </p> <p>    } </p> <p>     return ret; </p> <p>} </p> <p> @end </p> </li></li> <li class="exp-content-list list-item-3"><li class="exp-content-list list-item-3"><p>MD5 is irreversible only if encryption is not decrypted, iOS code is encrypted using the following methods</p><p>NSString *username = @ "cerastes";</p><p>NSString *password = @ "hello Word";</p><p>MD5 encryption</p><p>NSString *md5 = [CJMD5 md5hexdigest:password];</p><p>NSLog (@ "%@", md5);</p><span class="last-item-end">END</span></li></li> </ol>AES encrypted iOS code encryption <ol class="exp-conent-orderlist"> <ol class="exp-conent-orderlist"> <li class="exp-content-list list-item-1"><p>How to encrypt iOS code using AES encryption</p><p>AES Encryption</p><p>NSString *encrypteddata = [aescrypt encrypt:username password:password];//encryption</p><p>NSString *message = [aescrypt decrypt:encrypteddata password:password]; Decrypt</p><p>NSLog (@ "encrypted result =%@", encrypteddata);</p><p>NSLog (@ "decryption result =%@", message);</p><span class="last-item-end">END</span></li> </ol> </ol>BASE64 encrypted iOS code encryption <ol class="exp-conent-orderlist"> <li class="exp-content-list list-item-1"><li class="exp-content-list list-item-1"><p>BASE64 encrypt iOS code encryption add the following methods</p><p>. h</p><p>+ (nsstring*) encodebase64string: (nsstring *) input;</p><p>+ (nsstring*) decodebase64string: (nsstring *) input;</p><p>+ (nsstring*) Encodebase64data: (nsdata *) data;</p><p>+ (nsstring*) Decodebase64data: (nsdata *) data;</p></li></li> <li class="exp-content-list list-item-2"><p><p>. m</p></p><p><p>+ (nsstring*) encodebase64string: (nsstring *) Input {</p></p><p><p>NSData *data = [input datausingencoding:nsutf8stringencoding allowlossyconversion:yes];</p></p><p><p>data = [GTMBase64 encodedata:data];</p></p><p><p>NSString *base64string = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding];</p></p><p><p>Return base64string;</p></p><p><p>}</p></p><p><p></p></p><p><p>+ (nsstring*) decodebase64string: (nsstring *) Input {</p></p><p><p>NSData *data = [input datausingencoding:nsutf8stringencoding allowlossyconversion:yes];</p></p><p><p>data = [GTMBase64 decodedata:data];</p></p><p><p>NSString *base64string = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding];</p></p><p><p>Return base64string;</p></p><p><p>}</p></p><p><p></p></p><p><p>+ (nsstring*) Encodebase64data: (nsdata *) Data {</p></p><p><p>data = [GTMBase64 encodedata:data];</p></p><p><p>NSString *base64string = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding];</p></p><p><p>Return base64string;</p></p><p><p>}</p></p><p><p></p></p><p><p>+ (nsstring*) Decodebase64data: (nsdata *) Data {</p></p><p><p>data = [GTMBase64 decodedata:data];</p></p><p><p>NSString *base64string = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding];</p></p><p><p>Return base64string;</p></p><p><p>}</p></p></li> <li class="exp-content-list list-item-3"><p><p>BASE64 How to encrypt iOS code using encryption</p></p><p><p>BASE64 encryption</p></p><p><p>NSString *baseencodestring = [GTMBase64 encodebase64string:password];</p></p><p><p>NSString *basedecodestring = [GTMBase64 decodebase64string:baseencodestring];</p></p><p><p>NSLog (@ "baseencodestring =%@", baseencodestring);</p></p><p><p>NSLog (@ "basedecodestring =%@", basedecodestring);</p></p><p><p></p></p><p><p><span style="font-size: 18px; color: #ff0000;"><strong>4.RSA encryption:</strong></span></p></p><p><p>RSA is currently the most influential Public-key encryption algorithm, it can resist the most known password attacks so far, has been recommended by the ISO public key data encryption Standard.</p></p><p><p>RSA Public Key Cryptosystem. The so-called public key cryptosystem is to use different encryption keys and decryption keys, is a "cryptographic key derived from the known encryption key is not feasible in computing" Cryptosystem.</p></p>Usually the husband is a pair of RSA keys, one of which is the secret key, which is saved by the user, and the other is the public key, which can be exposed externally and even registered on the network server. To improve confidentiality, RSA keys are at least 500 bits long and 1024 bits are generally recommended. This makes the computation of encryption very large. In order to reduce the amount of computation, the traditional encryption method is used in conjunction with the public key encryption method when transmitting information, that is, the information is encrypted with an improved des or idea conversation key, and then the RSA key is used to encrypt the conversation key and the information Digest. When the other party receives the message, it decrypts it with a different key and can check the information Digest. The RSA algorithm is the first algorithm that can be used for both encryption and digital signature, and it is easy to understand and Manipulate. RSA is the most widely researched Public-key algorithm.  RSA algorithm is an asymmetric cryptographic algorithm, so-called asymmetric, means that the algorithm needs a pair of keys, using one of the encryption, you need to use another to Decrypt. Rsa's algorithm involves three parameters, n, e1, e2. where n is the product of two large prime numbers p and q, and the number of bits occupied by the binary representation of n is the so-called key Length. E1 and E2 are a pair of related values, E1 can be arbitrarily taken, but require E1 with (p-1) * (q-1) coprime, then e2, request (e2*e1) MoD ((p-1) * (q-1)) = 1. (n,e1), (n,e2) is the key pair. Where (n,e1) is the public key, (n,e2) is the private key. [1] RSA Plus decryption algorithm is identical, set A is plaintext, B is ciphertext, then: a=b^e2 mod n;b=a^e1 mod n; (public key cryptography, public key encryption, private key Decryption) E1 and E2 can be used interchangeably, namely: a=b^e1 mod n;b=a^e2 mod n;<p><p>1) Local Data encryption</p></p><p><p>Encrypt nsuserdefaults,sqlite storage file data, protect account number and key Information. <span style="line-height: 1.5;">) url-encoded Encryption</span></p></p><p><p>Encrypt URLs that appear in the program to prevent URLs from being statically parsed</p></p><p><p>3) Network Transmission Data encryption</p></p><p><p>Provide encryption scheme for client transmitting data, effectively prevent interception by network interface</p></p><p><p>4) method body, Method name Advanced Confusion</p></p><p><p>Confusing the method name and method body of the application to ensure that the code cannot be parsed after the source is reversed</p></p><p><p>5) Program Structure Mixed-line encryption</p></p><p><p>Clutter the application logical structure to minimize source readability</p></p></li> </ol><p><p>iOS code encryption common encryption method</p></p></span>

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.