1

I want to decrypt and encrypt a data using AES/CBC/PKCS5Padding in IOS, in android i can use Cipher class to do it, but in IOS dont have those class to use it, right?

Currently i using this to do it, but it seem incorrect.

- (NSData *) DecryptAES: (NSString *) key{
char  keyPtr[kCCKeySizeAES128];
bzero( keyPtr, sizeof(keyPtr) );

[key getCString: keyPtr maxLength: sizeof(keyPtr) encoding: NSUTF8StringEncoding];

size_t numBytesEncrypted = 0;

NSUInteger dataLength = [self length];

size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer_decrypt = malloc(bufferSize);
const unsigned char iv[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

CCCryptorStatus result = CCCrypt( kCCDecrypt , kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                 keyPtr, kCCKeySizeAES128,
                                 iv,
                                 [self bytes], [self length],
                                 buffer_decrypt, bufferSize,
                                 &numBytesEncrypted );

if( result == kCCSuccess )
    return [NSData dataWithBytesNoCopy:buffer_decrypt length:numBytesEncrypted];

return nil;}

how do i suppose to do it same like java did?

i have tried a lot diff way to do it, but it still now working, hope you guys can help me, Thank's.

Additional information: return data:

{"data":"3557793957617431633179755554443638483834686662707a652b7977454c655a654d344e316463513348324e2f2f6e6e4f54783961564e5a4f56426c6e69675a3850644c66734136446f736950516279366b375a5066302f7a424e654b47454c4153547132354c6746724e38432b4d7a3750514c4b3836796f7a54307764614666574e776373716d49766f517552347877766432337778584a796a49457878374e6c354a4f32434755583034722b4770324c79514658704d686e51586553574c6b6939303045754c6a7954494c454977493242796365496a75394b4a2b456347526136527244682b316168533067303651597a6b47713469717a75764d7856"}

encrypt and decrypt step

Cipher: AES (Rijndael block size = 128)

Key: fTG90HGFyeal3kGw

Mode: CBC

IV: CBC random (must append to crypted data)

*base64 is being used in order to make data transmission possible.

Request steps:

1- Collect required data in key-value format
2- JSON encode the collection
3- Encrypt JSON string
4- Generate random IV and append to head of encrypted data
5- Encode crypted data with base64
6- Post 5th item result under a key named “data”

Response steps:
1- Decode JSON string response
2- Decode the value of key named “data” with base64
3- Substring IV from decoded data
4- Decrypt the data
5- JSON decode the result of 4th item
6- Response in key-value format is

I follow the decrypt step to decrypt, i still not able to decrypt data.

  • 2
    *"but it seem incorrect"* is pretty vague. You should at least provide some input data together with the actual and expected output. The corresponding Android/Java code might also be useful. – Martin R Jun 23 '14 at 16:40
  • i have add some information up there, will it useful? – user3214474 Jun 25 '14 at 16:05
  • Perhaps the down-voter would like to point out the problem and help the OP. It does not help unless the error is pointed out. – zaph Jul 01 '14 at 23:43
  • I just posted an acceptable answer in https://stackoverflow.com/questions/40421146/aes-cbc-pkcs5padding-in-ios-objective-c-result-differs-from-android/56178213#56178213. – Julian Corrêa May 17 '19 at 01:06

0 Answers0