0

I have an iPad app which communicates with a webservice. There I can download an encrypted file. In a particular request I get a json with login credentials. Also in that json is a key which is used to encrypt the data.

The key looks like: [0,44,215,1,215,88,94,150]

With the json framework I can put this key into an NSMutableArray. After that I use a AES256 code to decrypt the file. But that code needs a NSString as a key. So my question is: how can I decode that NSMutableArray into an NSString? I guess I first need to put it into an byte arary, and then put it into an NSString?

Who can help me with this one?

Thanks in advance!

3
  • What encoding are the key bytes given as in the JSON response? UTF8? Commented Nov 22, 2011 at 18:26
  • Yes if I'm correct it's UTF8. We use System.Security.Cryptography.RijndaelManaged in C# to encode the file, and if I'm correct, it uses UTF8 Commented Nov 22, 2011 at 18:28
  • Is that what the actual key looks like? If so, then that isn't UTF-8 and if it starts with a null character then you're not going to get very far with NSString. Commented Nov 22, 2011 at 19:13

2 Answers 2

3

Firstly, convert your array of numbers (I assume they're given as NSNumbers) into a C array using code similar to the first snippet in the accepted answer here. In other words, something similar to this:

// Test array for now -- this data will come from JSON response
NSArray* nsArray = [NSArray arrayWithObjects:[NSNumber numberWithChar:1],
                                             [NSNumber numberWithChar:2],
                                             nil];
char cArray[2];

// Fill C-array with ints
int count = [nsArray count];

for (int i = 0; i < count; ++i) {
    cArray[i] = [[nsArray objectAtIndex:i] charValue];
}

Then create an NSString using the correct encoding:

NSString *encodedStr = [NSString stringWithCString:cArray encoding:NSUTF8StringEncoding];

Note: these are code sketches, they haven't been tested!

EDIT: changed from ints to chars.

Sign up to request clarification or add additional context in comments.

2 Comments

stringWithCString: will expect a char *, not an int *, plus, all of the data will be misaligned for UTF-8 interpretation.
Correct, I've changed the code to use chars. It's still only a sketch, final solution will require some wrangling.
0

If your array is the sequence of numbers, you can loop through it

//Assume you have created keyArray from your JSON
NSMutableString * keyString = [NSMutableString string];
for (id element in keyArray) {
    [string appendFormat:@"%@", id];
}

// if you need the comma's in the string
NSMutableString * keyString = [NSMutableString string];
for (id element in keyArray) {
    [string appendFormat:@"%@,", id];
}
int length = [string length];
NSRange range = NSMakeRange(0, length-1);
string = [string substringWithRange:range];

4 Comments

No I don't need a string with numbers, I need a decrypted string. The numbers is an array, also called a byte array, which represents some string value. With the solution you give me I just get the string like 0,44,215,1,215,88,94,150 but I need a string like 'mydecodedstring'
Sorry, I guess I just don't understand your question. It is clear to me now that my suggestion puts you right back where you started. I was focusing on "how can I decode that NSMutableArray into an NSString?" What NSMutableArray are you trying to decode into a string? I would imagine your solution is going to take a form similar to mine. Looping through an array, and doing some decoding in the array, appending the decoded results to your final decoded string
Well the array is just some byte array, so I need the string of that byte array. Decoding those bytes will result in an string
You can still use Maudicus' first sample, but switch %@ to %c and take the intValue on id. That is, assuming your key array supplies byte values?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.