2011-10-05 151 views
0

我喜歡現在這個右門柱HTTP請求:Base64encoding密碼字段

NSString *post =[NSString stringWithFormat:@"memberId=%@&pwd=%@",txtUName.text,txtPwd.text]; 

我的問題是,而不是發送密碼字符串,我想Base64encode密碼字段。我在互聯網上搜索,我發現以下方法。 如何調用此方法來編碼我的密碼?我如何在這裏更改我的發佈請求?

-(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] = '='; 

    /* http://en.wikipedia.org/wiki/Base64 
    Text content M   a   n 
    ASCII   77   97   110 
    8 Bit pattern 01001101 01100001 01101110 

    6 Bit pattern 010011 010110 000101 101110 
    Index   19  22  5  46 
    Base64-encoded T  W  F  u 
    */ 


    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; 
} 
+2

您應該重新考慮在Base64中發送密碼,因爲它非常不安全。 – esqew

+0

我同意。爲什麼不使用crypt()函數來加密密碼併發送該密碼,或者可能創建自己的字符串擾碼器? – Zigglzworth

+0

@ user973637你會爲上面的代碼發佈解密邏輯嗎? – Sandeep

回答

3

雖然我認爲你應該加密,而不是這樣做你的字符串,我相信該方法只需要您的密碼字符串的數據並返回一個新編碼的字符串。

//Make sure you import NSData+Base64Additions.h into your class 

NSData* passwordData = [passwordString dataUsingEncoding:NSASCIIStringEncoding]; 
NSString *base64PasswordString = [passwordData encodeBase64ForData]; 

編輯 - 增加了密鑰文件的位置

您需要下載以下文件,並添加到您的項目: http://skpsmtpmessage.googlecode.com/svn-history/r24/trunk/SMTPSender/Classes/Base64Transcoder.h

http://skpsmtpmessage.googlecode.com/svn-history/r24/trunk/SMTPSender/Classes/Base64Transcoder.m

http://skpsmtpmessage.googlecode.com/svn-history/r2/trunk/SMTPSender/Classes/NSData+Base64Additions.h

http://skpsmtpmessage.googlecode.com/svn-history/r24/trunk/SMTPSender/Classes/NSData+Base64Additions.m

+0

非常感謝您對加密的回答和建議。我會記住,但現在,我在哪裏獲得NSData + Base64Addiitions.h?我是Xcode的新手,這是我的第一個項目。感謝您的幫助。 – user973637

+0

我添加了文件的鏈接。 – Zigglzworth

+0

感謝您的幫助。我已經下載了所有4個文件並添加到了我的項目中。但NSData + Base64Additions.m沒有Base64Encode。它確實有encodeBase64ForData,但它不讓我使用這種方法。我如何使用它而不是跟隨? – user973637