原由
项目中经常需要使用base64进行处理,通过base64可以将特殊字符转化为普通可见字符,便于网络传输,代价是增长了传输长度。
base64将每3个byte转化为4个6bit位,然后高位补两个零。这意味着,base64编码后长度会变长。
- 当源文长度不是3的倍数时,需要补零。编码后以等号“=”表示。至多有1至2个补零的情况出现,则结尾最多一至两个等号。
- 编码后长度可预测4*(len+2)/3。
- base64是个可逆编码。
- 源文长度预估如下:末尾等号个数为p(此时p小于等于2),编码长度l (此时l为4的倍数),则源文长度 ( l * 3/4 - p)
源文 | 源长度 | base编码后 | 编码后长度 |
abc | 3 | YWJj | 4 |
abca | 4 | YWJjYQ== | 8 |
abcab | 5 | YWJjYWI= | 8 |
abcabc | 6 | YWJjYWJj | 8 |
源码请见:https://github.com/fpzeng/aes128
C语言实现
base64的源码其实没有几行,但是在项目代码中添加base64源码总有造轮子的嫌疑。这里使用libopenssl库。
#include <stdio.h>
#include <string.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <stdio.h>
#include <math.h> int Base64Encode(const char* message, char** buffer) { //Encodes a string to base64
BIO *bio, *b64;
FILE* stream;
int encodedSize = *ceil((double)strlen(message)/);
*buffer = (char *)malloc(encodedSize+); stream = fmemopen(*buffer, encodedSize+, "w");
b64 = BIO_new(BIO_f_base64());
bio = BIO_new_fp(stream, BIO_NOCLOSE);
bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); //Ignore newlines - write everything in one line
BIO_write(bio, message, strlen(message));
BIO_flush(bio);
BIO_free_all(bio);
fclose(stream); return (); //success
}
int calcDecodeLength(const char* b64input) { //Calculates the length of a decoded base64 string
int len = strlen(b64input);
int padding = ; if (b64input[len-] == '=' && b64input[len-] == '=') //last two chars are =
padding = ;
else if (b64input[len-] == '=') //last char is =
padding = ; return (int)len*0.75 - padding;
} int Base64Decode(char* b64message, char** buffer) { //Decodes a base64 encoded string
BIO *bio, *b64;
int decodeLen = calcDecodeLength(b64message),
len = ;
*buffer = (char*)malloc(decodeLen+);
FILE* stream = fmemopen(b64message, strlen(b64message), "r"); b64 = BIO_new(BIO_f_base64());
bio = BIO_new_fp(stream, BIO_NOCLOSE);
bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); //Do not use newlines to flush buffer
len = BIO_read(bio, *buffer, strlen(b64message));
//Can test here if len == decodeLen - if not, then return an error
(*buffer)[len] = '\0'; BIO_free_all(bio);
fclose(stream); return (); //success
} int main() {
//Encode To Base64
char* base64EncodeOutput;
char* pPlainText = "Hi, I'm fpzeng";
printf("Input plain text: %s\n", pPlainText);
Base64Encode(pPlainText, &base64EncodeOutput);
printf("Output (base64): %s\n", base64EncodeOutput); //Decode From Base64
char* base64DecodeOutput;
Base64Decode(base64EncodeOutput, &base64DecodeOutput);
printf("Output: %s\n", base64DecodeOutput); return();
}
Makefile
.PHONY : clean
OPT=-O0
DEBUG= -g
CFLAGS=-Wall -fPIC $(XCFLAGS) $(INC) $(OPT) $(SO_DEF) $(DEBUG) $(DEF)
INCLUDES = -I./
SRCS = base64.c
OBJS = $(SRCS:.c=.o)
LIBS = -lssl -lcrypto -lm MAIN = base64
all: $(MAIN)
@echo $(MAIN) has been compiled $(MAIN): $(OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS)
@echo output: $(OBJS)
.c.o:
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
clean:
@echo clean $(MAIN) $(OBJS)
rm -fr $(MAIN) $(OBJS)