Thursday, July 29, 2010

How to save a headache using Jeff LaMarche's NSData-AES category

So after about 30 or so minutes of trying to figure why I could not get my data to decrypt properly after I encrypted it I came across a post by someone finding relief to this problem by changing the encryption keybits to 128 (located in the NSData-AES category *.h file) which did end up doing the trick....sort of, my data had a trailing tail of <202020 202020 202020 202020> which is...you guessed it, whitespace. This was very irritating because as I'm sure you can imagine comparing data with trailing whitespace does not yield nice results. So here is a solution I wrote. There may be more elegant ways to do this but this works for now.

NSData * encryptedData; 
NSString * trimString;
NSData * decryptedData;

encryptedData = [self returnSomeEncryptedDataUsingAPassword:@"password"];
decryptedData = [encryptedData AESDecryptWithAPassphrase:@"password"];
trimString = [[NSString alloc]initWithData:decryptedData

                                                     encoding:NSASCIIStringEncoding];
[
trimString autorelease]; 
trimString = [trimString stringByTrimmingCharactersInSet:[NSCharacterSet whiteSpaceCharacterSet]]; 

decryptedData = [trimString dataUsingEncoding:NSASCIIStringEncoding];






There are of course variations to this for example you could use a temp string opposed to autoreleasing the trim string or instead of creating your own character set you could also use [NSCharacterSet whiteSpaceAndNewlineCharacterSet]; but for this we don't need new lines in the mix




Here is a link to the AES Catergories (NSFileManager works with 256 bit encryption)
http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html

No comments:

Post a Comment