base64編解碼
工作中經常會用到base64編解碼, 有些開源庫中也有實現, 但是如果再去看他們的怎么用有時候也是有點費勁的, 還有就是需要引用那個頭文件啊, 什么的, 尤其是OpenSSL里邊的, 所以這里獻上原理, 及其使用.
至于用途還有詳細的介紹我覺得某度某科里講的挺好的, 這里就只寫上實現原理及代碼了.
詳細請看base64.h 和base64.c, 使用見main.c 即可, 可以使用任何編譯器編譯運行, 下面依次是base64.h, base64.c, main.c如果需要源文件可以留言哦, 對你有幫助的話贊一吧, 之后也會把之前的干貨, 竟然分享給大家的.請多多支持.
這里我盡量使用了簡單的代碼結構實現的, 容易理解一點, 如果你理解之后可以加之優(yōu)化的. 大神請飄過...
//
// base64.h
// base64
//
// Created by guofu on 2017/5/25.
// Copyright ? 2017年 guofu. All rights reserved.
//
#ifndef base64_h
#define base64_h
#include <stdio.h>
#if __cplusplus
extern "C"{
#endif
int base64_encode(const char *indata, int inlen, char *outdata, int *outlen);
int base64_decode(const char *indata, int inlen, char *outdata, int *outlen);
#if __cplusplus
}
#endif
#endif /* base64_h */
//
// base64.c
// base64
//
// Created by guofu on 2017/5/25.
// Copyright ? 2017年 guofu. All rights reserved.
//
/**
* 轉解碼過程
* 3 * 8 = 4 * 6; 3字節(jié)占24位, 4*6=24
* 先將要編碼的轉成對應的ASCII值
* 如編碼: s 1 3
* 對應ASCII值為: 115 49 51
* 對應二進制為: 01110011 00110001 00110011
* 將其6個分組分4組: 011100 110011 000100 110011
* 而計算機是以8bit存儲, 所以在每組的高位補兩個0如下:
* 00011100 00110011 00000100 00110011對應:28 51 4 51
* 查找base64 轉換表 對應 c z E z
*
* 解碼
* c z E z
* 對應ASCII值為 99 122 69 122
* 對應表base64_suffix_map的值為 28 51 4 51
* 對應二進制值為 00011100 00110011 00000100 00110011
* 依次去除每組的前兩位, 再拼接成3字節(jié)
* 即: 01110011 00110001 00110011
* 對應的就是s 1 3
*/
#include "base64.h"
#include <stdio.h>
#include <stdlib.h>
// base64 轉換表, 共64個
static const char base64_alphabet[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'+', '/'};
// 解碼時使用
static const unsigned char base64_suffix_map[256] = {
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 253, 255,
255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 253, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255 };
static char cmove_bits(unsigned char src, unsigned lnum, unsigned rnum) {
src <<= lnum; // src = src << lnum;
src >>= rnum; // src = src >> rnum;
return src;
}
int base64_encode(const char *indata, int inlen, char *outdata, int *outlen) {
int ret = 0; // return value
if (indata == NULL || inlen == 0) {
return ret = -1;
}
int in_len = 0; // 源字符串長度, 如果in_len不是3的倍數, 那么需要補成3的倍數
int pad_num = 0; // 需要補齊的字符個數, 這樣只有2, 1, 0(0的話不需要拼接, )
if (inlen % 3 != 0) {
pad_num = 3 - inlen % 3;
}
in_len = inlen + pad_num; // 拼接后的長度, 實際編碼需要的長度(3的倍數)
int out_len = in_len * 8 / 6; // 編碼后的長度
char *p = outdata; // 定義指針指向傳出data的首地址
//編碼, 長度為調整后的長度, 3字節(jié)一組
for (int i = 0; i < in_len; i+=3) {
int value = *indata >> 2; // 將indata第一個字符向右移動2bit(丟棄2bit)
char c = base64_alphabet[value]; // 對應base64轉換表的字符
*p = c; // 將對應字符(編碼后字符)賦值給outdata第一字節(jié)
//處理最后一組(最后3字節(jié))的數據
if (i == inlen + pad_num - 3 && pad_num != 0) {
if(pad_num == 1) {
*(p + 1) = base64_alphabet[(int)(cmove_bits(*indata, 6, 2) + cmove_bits(*(indata + 1), 0, 4))];
*(p + 2) = base64_alphabet[(int)cmove_bits(*(indata + 1), 4, 2)];
*(p + 3) = '=';
} else if (pad_num == 2) { // 編碼后的數據要補兩個 '='
*(p + 1) = base64_alphabet[(int)cmove_bits(*indata, 6, 2)];
*(p + 2) = '=';
*(p + 3) = '=';
}
} else { // 處理正常的3字節(jié)的數據
*(p + 1) = base64_alphabet[cmove_bits(*indata, 6, 2) + cmove_bits(*(indata + 1), 0, 4)];
*(p + 2) = base64_alphabet[cmove_bits(*(indata + 1), 4, 2) + cmove_bits(*(indata + 2), 0, 6)];
*(p + 3) = base64_alphabet[*(indata + 2) & 0x3f];
}
p += 4;
indata += 3;
}
if(outlen != NULL) {
*outlen = out_len;
}
return ret;
}
int base64_decode(const char *indata, int inlen, char *outdata, int *outlen) {
int ret = 0;
if (indata == NULL || inlen <= 0 || outdata == NULL || outlen == NULL) {
return ret = -1;
}
if (inlen % 4 != 0) { // 需要解碼的數據不是4字節(jié)倍數
return ret = -2;
}
int t = 0, x = 0, y = 0, i = 0;
unsigned char c = 0;
int g = 3;
while (indata[x] != 0) {
// 需要解碼的數據對應的ASCII值對應base64_suffix_map的值
c = base64_suffix_map[indata[x++]];
if (c == 255) return -1;// 對應的值不在轉碼表中
if (c == 253) continue;// 對應的值是換行或者回車
if (c == 254) { c = 0; g--; }// 對應的值是'='
t = (t<<6) | c; // 將其依次放入一個int型中占3字節(jié)
if (++y == 4) {
outdata[i++] = (unsigned char)((t>>16)&0xff);
if (g > 1) outdata[i++] = (unsigned char)((t>>8)&0xff);
if (g > 2) outdata[i++] = (unsigned char)(t&0xff);
y = t = 0;
}
}
if (outlen != NULL) {
*outlen = i;
}
return ret;
}
//
// main.c
// base64
//
// Created by guofu on 2017/5/25.
// Copyright ? 2017年 guofu. All rights reserved.
//
#include <stdio.h>
#include <string.h>
#include "base64.h"
int main(int argc, const char * argv[]) {
// insert code here...
char str1[] = "abcdefg";
char str2[20] = {0}; //注意長度要給夠
int len = 0;
base64_encode(str1,(int)strlen(str1),str2,&len);
printf("%s, len = %d\n", str2, len);
char str3[10] = {0};
base64_decode(str2, (int)strlen(str2), str3, &len);
printf("%s, len = %d\n", str3, len);
return 0;
}