0

I am new in iphone development.I am using

-(NSString *)Base64Encode:(NSData *)data
{
    //Point to start of the data and set buffer sizes 
    int inLength = [data length]; 
    int outLength = ((((inLength * 4)/3)/4)*4) + (((inLength * 4)/3)%4 ? 4 : 0); 
    const char *inputBuffer = [data bytes]; 
    char *outputBuffer = malloc(outLength); 
    outputBuffer[outLength] = 0; 
    //64 digit code 
    static char Encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 
    //start the count
    int cycle = 0; 
    int inpos = 0;
    int outpos = 0; 
    char temp; 
    //Pad the last to bytes, the outbuffer must always be a multiple of 4 
    outputBuffer[outLength-1] = '='; 
    outputBuffer[outLength-2] = '=';  

    while (inpos < inLength)
    {     switch (cycle) 
        {         case 0:         
            outputBuffer[outpos++] = Encode[(inputBuffer[inpos]&0xFC)>>2];      
            cycle = 1;            
            break;         
        case 1:            
            temp = (inputBuffer[inpos++]&0x03)<<4; 
            outputBuffer[outpos] = Encode[temp];    
            cycle = 2;       
            break;        
        case 2:         
            outputBuffer[outpos++] = Encode[temp|(inputBuffer[inpos]&0xF0)>> 4];      
            temp = (inputBuffer[inpos++]&0x0F)<<2; 
            outputBuffer[outpos] = Encode[temp];   
            cycle = 3;                            
            break;       
        case 3:      
            outputBuffer[outpos++] = Encode[temp|(inputBuffer[inpos]&0xC0)>>6];   
            cycle = 4;       
            break;    
        case 4:          
            outputBuffer[outpos++] = Encode[inputBuffer[inpos++]&0x3f];  
            cycle = 0;   
            break; 
        default:
            cycle = 0; 
            break;    
        }
    } 
    NSString *pictemp = [NSString stringWithUTF8String:outputBuffer];
    free(outputBuffer);
    return pictemp;
}   

this code for encryption of data and this is a logic which is i am also using in my java code.But I don't hava any idea how to write decryption logic in objective c for this encrypted result method.Is there anyone have idea then please help me.Thanks in Advance!

highlycaffeinated
  • 19,729
  • 9
  • 60
  • 91
Sandeep
  • 725
  • 1
  • 14
  • 28
  • Already answered at http://stackoverflow.com/questions/392464/any-base64-library-on-iphone-sdk – Chris J Mar 01 '12 at 19:40
  • Use [`uuencode.c`](http://www.nr.com/utils/uuencode.c.txt) and [`uudecode.c`](http://www.nr.com/utils/uudecode.c.txt) sources as a place from which to shamelessly copy some code your inspiration. These are great, time-tested implementations of base-64 encoding with very elegantly coded bit manipulations, you should have no problem converting it to Objective C. Good luck! – Sergey Kalinichenko Mar 01 '12 at 19:41
  • @ChrisJ i tested all your logic about decryption but its not working .I am not able to decrypt a value whatever i am getting from my ecrypted method. – Sandeep Mar 01 '12 at 19:50

1 Answers1

1

Check out http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html Ag great blog for Objective-C programmers!

dbrajkovic
  • 3,693
  • 1
  • 17
  • 14
  • @Dear you are providing differnent way .But i don't want this type of encryption and decryption logic.I already did encryption but i am not getting any logic how to decrypt . – Sandeep Mar 02 '12 at 03:56