1

Is there a quick and dirty way to scramble an NSString and then unscramble it?

// i.e.
NSString *helloWorld = @"Hello World!";
[helloWorld scrambled]; //helloWorld now = @"W olell!odh"

[helloWorld unscramble]; //helloWorld now = @"Hello World!"

I've already accomplished something similar to this with Base64 encryption, but it adds significant bloat to my string. I don't care about the security level of the scrambling in the slightest, it just needs to be re-ordered so that it isn't human-readable.

jscs
  • 63,694
  • 13
  • 151
  • 195
Bob Yousuk
  • 1,155
  • 3
  • 10
  • 16
  • 1
    possible duplicate of [quick way to jumble the order of an nstring?](http://stackoverflow.com/questions/9003182/quick-way-to-jumble-the-order-of-an-nstring) – Bo A Aug 11 '13 at 18:17
  • @BoA that question doesn't have unscrambling in mind – Bob Yousuk Aug 11 '13 at 18:19
  • 1
    Just save the original string to a variable and read it in the unscramble method? – Bo A Aug 11 '13 at 18:20
  • @BoA won't work for what I'm trying to do – Bob Yousuk Aug 11 '13 at 18:21
  • What idea do you have in mind then? How would the method know what the original string was? It can't just guess. – Bo A Aug 11 '13 at 18:22
  • Need to know what you are trying to achieve in regards to unscrambling, given you can't even keep the original string for unscramble piece. – Reno Jones Aug 11 '13 at 18:28
  • How about reversing it? That's easy to "unscramble". I think you need to specify how "scrambled" you need this. – Dave Aug 11 '13 at 18:32
  • Is the "bloat" actually causing performance problems, or does the base-64 encoding just look longer? – jscs Aug 11 '13 at 18:33

2 Answers2

3

If you can't save the original string, your procedure has to have some kind of a known key so that it's reversible. You're also concerned about the amount of information added to the string, but you don't care about it being particularly hard to decode. I guess you're transmitting the string over the network.

My suggestion is this: use a Cæsar cipher. ROT-13 is the most famous example. Every character in the set of possible input characters is lined up, and that list is paired with another in the same order, but with its start point shifted. The second list provides the output for each character. E.g.,

Original: A B C D E ...
Encoded:  F G H I J ...

(Don't forget punctuation!)

Your "Hello, world!" might come out something like this: "Mjqqt; Btwqi&".

You can thus transmit the key easily by prepending the encoding for 'A' to the string: "FMjqqt; Btwqi&", which is only one extra character. This provides no meaningful security whatsoever -- people solve these things over breakfast -- but it does look like gibberish at a glance.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • 1
    and for anybody looking to implement a Caesar Cipher there's a great one already written on GitHub https://github.com/RyanBibby/Caesar-Cipher-iPhone-App – Bob Yousuk Aug 11 '13 at 18:57
0

Enter all the letters of the word in a array and randomize the array. Remember to save the word to a NSString so you can get it back.

NSString *myString = @"Hello Word";
NSArray *myWords = [myString componentsSeparatedByString:@""];
 NSUInteger count = [myWords count];
for (NSUInteger i = 0; i < count; ++i) {
    // Select a random element between i and end of array to swap with.
    NSInteger nElements = count - i;
    NSInteger n = (arc4random() % nElements) + i;
    [self exchangeObjectAtIndex:i withObjectAtIndex:n];
}
NSString *newWord;
for(NSString *string in myWords){
     newWord = [newWord stringByAppendingString:string];
}
Abdullah Shafique
  • 6,878
  • 8
  • 35
  • 70
  • 3
    This doesn't address the second part of the question, which is un-jumbling the letters – danielbeard Aug 11 '13 at 18:25
  • @danielbeard He can save the word to a NNString – Abdullah Shafique Aug 11 '13 at 18:26
  • But that won't work for what he is trying to do... Whatever that may be... – CaptJak Aug 11 '13 at 18:27
  • @Abdullah He stated in the comments that storing the original string as a variable is not an option (for some reason). – Bo A Aug 11 '13 at 18:35
  • That would work for me, but I am not using it. I am just amused at how @BobYousuk is trying to get an answer to this question when all he is saying is "Nope. Doesn't work. Don't want it" when all of the suggestions are valid. He still hasn't said why saving to a string won't work and why he is saying no to these methods. – CaptJak Aug 11 '13 at 18:35