I am trying to encrypt a string in Xcode and decrypt it with PHP or encrypt with PHP and decrypt with Xcode.So first of all I am trying to get the same \[code\]AES encryption\[/code\] result.Here is my Xcode file (I've put everything in a single file for simplicity):\[code\]#import "t.h"#import <CommonCrypto/CommonCryptor.h>@interface NSData(AES)- (NSData*)AES256EncryptWithKey
NSString*)key;- (NSData*)AES256DecryptWithKey
NSString*)key;@end@implementation NSData (AES)- (NSData*)AES256EncryptWithKey
NSString*)key {char keyPtr[kCCKeySizeAES256];[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSASCIIStringEncoding];NSString *iv = @"1234567812345678";char ivPtr[kCCKeySizeAES128];[iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSASCIIStringEncoding]; NSUInteger dataLength = [self length];size_t bufferSize = dataLength + kCCBlockSizeAES128;void* buffer = malloc(bufferSize);size_t numBytesEncrypted = 0;CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, NULL, keyPtr, kCCKeySizeAES256, ivPtr /* initialization vector (optional) */, [self bytes], dataLength, /* input */ buffer, bufferSize, /* output */ &numBytesEncrypted);if (cryptStatus == kCCSuccess){ return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];}free(buffer); //free the buffer;return nil;}- (NSData*)AES256DecryptWithKey
NSString*)key {char keyPtr[kCCKeySizeAES256]; NSString *iv = @"1234567812345678";char ivPtr[kCCKeySizeAES128];[iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSASCIIStringEncoding];// fetch key data[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSASCIIStringEncoding];NSUInteger dataLength = [self length];size_t bufferSize = dataLength + kCCBlockSizeAES128;void* buffer = malloc(bufferSize);size_t numBytesDecrypted = 0;CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, NULL, keyPtr, kCCKeySizeAES256, ivPtr /* initialization vector (optional) */, [self bytes], dataLength, /* input */ buffer, bufferSize, /* output */ &numBytesDecrypted);if (cryptStatus == kCCSuccess){ return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];}free(buffer); //free the buffer;return nil;}@end@interface t ()@end@implementation t- (void)viewDidLoad{[super viewDidLoad];NSString *key = @"12345678123456781234567812345678";NSData *plaintext = [[@"aaa0000000000000" dataUsingEncoding:NSASCIIStringEncoding] AES256EncryptWithKey: key];label1.text = [[NSString alloc] initWithData
laintext encoding:NSASCIIStringEncoding];}@end\[/code\]And now the PHP code: \[code\]<?php$key256 = "12345678123456781234567812345678";$iv128 = "1234567812345678";$text = "aaa";$blocksize = 16;$len = strlen($text);$pad = $blocksize - ($len % $blocksize);$text .= str_repeat("0", $pad);$cipher = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key256, $text, MCRYPT_MODE_CBC, $iv128);echo $cipher;?>\[/code\]The Xcode encrypted string and the php encrypted string are different.PHP: \[code\]a5
![Frown :( :(](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f641.png)
![Frown :( :(](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f641.png)
![Frown :( :(](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f641.png)
![Frown :( :(](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f641.png)
![Stick Out Tongue :p :p](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f61b.png)