如何解密base 64编码值

时间:2012-03-01 19:25:51

标签: iphone objective-c

我是iphone开发的新手。我正在使用

-(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;
}   

这个加密数据的代码,这是一个逻辑,我也在我的java代码中使用。但我不知道如何在目标c中为这种加密结果方法编写解密逻辑。是否有任何人有想法然后请帮帮我。谢谢你!

1 个答案:

答案 0 :(得分:1)

查看关于Objective-C程序员的http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html Ag精彩博客!