Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chrono1081

macrumors G3
Original poster
Jan 26, 2008
8,456
4,164
Isla Nublar
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:

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.
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
Description in words.

Base64 encoding takes "normal" 8 bit data and spreads it out into 64 individual characters defined by the table array. (Which is basically a dictionary).

Thus you take 4 characters to represent 3 original bytes of binary data.

That's what the
Code:
((length + 2) / 3) * 4
is all about. (The +2 has to do with the last characters of the encoded string (in case you don't have a full set of 3 characters to encode.)

Does that make more sense?

Maybe this link and graphic will help:
Screen%2Bshot%2B2011-08-29%2Bat%2B4.59.39%2BPM.png


http://telliott99.blogspot.com/2011/08/dissecting-rsa-keys-in-python.html

B
 

chrono1081

macrumors G3
Original poster
Jan 26, 2008
8,456
4,164
Isla Nublar
Description in words.

Base64 encoding takes "normal" 8 bit data and spreads it out into 64 individual characters defined by the table array. (Which is basically a dictionary).

Thus you take 4 characters to represent 3 original bytes of binary data.

That's what the
Code:
((length + 2) / 3) * 4
is all about. (The +2 has to do with the last characters of the encoded string (in case you don't have a full set of 3 characters to encode.)

Does that make more sense?

Maybe this link and graphic will help:
Image

http://telliott99.blogspot.com/2011/08/dissecting-rsa-keys-in-python.html

B

Thanks so much! That makes much more sense :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.