• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Obscuring an NSString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Obscuring an NSString


  • Subject: Re: Obscuring an NSString
  • From: Graham Cox <email@hidden>
  • Date: Fri, 3 Dec 2010 09:16:33 +1100

On 03/12/2010, at 12:30 AM, Adam Gerson wrote:

> I am writing an NSString to a file and I would like to obscure it in a
> two way reversible fashion. It doesn't have to be major hacker proof,
> just not understandable by an average person. I need to be able to
> read the value back in from the file and convert it back to "clear
> text". In php I would just use base64 encode / decode. All the cocoa
> examples I have found show converting in base64 between nsstring and
> nsdata. Is there a smiple function I can pass an NSString through to
> obscure it while keeping it as a string, then reverse it back to clear
> text in a reliable way?


Lots of ways to do this, but a very simple one is to take advantage of the fact that A ^ B ^ B = A, in other words, if you XOR something twice you get back the original.

So convert to bytes [NSString dataUsingEncoding], then XOR each byte with some constant. Save the data. To recover plain text, XOR with the same constant and convert back to a string. The constant should not be 0.

Note that this only obfuscates slightly - the text looks pretty scrambled but is still open to simple techniques such as frequency analysis. If you change the constant in some predetermined fashion for each byte you can make a much stronger 'encryption'.

(typed in mail):


- (NSData*) reversiblyObfuscateData:(NSData*) dataIn
{
    NSMutableData* dataOut = [NSMutableData data];

    NSUInteger n;
    char byte;

    for( n = 0; n < [dataIn length]; ++n )
    {
        [dataIn getBytes:&byte length:1];
        byte ^= 0xA5;
        [dataOut appendBytes:&byte length:1];
    }

    return dataOut;
}

--Graham_______________________________________________

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

References: 
 >Obscuring an NSString (From: Adam Gerson <email@hidden>)

  • Prev by Date: Re: Obscuring an NSString (Adam Gerson)
  • Next by Date: Re: Basic HTTP program failed to compile
  • Previous by thread: Re: Obscuring an NSString
  • Next by thread: Re: Obscuring an NSString
  • Index(es):
    • Date
    • Thread