Note that there are some explanatory texts on larger screens.

plurals
  1. POCCCrypt fails when decrypting in another application
    text
    copied!<p>I have some Encryption and Decryption class methods in my application. I can encrypt 'data' using 'key' and it works great. I can write that data to disk and then read the data in at a later time using the application and call decryptWithKey to decrypt the data block. Everything works great. However, if I include this .m class file into another application, compile that application and try to decrypt that same data which was encrypted with the first application, the CCCrypt(decrypt) call fails...well, not exactly, it returns Success but the data is not decrypted. I have compared the data and key values in both applications and they are identical, down to the byte.</p> <p>Any ideas?</p> <pre><code>+(BOOL)encryptWithKey:(NSMutableData*)data withKey:(NSString *)key { CCCryptorStatus result = kCCSuccess; @try { char keyPtr1[kCCKeySizeAES256+1]; // room for terminator (unused) bzero( keyPtr1, sizeof(keyPtr1) ); // fill with zeroes (for padding) [key getCString: keyPtr1 maxLength: sizeof(keyPtr1) encoding: NSUTF8StringEncoding]; size_t numBytesEncrypted = 0; size_t dataInLength = [data length]; size_t dataOutLength = 18*dataInLength; [data setLength:dataOutLength]; result = CCCrypt( kCCEncrypt,kCCAlgorithmAES128,kCCOptionPKCS7Padding, (const void*)keyPtr1, kCCKeySizeAES256, 0 /* initialization vector (optional) */, [data mutableBytes], dataInLength, /* input */ [data mutableBytes], dataOutLength, /* output */ &amp;numBytesEncrypted ); [data setLength:numBytesEncrypted]; } @catch (NSException *exception) { WDCATCH(exception); } return ( result == kCCSuccess ); } +(BOOL)decryptWithKey:(NSMutableData*)data plaintext:(NSString**)plaintext withKey:(NSString *)key { CCCryptorStatus result = kCCSuccess; @try { // 'key' should be 32 bytes for AES256, will be null-padded otherwise char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused) char szPlaintext[1024]={0}; bzero( keyPtr, sizeof(keyPtr) ); // fill with zeroes (for padding) [key getCString: keyPtr maxLength: sizeof(keyPtr) encoding: NSUTF8StringEncoding]; size_t numBytesEncrypted = 0; result = CCCrypt( kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, (const void*)keyPtr, kCCKeySizeAES256, 0 /* initialization vector (optional) */, [data mutableBytes], [data length], /* input */ szPlaintext,1024,// [data mutableBytes], dataOutLength, /* output */ &amp;numBytesEncrypted ); if (result == kCCSuccess) { szPlaintext[numBytesEncrypted] = 0; *plaintext = [[NSString alloc] initWithFormat:@"%s",szPlaintext]; } } @catch (NSException *exception) { WDCATCH(exception); } return ( result == kCCSuccess ); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload