IOS and PHP communication encryption, using AES-CBC no padding

Source: Internet
Author: User

The information on the web is truly voluminous, but it is a handful of really valuable

I tried for more than a day, and finally it was done.

Thank you again for your online predecessors.

For example, the following implementation of the PHP and Java side:

http://my.oschina.net/Jacker/blog/86383

The implementation of PHP and Java side.

Another example of this is the iOS-side implementation:

Http://www.cnblogs.com/wanyakun/p/3403352.html

Why use no padding in this form:

AES encryption if the original input data is not enough 16 bytes of integer digits, it is necessary to complete, if the use of

PKCS7 or PKCS5 This encryption method, the end of the added data may be 0x1,0x2,0x3, not fixed,

After decoding the end of the need to remove the extra characters, it seems more difficult.

If no matter how many bits, the end is ' the ', remove the words easier to operate.

OK, once again, here is the AES128 CBC no padding encryption and decryption method.

First on the iOS side of the code:

 <pre class= "OBJC" name= "code" >////aes128.m//login////Created by Wangdan on 15-3-3.//Copyright (c) 2015 Year Wangdan. All rights reserved.//#import "AES128.h" #import <CommonCrypto/CommonCryptor.h> #import "GTMBase64.h" @ Implementation aes128+ (NSString *) Aes128encrypt: (NSString *) plaintext Withkey: (NSString *) key{if (![    Self Validkey:key]) {return nil;    } Char keyptr[kcckeysizeaes128+1];    memset (keyptr, 0, sizeof (KEYPTR));            [Key Getcstring:keyptr maxlength:sizeof (KEYPTR) encoding:nsutf8stringencoding];    Char ivptr[kccblocksizeaes128+1];    memset (ivptr, 0, sizeof (IVPTR));        [Key Getcstring:ivptr maxlength:sizeof (IVPTR) encoding:nsutf8stringencoding];    nsdata* data = [plaintext datausingencoding:nsutf8stringencoding];        Nsuinteger datalength = [data length];    int diff = kCCKeySizeAES128-(datalength% kCCKeySizeAES128);        unsigned long newSize = 0;     if (diff > 0) {newSize = Datalength + diff;   NSLog (@ "diff is%d", diff);    } Char Dataptr[newsize];    memcpy (dataptr, [data bytes], [data length]);    for (int i = 0; i < diff; i++) {dataptr[i + datalength] =0x0000;    } size_t buffersize = NewSize + kCCBlockSizeAES128;    void *buffer = malloc (buffersize);        memset (buffer, 0, buffersize);        size_t numbytescrypted = 0;                                          Cccryptorstatus cryptstatus = Cccrypt (Kccencrypt, kCCAlgorithmAES128,                                          0x0000, [key utf8string],                                          kCCKeySizeAES128, [key utf8string], Dataptr, sizeof (dataptr), BU         Ffer, buffersize, &numbytescrypted); if (cryptstATUs = = kccsuccess) {NSData *resultdata = [NSData datawithbytesnocopy:buffer length:numbytescrypted];    return [GTMBase64 Stringbyencodingdata:resultdata];    } free (buffer); return nil;}    + (NSString *) processdecodedstring: (NSString *) decoded{if (Decoded==nil | | decoded.length==0) {return nil;    } const char *tmpstr=[decoded utf8string];        int i=0;    while (tmpstr[i]!= ') {i++;    } nsstring *final=[[nsstring alloc]initwithbytes:tmpstr length:i encoding:nsutf8stringencoding];    return final; }+ (NSString *) Aes128decrypt: (NSString *) Encrypttext Withkey: (NSString *) key{if (![    Self Validkey:key]) {return nil;    } char keyptr[kcckeysizeaes128 + 1];    memset (keyptr, 0, sizeof (KEYPTR));        [Key Getcstring:keyptr maxlength:sizeof (KEYPTR) encoding:nsutf8stringencoding];    Char ivptr[kccblocksizeaes128 + 1];    memset (ivptr, 0, sizeof (IVPTR)); [Key Getcstring:ivptr maxlength:sizeof (ivptr) encoding:nsutf8sTringencoding];    NSData *data = [GTMBase64 decodedata:[encrypttext datausingencoding:nsutf8stringencoding];    Nsuinteger datalength = [data length];    size_t buffersize = datalength + kCCBlockSizeAES128;        void *buffer = malloc (buffersize);    size_t numbytescrypted = 0;                                          Cccryptorstatus cryptstatus = Cccrypt (Kccdecrypt, kCCAlgorithmAES128,                                          0x0000, [key utf8string],                                          kCCBlockSizeAES128, [key utf8string],                                          [Data bytes], datalength, Buffer, buffersize, &numbytescrypted    ); if (Cryptstatus = = kccsuccess) {NSData *resultdata = [NSData datawithbytesnocopy:buffer Length:numbytescrypted];        NSString *decoded=[[nsstring alloc] Initwithdata:resultdata encoding:nsutf8stringencoding];    return [self processdecodedstring:decoded];    } free (buffer);    return nil;    }+ (BOOL) Validkey: (nsstring*) key{if (Key==nil | | key.length!=16) {return NO; } return YES;    -(NSString *) processdecodedstring: (NSString *) decoded{if (Decoded==nil | | decoded.length==0) {return nil;    } const char *tmpstr=[decoded utf8string];        int i=0;    while (tmpstr[i]!= ') {i++;    } nsstring *final=[[nsstring alloc]initwithbytes:tmpstr length:i encoding:nsutf8stringencoding];    return final; } @end


The code above needs to explain that, when AES encoding, the input encoding must be 16 bytes of integer times, or call iOS system API will error 4003

The number of bytes padded is all 0

In addition processdecodedstring this function in order to put the decoded string, the end of the "

Here is the PHP side of the code, this code is written by the great God, after the actual experiment can be used:

<?php$privatekey = "1234567812345678"; $iv     = "1234567812345678"; $data   = "Test String";//Encryption $encrypted = Mcrypt_encrypt (mcrypt_rijndael_128, $privateKey, $data, MCRYPT_MODE_CBC, $IV); Echo (Base64_encode ($encrypted)); Echo ' <br/> '; Decrypt $encrypteddata = Base64_decode ("2fbww9+8vpid2/foafzq6q=="); $decrypted = Mcrypt_decrypt (mcrypt_rijndael_128, $ Privatekey, $encryptedData, MCRYPT_MODE_CBC, $IV); Echo ($decrypted);? >


IOS and PHP communication encryption, using AES CBC no padding

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.