libb64下载地址http://sourceforge.net/projects/libb64
以下为Demo
libb64_demo.c
#include <stdio.h> #include <string.h> #include <libb64/cencode.h> #include <libb64/cdecode.h> void base64EncodeString(const char *textToEncode, char *buffer); void base64DecodeString(const char *codeToDecode, char *buffer); void base64EncodeFile(FILE *fileToEncode, FILE *encodedFile); void base64DecodeFile(FILE *encodedFile, FILE *decodedFile); int main() { const char *text = "Matt Damon"; char buffer[1000]; base64EncodeString(text, buffer); printf("%s", buffer); const char *code = "R29vZCBXaWxsIEh1bnRpbmc="; base64DecodeString(code, buffer); printf("%s\n", buffer); const char *inputFilename = "易语言5.2修正版.rar"; FILE *inputFile = fopen(inputFilename, "rb"); if (inputFile) { const char *encodedFilename = "易语言5.2修正版.rar.txt"; FILE *encodedFile = fopen(encodedFilename, "wb"); if (encodedFile) { base64EncodeFile(inputFile, encodedFile); fclose(encodedFile); } else printf("Cannot open file: %s\n", encodedFile); fclose(inputFile); } else printf("Cannot open file: %s\n", inputFilename); const char *encodedFilename = "360DrvMgrInstaller_net.exe.txt"; FILE *encodedFile = fopen(encodedFilename, "rb"); if (encodedFile) { const char *decodedFilename = "360DrvMgrInstaller_net"; FILE *decodedFile = fopen(decodedFilename, "wb"); if (decodedFile) { base64DecodeFile(encodedFile, decodedFile); fclose(decodedFile); } else printf("Cannot open file: %s\n", decodedFile); fclose(encodedFile); } else printf("Cannot open file: %s\n", encodedFilename); system("pause"); return 0; } void base64EncodeString(const char *textToEncode, char *buffer) { base64_encodestate state; base64_init_encodestate(&state); int numberOfBytesEncoded = base64_encode_block(textToEncode, strlen(textToEncode), buffer, &state); numberOfBytesEncoded += base64_encode_blockend(buffer + numberOfBytesEncoded, &state); buffer[numberOfBytesEncoded] = '\0'; } void base64DecodeString(const char *codeToDecode, char *buffer) { base64_decodestate state; base64_init_decodestate(&state); int numberOfCharactersDecoded = base64_decode_block(codeToDecode, strlen(codeToDecode), buffer, &state); buffer[numberOfCharactersDecoded] = '\0'; } void base64EncodeFile(FILE *fileToEncode, FILE *encodedFile) { char readBuffer[1024]; char encodedBuffer[2048]; int numberOfBytesEncoded; base64_encodestate state; base64_init_encodestate(&state); while (!feof(fileToEncode)) { size_t numberOfObjects = fread(readBuffer, sizeof(char), 1024, fileToEncode); numberOfBytesEncoded = base64_encode_block(readBuffer, numberOfObjects, encodedBuffer, &state); fwrite(encodedBuffer, sizeof(char), numberOfBytesEncoded, encodedFile); } numberOfBytesEncoded = base64_encode_blockend(encodedBuffer, &state); fwrite(encodedBuffer, sizeof(char), numberOfBytesEncoded, encodedFile); } void base64DecodeFile(FILE *encodedFile, FILE *decodedFile) { char inputBuffer[1024]; char decodedBuffer[2048]; int numberOfBytesDecoded; base64_decodestate state; base64_init_decodestate(&state); while (!feof(encodedFile)) { size_t numberOfCharacters = fread(inputBuffer, sizeof(char), 1024, encodedFile); numberOfBytesDecoded = base64_decode_block(inputBuffer, numberOfCharacters, decodedBuffer, &state); fwrite(decodedBuffer, sizeof(char), numberOfBytesDecoded, decodedFile); } }