Can someone please explain this chunk of code to me (encoding to base64)

Discussion in 'iOS Programming' started by chrono1081, May 26, 2015.

  1. chrono1081 macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #1
    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.
     
  2. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    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:
    [​IMG]

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

    B
     
  3. chrono1081 thread starter macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #3
    Thanks so much! That makes much more sense :)
     

Share This Page