密码字段的Base64encoding

时间:2011-10-05 20:23:08

标签: iphone xcode4

我现在有这样的POST HTTP请求:

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

    /* 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;
}

1 个答案:

答案 0 :(得分: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