Common encryption algorithms for iOS

Source: Internet
Author: User

In iOS development, the content is often encrypted for data security, and here we summarize the commonly used cryptographic algorithms:

1, MD5

<span style= "FONT-SIZE:18PX;" >+ (NSString *) Md5hash: (NSString *) str {    const char *cstr = [str utf8string];    unsigned char result[16];    CC_MD5 (CStr, strlen (CSTR), result);    NSString *md5result = [NSString stringWithFormat:                           @ "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%0 2x%02x ",                           result[0], result[1], result[2], result[3],                           result[4], result[5], result[6], result[7],                           result[ 8], result[9], result[10], result[11],                           result[12], result[13], result[14], result[15]                           ];    return md5result;} </span>


2. SHA1 digital Signature Algorithm

<span style= "FONT-SIZE:18PX;" >const char *cstr = [str cstringusingencoding:nsutf8stringencoding];    NSData *data = [NSData datawithbytes:cstr length:str.length];        uint8_t Digest[cc_sha1_digest_length];        CC_SHA1 (Data.bytes, Data.length, Digest);        nsmutablestring* output = [nsmutablestring stringwithcapacity:cc_sha1_digest_length * 2];        for (int i = 0; i < cc_sha1_digest_length; i++) {        [Output appendformat:@ "%02x", Digest[i]];    }        Return output;</span>

3.AES Encryption Algorithm

<span style= "FONT-SIZE:18PX;"    >-(NSData *) Aes256encryptwithkey: (NSString *) key {//encryption char keyptr[kcckeysizeaes256+1];    Bzero (keyptr, sizeof (KEYPTR));    [Key Getcstring:keyptr maxlength:sizeof (KEYPTR) encoding:nsutf8stringencoding];    Nsuinteger datalength = [self length];    size_t buffersize = datalength + kCCBlockSizeAES128;    void *buffer = malloc (buffersize);    size_t numbytesencrypted = 0; Cccryptorstatus cryptstatus = Cccrypt (Kccencrypt, kCCAlgorithmAES128, KCCOPTIONPK cs7padding |                                          Kccoptionecbmode, Keyptr, kCCBlockSizeAES128, NULL, [self bytes], datalength, BU    Ffer, buffersize, &numbytesencrypted);    if (Cryptstatus = = kccsuccess) {return [NSData datawithbytesnocopy:buffer length:numbytesencrypted]; } FREE (buffer); return nil;}    -(NSData *) Aes256decryptwithkey: (NSString *) key {//decrypt char keyptr[kcckeysizeaes256+1];    Bzero (keyptr, sizeof (KEYPTR));    [Key Getcstring:keyptr maxlength:sizeof (KEYPTR) encoding:nsutf8stringencoding];    Nsuinteger datalength = [self length];    size_t buffersize = datalength + kCCBlockSizeAES128;    void *buffer = malloc (buffersize);    size_t numbytesdecrypted = 0; Cccryptorstatus cryptstatus = Cccrypt (Kccdecrypt, kCCAlgorithmAES128, KCCOPTIONPK cs7padding |                                          Kccoptionecbmode, Keyptr, kCCBlockSizeAES128, NULL, [self bytes], datalength, BU    Ffer, buffersize, &numbytesdecrypted);    if (Cryptstatus = = kccsuccess) {return [NSData datawithbytesnocopy:buffer length:numbytesdecrypted]; } Free (BuffER); return nil;} </span>

4,BASE64 code


<span style= "FONT-SIZE:18PX;" >+ (nsstring*) encodebase64string: (NSString *) input {nsdata *data = [input datausingencoding:nsutf8stringencoding     Allowlossyconversion:yes];     data = [GTMBase64 encodedata:data]; NSString *base64string = [[[NSString Alloc] initwithdata:data encoding:nsutf8stringencoding] autorelease]; return base64string;} + (nsstring*) decodebase64string: (NSString *) input {nsdata *data = [input datausingencoding:nsutf8stringencoding allo     Wlossyconversion:yes];     data = [GTMBase64 decodedata:data]; NSString *base64string = [[[NSString Alloc] initwithdata:data encoding:nsutf8stringencoding] autorelease]; return base64string;}     + (nsstring*) Encodebase64data: (NSData *) data {data = [GTMBase64 encodedata:data]; NSString *base64string = [[[NSString Alloc] Initwithdata:data encoding:nsutf8stringencoding] Autorelease];return base64string;}     + (nsstring*) Decodebase64data: (NSData *) data {data = [GTMBase64 decodedata:data]; NSString *base64string = [[[NS]String alloc] Initwithdata:data encoding:nsutf8stringencoding] Autorelease];return base64string;} </span>

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.