iOS 简单实现base64编码/解码

作者: 大地惊雷 | 来源:发表于2016-06-23 20:11 被阅读292次

1 Base64编码/解码

网上已经有很多介绍Base64原理的博客,给大家推荐一个Base64原理

2.Baes64源码

base64.h

#ifndef _BASE64_H_
#define _BASE64_H_

int base64_encode(char *input, char *output);

int base64_decode(char *input, char *output);

#endif

base64.c

#include "base64.h"
#include <string.h>
#include <stdlib.h>
int base64_encode(char *input, char *output) {

    char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    int length = strlen(input) / 3;
    strlen(input) % 3 > 0 ? length++ : 0 ; 
    char *code = (char *)malloc(sizeof(char ) * length * 4 + 1);
    int *buff = (int *)malloc(sizeof(int) * 4);
    char *temp = (char *)malloc(sizeof(int) * 3);
    for (int i = 0; i <  length; ++i)
    {
        //temp[0] = *(input++);
        //temp[1] = *(input++);
        //temp[2] = *(input++);
        memset(temp, 0, sizeof(temp));
        strncpy(temp, input, len);
        input += 3;

        int lTemp = temp[0]  << 16 | temp[1] << 8 | temp[2];

        buff[0] = lTemp >> 18 & 0X3F;
        buff[1] = lTemp >> 12 & 0X3F;
        buff[2] = lTemp >> 6  & 0X3F;
        buff[3] = lTemp & 0X3F;
        char cstring[4];
        if ( (lTemp & 0x000FFF)  == 0) {
            for (int k = 0; k < 2; ++k) { 
                cstring[k]= *(base64 + buff[k]);
            }
            cstring[2]= '=';
            cstring[3]= '=';
        } else if ( (lTemp & 0x00003F) == 0 ) {
            for (int k = 0; k < 3; ++k) { 
                cstring[k]= *(base64 + buff[k]);
            }
            cstring[3]= '=';
        } else {
            for (int k = 0; k < 4; ++k) {
                cstring[k]= *(base64 + buff[k]);
            }
        }
        strcpy((code + i * 4), cstring);
    }
    free(buff);
    free(temp);
    *(code + strlen(code) + 1) = '\0';
    strcpy(output, code);
    free(code);
    return 0;
}
int getBase64CodeIndex(char code) {
    char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    for (int i = 0; i < strlen(base64); ++i)
    {
        /* code */
        if ( *(base64 + i) == code ) {
            return i;
        }
    } 
    if (code == '=') {
        return 0;
    }
    return -1;
}


int base64_decode(char *input, char *output) {

    int length = strlen(input) / 4;

    char *decode = (char *)malloc(sizeof(char ) * length + 1);
    char *buff = (char *)malloc(sizeof(char) * 3);
    char *temp = (char *)malloc(sizeof(char ) * 4);

    for (int i = 0; i < length; ++i)
    {
        memset(temp, 0, sizeof(temp));
        strncpy(temp, input, 4);
        input += 4;

        for (int i = 0; i < 4; ++i)
        {   
            buff[i] = getBase64CodeIndex((char)*(temp + i));
        }
        int hex = (buff[0] << 18) | (buff[1] << 12) | (buff[2] << 6) | buff[3];

        buff[0] = (char)(hex >> 16);
        buff[1] = (char)(hex >> 8);
        buff[2] = (char)hex;
        strcpy((decode + i * 3), buff);
    }
    *(decode + strlen(decode) + 1) = '\0';
    strcpy(output, decode);
    return 0;
}

test.c

#include <stdio.h>
#include "base64.h"
int main (int argc, char *argv[]) {
    char buff[512] = {0};

    base64_encode("ABDsdaf", buff);
    printf("%s\n", buff);
    base64_decode(buff, buff);
    printf("%s\n", buff);


    return 0;
}

这是我个人的实现思路,还存在大量的bug!

相关文章

网友评论

    本文标题:iOS 简单实现base64编码/解码

    本文链接:https://www.haomeiwen.com/subject/cxhodttx.html