Base 64 encoding is not working in device…!!

Discussion in 'iOS Programming' started by 01.vipin, Mar 7, 2011.

  1. 01.vipin macrumors newbie

    Joined:
    Feb 22, 2011
    #1
    I am using iOS 3.1. The decoding data is successfully running in simulator. But itz not working in device. PlZ HELP!!!
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    Just saying, DUN WORK, HALP.
    Will not motivate alot of people figuring this out.
    What have u tried, and what classes have u tried.
    I have a Base64 encoding string working just fine. I need it to call an URL, and it's coding just fine.
    So, u did something wrong, but we can't fix it, if u don't tell us.
     
  3. 01.vipin, Mar 8, 2011
    Last edited by a moderator: Mar 8, 2011

    01.vipin thread starter macrumors newbie

    Joined:
    Feb 22, 2011
    #3
    Base64.h
    ********
    Code:
    #import <Foundation/Foundation.h>
    
    
    @interface Base64 : NSObject {
    
    }
    + (void) initialize;
    + (NSString*) encode: (const uint8_t*) input length: (NSInteger) length;
    + (NSString*) encode: (NSData*) rawBytes;
    + (NSData*) decode: (const char*) string length:(NSInteger) inputLength;
    + (NSData*) decode: (NSString*) string;
    @end
    Base64.m
    ********


    Code:
    #import "Base64.h"
    
    @implementation Base64
    #define ArrayLength(x) (sizeof(x)/sizeof(*(x)))
    
    static char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    static char decodingTable[128];
    
    + (void) initialize {
        if (self == [Base64 class]) {
            memset(decodingTable, 0, ArrayLength(decodingTable));
            for (NSInteger i = 0; i < ArrayLength(encodingTable); i++) {
                decodingTable[encodingTable[i]] = i;
            }
        }
    }
    
    
    + (NSString*) encode: (const uint8_t*) input length:(NSInteger) length {
        NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
        uint8_t* output = (uint8_t*)data.mutableBytes;
       
        for (NSInteger i = 0; i < length; i += 3) {
            NSInteger value = 0;
            for (NSInteger j = i; j < (i + 3); j++) {
                value <<= 8;
               
                if (j < length) {
                    value |= (0xFF & input[j]);
                }
            }
           
            NSInteger index = (i / 3) * 4;
            output[index + 0] =                    encodingTable[(value >> 18) & 0x3F];
            output[index + 1] =                    encodingTable[(value >> 12) & 0x3F];
            output[index + 2] = (i + 1) < length ? encodingTable[(value >> 6)  & 0x3F] : '=';
            output[index + 3] = (i + 2) < length ? encodingTable[(value >> 0)  & 0x3F] : '=';
        }
       
        return [[[NSString alloc] initWithData:data
                                      encoding:NSASCIIStringEncoding] autorelease];
    }
    
    
    + (NSString*) encode: (NSData*) rawBytes {
        return [self encode: (const uint8_t*) rawBytes.bytes length:rawBytes.length];
    }
    
    
    + (NSData*) decode:(const char*) string length:(NSInteger) inputLength {
        if ((string == NULL) || (inputLength % 4 != 0)) {
            return nil;
        }
       
        while (inputLength > 0 && string[inputLength - 1] == '=') {
            inputLength--;
        }
       
        NSInteger outputLength = inputLength * 3 / 4;
        NSMutableData* data = [NSMutableData dataWithLength:outputLength];
        uint8_t* output = data.mutableBytes;
       
        NSInteger inputPoint = 0;
        NSInteger outputPoint = 0;
        while (inputPoint < inputLength) {
            char i0 = string[inputPoint++];
            char i1 = string[inputPoint++];
            char i2 = inputPoint < inputLength ? string[inputPoint++] : 'A'; /* 'A' will decode to \0 */
            char i3 = inputPoint < inputLength ? string[inputPoint++] : 'A';
           
            output[outputPoint++] = (decodingTable[i0] << 2) | (decodingTable[i1] >> 4);
            if (outputPoint < outputLength) {
                output[outputPoint++] = ((decodingTable[i1] & 0xf) << 4) | (decodingTable[i2] >> 2);
            }
            if (outputPoint < outputLength) {
                output[outputPoint++] = ((decodingTable[i2] & 0x3) << 6) | decodingTable[i3];
            }
        }
       
        return data;
    }
    
    
    + (NSData*) decode: (NSString*) string {
        return [self decode:[string cStringUsingEncoding:NSASCIIStringEncoding] length:string.length];
    }
    
    @end
    encode.h
    ********
    Code:
    #import "Base64.h"
    encode.m
    ********
    Code:
    #import "encode.h"
    
    [Base64 initialize];
    
    NSString *str =[dict objectForKey:@"content:encoded"];
                NSData *data = [str dataUsingEncoding: NSUnicodeStringEncoding];
                NSString *b64EncStr = [Base64 encode:data];

    decode.h
    *******
    Code:
    #import "Base64.h"
    decode.m
    ********

    Code:
    [Base64 initialize];
    
    NSData    *b64DecData = [Base64 decode:rowNumber];
        NSString* decryptedStr = [[NSString alloc] initWithData:b64DecData encoding:NSUnicodeStringEncoding];
        NSString *rowNum = [html stringByAppendingString:decryptedStr];
    Sorry ....... I am new to this iphone programming..!!

    I posted some piece of code here....!!! I think it is enough to understand.If not so please kind to reply me.
    ........
     
  4. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #4
    Code Tags, please..
    I'm using the same Base64 encoding method from Kiichi Takeuchi .

    So let's code the encoding.

    Code:
    	
    NSString *userName = @"username";
    NSString *password = @"password";
    NSError *myError = nil;
    NSMutableString *loginString = (NSMutableString *)[@"" stringByAppendingFormat:@"%@:%@", userName, password];
    NSString *encodedLoginData = [Base64 encode:[loginString dataUsingEncoding:NSUTF8StringEncoding]];
    
    
    NSURL *promotiesUrl = [NSURL URLWithString:@"https://xxx.xx.com/backend-1.0/service/something/get/something/1"];
    	
    NSMutableURLRequest *requestPromoties = [NSMutableURLRequest requestWithURL: promotiesUrl  
    																	cachePolicy: NSURLRequestReloadIgnoringCacheData    
    																timeoutInterval: 3]; 
    	
    	[requestPromoties addValue: encodedLoginData forHTTPHeaderField:@"Authorization"];
    
    NSHTTPURLResponse *responsePromoties;  
    		
    	NSData *dataPromotie = [NSURLConnection    
    							sendSynchronousRequest: requestPromoties    
    							returningResponse: &responsePromoties    
    							error: &myError];    
    After this i do some XML parsing etc. so not usefull to u.
     
  5. 01.vipin thread starter macrumors newbie

    Joined:
    Feb 22, 2011
    #5
    Ya i got it worked .............. Sincere thnx for your Answer..........bro.
     

Share This Page