iOS often uses encryption (MD5,AES,BASE64) and network data security

Source: Internet
Author: User
Tags decrypt md5 encryption

Demo Sample Project Https://github.com/cerastes/Encryption


1md5

Creating the MD5 Class

#import <Foundation/Foundation.h> @interface cjmd5:nsobject+ (NSString *) Md5hexdigest: (NSString *) input; @end

#import "CJMD5.h" #import <CommonCrypto/CommonDigest.h> @implementation cjmd5+ (NSString *) Md5hexdigest: ( NSString *) input{        const char* str = [input utf8string];    unsigned char result[cc_md5_digest_length];    CC_MD5 (str, strlen (str), result);    nsmutablestring *ret = [nsmutablestring stringwithcapacity:cc_md5_digest_length];        for (int i = 0; i<cc_md5_digest_length; i++) {        [ret appendformat:@ "%02x", result];    }    return ret;} @end

MD5 is irreversible only there is no decryption using the method such as the following

NSString *username = @ "Cerastes"; NSString *password = @ "Hello Word";//   MD5 encryption NSString *md5 = [CJMD5 Md5hexdigest:password]; NSLog (@ "%@", MD5);

2AES encryption

AES encrypted open source code git

Https://github.com/Gurpartap/AESCrypt-ObjC

Usage

  AES Encryption    nsstring *encrypteddata = [Aescrypt encrypt:username password:password];//encryption    NSString *message = [Aescrypt Decrypt:encrypteddata Password:password]; Decrypt    NSLog (@ "Encrypted result =%@", EncryptedData);    NSLog (@ "Decryption result =%@", message);

3 BASE64 Encryption

https://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/?r=87

Join for example the following methods

. h

+ (nsstring*) encodebase64string: (NSString *) input;+ (nsstring*) decodebase64string: (NSString *) input;+ (NSString*) Encodebase64data: (NSData *) data;+ (nsstring*) Decodebase64data: (NSData *) data;

. m

+ (nsstring*) encodebase64string: (NSString *) input {nsdata *data = [input datausingencoding:nsutf8stringencoding allow    Lossyconversion:yes];    data = [GTMBase64 encodedata:data];    NSString *base64string = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; return base64string;} + (nsstring*) decodebase64string: (NSString *) input {nsdata *data = [input datausingencoding:nsutf8stringencoding allow    Lossyconversion:yes];    data = [GTMBase64 decodedata:data];    NSString *base64string = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; return base64string;}    + (nsstring*) Encodebase64data: (NSData *) data {data = [GTMBase64 encodedata:data];    NSString *base64string = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; return base64string;}    + (nsstring*) Decodebase64data: (NSData *) data {data = [GTMBase64 decodedata:data];    NSString *base64string = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; RetUrn base64string;} 

Usage

    BASE64 encryption    nsstring *baseencodestring = [GTMBase64 Encodebase64string:password];    NSString *basedecodestring = [GTMBase64 decodebase64string:baseencodestring];    NSLog (@ "baseencodestring =%@", baseencodestring);    NSLog (@ "basedecodestring =%@", basedecodestring);


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.