Reputation: 1578
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!
Upvotes: 0
Views: 3369
Reputation: 17014
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.
Upvotes: 3
Reputation: 7976
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];
Upvotes: 0