Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since you haven't posted any code, it's difficult to know exactly which problems you're encountering. However, the blog post you link to does seem to work pretty decently... aside from the extra comma in each call to <code>CCCrypt()</code> which caused compile errors.</p> <p>A later comment on that post includes <a href="http://pastie.org/426530" rel="nofollow noreferrer">this adapted code</a>, which works for me, and seems a bit more straightforward. If you include their code for the NSData category, you can write something like this: (Note: The <code>printf()</code> calls are only for demonstrating the state of the data at various points — in a real application, it wouldn't make sense to print such values.)</p> <pre><code>int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSString *key = @"my password"; NSString *secret = @"text to encrypt"; NSData *plain = [secret dataUsingEncoding:NSUTF8StringEncoding]; NSData *cipher = [plain AES256EncryptWithKey:key]; printf("%s\n", [[cipher description] UTF8String]); plain = [cipher AES256DecryptWithKey:key]; printf("%s\n", [[plain description] UTF8String]); printf("%s\n", [[[NSString alloc] initWithData:plain encoding:NSUTF8StringEncoding] UTF8String]); [pool drain]; return 0; } </code></pre> <p>Given this code, and the fact that encrypted data will not always translate nicely into an NSString, it may be more convenient to write two methods that wrap the functionality you need, in forward and reverse...</p> <pre><code>- (NSData*) encryptString:(NSString*)plaintext withKey:(NSString*)key { return [[plaintext dataUsingEncoding:NSUTF8StringEncoding] AES256EncryptWithKey:key]; } - (NSString*) decryptData:(NSData*)ciphertext withKey:(NSString*)key { return [[[NSString alloc] initWithData:[ciphertext AES256DecryptWithKey:key] encoding:NSUTF8StringEncoding] autorelease]; } </code></pre> <p>This definitely works on Snow Leopard, and <strong>@Boz</strong> reports that CommonCrypto is part of the Core OS on the iPhone. Both 10.4 and 10.5 have <code>/usr/include/CommonCrypto</code>, although 10.5 has a man page for <code>CCCryptor.3cc</code> and 10.4 doesn't, so YMMV.</p> <hr> <p><strong>EDIT:</strong> See <a href="https://stackoverflow.com/questions/1417893/">this follow-up question</a> on using Base64 encoding for representing encrypted data bytes as a string (if desired) using safe, lossless conversions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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