5

when I encrypt a file(doc, pdf, etc.), I use:

size_t bufferSize = dataLength + kCCBlockSizeAES128;    
CCCrypt( kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                 keyPtr, kCCKeySizeAES256,
                                 NULL /* initialization vector (optional) */,
                                 dataBytes, dataLength, /* input */
                                 buffer, bufferSize,/* output */
                                 &numBytesEncrypted );

when decrypt, I use:

size_t bufferSize = dataLength + kCCBlockSizeAES128;
CCCryptorStatus result = CCCrypt( kCCDecrypt, kCCAlgorithmAES128,    kCCOptionPKCS7Padding,
                                 keyPtr, kCCKeySizeAES256,
                                 NULL /* initialization vector (optional) */,
                                 dataBytes, dataLength,/* input */
                                 buffer, bufferSize,/* output */
                                 &numBytesEncrypted );

But when decrypt, it returns error:kCCDecodeError = -4304.

If I remove the param of kCCOptionPKCS7Padding when decrypt, there is no error. But the file cannot open either.

So, could u tell me how to pass these params?

thanks alot!

EVA
  • 375
  • 1
  • 6
  • 18

3 Answers3

4

This for encryption

    NSString *key =@"YourKey";
    char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
    bzero( keyPtr, sizeof(keyPtr) ); // fill with zeroes (for padding)

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

    char *dataIn = "This is your data";
    char dataOut[500];// set it acc ur data
    bzero(dataOut, sizeof(dataOut));
    size_t numBytesEncrypted = 0;

    CCCryptorStatus result = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,kCCOptionPKCS7Padding,  keyPtr,kCCKeySizeAES256, NULL, dataIn, strlen(dataIn), dataOut, sizeof(dataOut), &numBytesEncrypted);

this is for decryption

char dataOut2[500];
bzero(dataOut2, sizeof(dataOut2));
size_t numBytesDecrypted = 0;   

CCCryptorStatus result = CCCrypt(kCCDecrypt, kCCAlgorithmAES128,kCCOptionPKCS7Padding, keyPtr,kCCKeySizeAES256, NULL, dataOut, numBytesEncrypted, dataOut2, sizeof(dataOut2), &numBytesDecrypted);
Alexander Volkov
  • 7,904
  • 1
  • 47
  • 44
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
  • 2
    you don't check CCCryptorStatus when you encrypt. Are you sure it works? Also, how to you create "buffer". Is it based on bufferSize? – ax123man Apr 12 '11 at 19:13
  • Deriving a key from a string via NSString is quite a tricky process. Because a char byte usually has lots of leading zero and putting a lot of zeros in your AES key. IOS provides a decent key derivation algorithm as described here http://stackoverflow.com/questions/8569555/pbkdf2-using-commoncrypto-on-ios – Nils Jul 31 '14 at 14:02
1

Change the line

bzero(dataOut, sizeof(dataOut2));

to

bzero(dataOut2, sizeof(dataOut2));

Thanks Inder

Sotiris
  • 38,986
  • 11
  • 53
  • 85
Abhilash Divakaran
  • 4,229
  • 1
  • 16
  • 16
0

OK, I got it! Have also tested it successfully. The line should be:

CCCrypt(kCCDecrypt, kCCAlgorithmAES128,kCCOptionPKCS7Padding, keyPtr,kCCKeySizeAES256, NULL, dataOut, numBytesEncrypted, dataOut2, sizeof(dataOut2), &numBytesDecrypted);
oers
  • 18,436
  • 13
  • 66
  • 75
william.shark
  • 91
  • 1
  • 8
  • Hi, Could you please share the full code. i am also trying the file encryption and decryption. Whats dataIn and dataout here. its file path?. how to test in ios device. I am totally new in ios. Please help me. Thanks – user Mar 16 '20 at 06:53