Hi guys,
I'm struggling with getting RSA encryption to work (WHY is this so difficult on iOS?!) and found this site: http://jslim.net/blog/2013/01/05/rsa-encryption-in-ios-and-decrypt-it-using-php/
And on there they have some code I'm going to try combined with the stuff Apple gives to see if I can get it working but I'm really lost on what exactly this code does:
Any insight would be greatly appreciated. (Or if anyone knows of good working examples of RSA encryption I would greatly love that too, Apples docs don't show the full thing and nothing I've found on Stack Overflow actually works.)
My background is in games so this is new territory for me.
I'm struggling with getting RSA encryption to work (WHY is this so difficult on iOS?!) and found this site: http://jslim.net/blog/2013/01/05/rsa-encryption-in-ios-and-decrypt-it-using-php/
And on there they have some code I'm going to try combined with the stuff Apple gives to see if I can get it working but I'm really lost on what exactly this code does:
Code:
// convert NSData to NSString
- (NSString*)base64forData:(NSData*)theData {
const uint8_t* input = (const uint8_t*)[theData bytes];
NSInteger length = [theData length];
[B]//What is this for?[/B]
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
[B]//What is ((length + 2) / 3) * 4 for?[/B]
NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t* output = (uint8_t*)data.mutableBytes;
[B]//I have no clue what is going on in here, is he breaking the data up into chunks?[/B]
NSInteger i;
for (i=0; i < length; i += 3) {
NSInteger value = 0;
NSInteger j;
for (j = i; j < (i + 3); j++) {
value <<= 8;
if (j < length) {
value |= (0xFF & input[j]);
}
}
[B]//I really don't understand what is going on in here either...[/B]
NSInteger theIndex = (i / 3) * 4;
output[theIndex + 0] = table[(value >> 18) & 0x3F];
output[theIndex + 1] = table[(value >> 12) & 0x3F];
output[theIndex + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '=';
output[theIndex + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '=';
}
return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];
}
Any insight would be greatly appreciated. (Or if anyone knows of good working examples of RSA encryption I would greatly love that too, Apples docs don't show the full thing and nothing I've found on Stack Overflow actually works.)
My background is in games so this is new territory for me.