Re: Obscuring an NSString
Re: Obscuring an NSString
- Subject: Re: Obscuring an NSString
- From: Adam Gerson <email@hidden>
- Date: Mon, 13 Dec 2010 20:39:37 -0500
Thanks Kyle. Here is the finished working code for anyone else who
stumbles onto this thread. It uses an NSData category found here:
http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
- (NSString *)base64Encode:(NSString *)plainText
{
NSLog(@"%@",plainText);
NSData *plainTextData = [plainText dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [plainTextData base64EncodedString];
NSLog(@"%@",base64String);
return base64String;
}
- (NSString *)base64Decode:(NSString *)base64String
{
NSData *plainTextData = [NSData dataFromBase64String:base64String];
NSString *plainText = [[NSString alloc] initWithData:plainTextData
encoding:NSUTF8StringEncoding];
NSLog(@"%@",plainText);
return plainText;
}
On Sun, Dec 5, 2010 at 11:41 PM, Kyle Sluder <email@hidden> wrote:
> On Dec 5, 2010, at 2:31 PM, Adam Gerson <email@hidden> wrote:
>
>> Thanks to all who responded. Let me explain my situation a little
>> better. I am storing several string values into an XML file. I want to
>> obscure one of them. When I encrpyt the NSString to an NSdata I can
>> store the data as a string in XML, however when I read the string back
>> in I dont know how to convert it back to NSData to decrypt.
>
> You'd better not be treating your XML data as an NSString. ;-)
>
> As long as you store your data in a CDATA section, the XML API you're using should give you the ability to get the raw data for a node.
>
> Alternatively, one of the convenient properties of Base64 is that it maps your data into a range of printable ASCII characters. So you can treat the Base64 data as ASCII and write it as a string in your XML document (being mindful, of course, of the document's encoding, which is most likely UTF-8 and therefore identical to ASCII for the Base64 output range). Then read it back in as a string, convert it to ASCII, and feed the raw ASCII data into the Base64 decoding algorithm.
>
> So to maximize flexibility without involving CDATAs, here's the process I'd use:
>
> Saving: Plaintext NSString --> Plaintext NSData (UTF-8; use -dataWithEncoding:) --> Base64 NSData (ASCII) --> Base64 NSString (use +stringWithData:encoding: to create this) --> your XML document
>
> Loading: Your XML document --> Base64 NSString (UTF-16) --> Base64 NSData (ASCII characters; use -dataWithEncoding to get this) --> Plaintext NSData (UTF-16) --> NSString (again use +stringWithData:encoding:)
>
> HTH,
> --Kyle Sluder
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden