2012-03-01 38 views
0

我新在iphone development.I正在使用如何解密基64編碼的值

-(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[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/"; 
    //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中編寫解密邏輯。是否有人有想法,請幫助我。感謝您提前!

+0

已經在http://stackoverflow.com/questions/392464/any-base64-library-on-iphone-sdk – 2012-03-01 19:40:53

+0

回答使用['uuencode.c'](http://www.nr.com/utils /uuencode.c.txt)和['uudecode.c'](http://www.nr.com/utils/uudecode.c.txt)源文件作爲的地方,從中無恥複製一些代碼您的靈感。這些都是基於64位編碼的優秀,經過時間考驗的實現,具有非常優雅的編碼位操作,您應該沒有問題將其轉換爲Objective C.祝您好運! – dasblinkenlight 2012-03-01 19:41:14

+0

@ChrisJ我測試了你所有關於解密的邏輯,但是它並不工作。無論我從我的ecrypted方法中獲得什麼,我都無法解密一個值。 – Sandeep 2012-03-01 19:50:16

回答