About the use of AES encryption and SHA256 encryption used in IOS development

Source: Internet
Author: User

* * about the use of AES encryption and SHA256 encryption used in IOS development

**
The author in the previous period of time on this issue with the background of the docking of people, and finally finally determine the problem is our encryption, although all for the same species, but there are essential differences. Below I briefly describe the difference and enclose the main code:
1. Commonly used AES encryption instructions
We commonly used in the development of AES for AES128 and AES256, the difference is that the 256 complement method is more secure, according to a byte of eight bits, the use of encryption keyAES128 for 128/8=16 bit, AES256 for the 256/8=32 bit, said here, Note A parameter: kccoptionpkcs7padding, here Java there are many kinds of authors have not studied, but know that there is another name: kccoptionpkcs5padding, but iOS only one, That is what the above mentioned kccoptionpkcs7padding, then how to do this. Here next encryption key, this parameter and encryption key related, mentioned above, AES128 for 16-bit encryption key,aes256 for 32-bit encryption key, normally, as long as the key to meet this requirement, Whether you use Kccoptionpkcs7padding or kccoptionpkcs5padding is no different, but when the key is less than this number, you need to do the complement, so the difference is out, Kccoptionpkcs7padding is missing several, on a few, such as: Missing 3, 3 3, 5, 5 5,kccoptionpkcs5padding is missing a few to make up a few 0, such as: Missing 3, Make up 3 0. In the use of a parameter can be set, iv vector, this is to set their own, but also do not set up, interested friends can own to understand the next IV vector, here do not do too much description.
Attached below are AES256 and AES128 encrypted code snippets for reference:

 -(NSData *) Aes256_encrypt: (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, Kccoptionp kcs7padding |
                                          Kccoptionecbmode, Keyptr, kCCBlockSizeAES128,
                                          NULL, [self bytes], datalength,
    Buffer, buffersize, &numbytesencrypted);
    if (Cryptstatus = = kccsuccess) {return [NSData datawithbytesnocopy:buffer length:numbytesencrypted];
    Free (buffer); RetUrn Nil; }
-(NSData *) Aes128encryptwithkey: (NSString *) key {//encrypt char keyptr[kcckeysizeaes128+1];
    Bzero (keyptr, sizeof (KEYPTR));

    [Key Getcstring:keyptr maxlength:sizeof (KEYPTR) encoding:nsutf8stringencoding];
    Char ivptr[kcckeysizeaes128+1];
    memset (ivptr, 0, sizeof (IVPTR));

    [GIv getcstring:ivptr maxlength:sizeof (ivptr) encoding:nsutf8stringencoding];
    Nsuinteger datalength = [self length];
    size_t buffersize = datalength + kCCBlockSizeAES128;
    void *buffer = malloc (buffersize);
    size_t numbytesencrypted = 0;
                                          Cccryptorstatus cryptstatus = Cccrypt (Kccencrypt, kCCAlgorithmAES128,
                                          Kccoptionpkcs7padding, Keyptr,
                                          kCCBlockSizeAES128, Ivptr,
       [Self bytes], datalength,                                   Buffer, buffersize,
    &numbytesencrypted);
    if (Cryptstatus = = kccsuccess) {return [NSData datawithbytesnocopy:buffer length:numbytesencrypted];
    Free (buffer);
return nil; }

You can see that AES128 has an IV vector, which can be set manually.

2. Below SHA256 encryption, which is much simpler than AES encryption, Baidu also has a lot of, but the author in use, the code copied from the Internet, the pit is not light, is clearly SHA256, the result became the other encryption results sha, looked at the code to know that the original online write wrong, Here only the SHA256 code is attached, not much to explain.

SHA256 encryption Method
-(NSString *) getsha256string: (NSString *) srcstring {
    const char *CSTR = [srcstring Cstringusingencoding:nsutf8stringencoding];
    NSData *data = [NSData datawithbytes:cstr length:srcString.length];

    uint8_t Digest[cc_sha256_digest_length];

    cc_sha256 (Data.bytes, Data.length, Digest);

    nsmutablestring* result = [nsmutablestring stringwithcapacity:cc_sha256_digest_length * 2];

    for (int i = 0; i < cc_sha256_digest_length i++) {
        [result appendformat:@ '%02x ', Digest[i]];
    }

    return result;
}

From the above code can see that there are 256 words, as for Sha's other encryption method is to directly replace the parameters of the corresponding numbers on the line, interested friends can be used in their own try.
The first time to send a blog, have written bad, but also please forgive me.

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.