Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

ios - How to decode / convert a base64 string to NSData?

Hello I am trying to figure out how convert / decode a base64 string in an iOS application to NSData, so I can decrypt the data that I encrypted.

The method I used for converting the NSData to a base 64 string can be found here Is there a similar way to create method to decode / convert the base 64 string to NSData?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is what I was looking for.

+ (NSData *)base64DataFromString: (NSString *)string
{
unsigned long ixtext, lentext;
unsigned char ch, inbuf[4], outbuf[3];
short i, ixinbuf;
Boolean flignore, flendtext = false;
const unsigned char *tempcstring;
NSMutableData *theData;

if (string == nil)
{
    return [NSData data];
}

ixtext = 0;

tempcstring = (const unsigned char *)[string UTF8String];

lentext = [string length];

theData = [NSMutableData dataWithCapacity: lentext];

ixinbuf = 0;

while (true)
{
    if (ixtext >= lentext)
    {
        break;
    }

    ch = tempcstring [ixtext++];

    flignore = false;

    if ((ch >= 'A') && (ch <= 'Z'))
    {
        ch = ch - 'A';
    }
    else if ((ch >= 'a') && (ch <= 'z'))
    {
        ch = ch - 'a' + 26;
    }
    else if ((ch >= '0') && (ch <= '9'))
    {
        ch = ch - '0' + 52;
    }
    else if (ch == '+')
    {
        ch = 62;
    }
    else if (ch == '=')
    {
        flendtext = true;
    }
    else if (ch == '/')
    {
        ch = 63;
    }
    else
    {
        flignore = true; 
    }

    if (!flignore)
    {
        short ctcharsinbuf = 3;
        Boolean flbreak = false;

        if (flendtext)
        {
            if (ixinbuf == 0)
            {
                break;
            }

            if ((ixinbuf == 1) || (ixinbuf == 2))
            {
                ctcharsinbuf = 1;
            }
            else
            {
                ctcharsinbuf = 2;
            }

            ixinbuf = 3;

            flbreak = true;
        }

        inbuf [ixinbuf++] = ch;

        if (ixinbuf == 4)
        {
            ixinbuf = 0;

            outbuf[0] = (inbuf[0] << 2) | ((inbuf[1] & 0x30) >> 4);
            outbuf[1] = ((inbuf[1] & 0x0F) << 4) | ((inbuf[2] & 0x3C) >> 2);
            outbuf[2] = ((inbuf[2] & 0x03) << 6) | (inbuf[3] & 0x3F);

            for (i = 0; i < ctcharsinbuf; i++)
            {
                [theData appendBytes: &outbuf[i] length: 1];
            }
        }

        if (flbreak)
        {
            break;
        }
    }
}

return theData;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...