码迷,mamicode.com
首页 > 编程语言 > 详细

在C语言中使用Libb64进行Base64编解码

时间:2018-06-11 02:08:11      阅读:527      评论:0      收藏:0      [点我收藏+]

标签:write   for   pac   bat   语言   color   txt   pil   buffer   

Libb64下载地址http://sourceforge.net/projects/libb64

以下为Demo

CLibb64Demo.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 = "E:/Packages/Java/Android/docs-24_r01.zip";
    FILE *inputFile = fopen(inputFilename, "rb");
    if (inputFile)
    {
        const char *encodedFilename = "E:/Packages/Java/Android/docs-24_r01.zip.txt";
        FILE *encodedFile = fopen(encodedFilename, "wb");
        if (encodedFile)
        {
            base64EncodeFile(inputFile, encodedFile);
            fclose(encodedFile);
        }
        else
            printf("Cannot open file: %s", encodedFile);
        fclose(inputFile);
    }
    else
        printf("Cannot open file: %s", inputFile);
    const char *encodedFilename = "en_office_professional_2007_cd_X12-42316.iso.txt";
    FILE *encodedFile = fopen(encodedFilename, "rb");
    if (encodedFile)
    {
        const char *decodedFilename = "office";
        FILE *decodedFile = fopen(decodedFilename, "wb");
        if (decodedFile)
        {
            base64DecodeFile(encodedFile, decodedFile);
            fclose(decodedFile);
        }
        else
            printf("Cannot open file: %s", decodedFile);
        fclose(encodedFile);
    }
    else
        printf("Cannot open file: %s", encodedFile);
    return 0;
}
void base64EncodeString(const char *textToEncode, char *buffer)
{
    base64_encodestate state;
    base64_init_encodestate(&state);
    int numberOfEncodedBytes = base64_encode_block(textToEncode, strlen(textToEncode), buffer, &state);
    numberOfEncodedBytes += base64_encode_blockend(buffer + numberOfEncodedBytes, &state);
    buffer[numberOfEncodedBytes] = \0;
}
void base64DecodeString(const char *codeToDecode, char *buffer)
{
    base64_decodestate state;
    base64_init_decodestate(&state);
    int numberOfCharacters = base64_decode_block(codeToDecode, strlen(codeToDecode), buffer, &state);
    buffer[numberOfCharacters] = \0;
}
void base64EncodeFile(FILE *fileToEncode, FILE *encodedFile)
{
    char readBuffer[1024];
    char encodedBuffer[2048];
    int numberOfEncodedBytes;
    base64_encodestate state;
    base64_init_encodestate(&state);
    while (!feof(fileToEncode))
    {
        size_t numberOfObjects = fread(readBuffer, sizeof(char), 1024, fileToEncode);
        numberOfEncodedBytes = base64_encode_block(readBuffer, numberOfObjects, encodedBuffer, &state);
        fwrite(encodedBuffer, sizeof(char), numberOfEncodedBytes, encodedFile);
    }
    numberOfEncodedBytes = base64_encode_blockend(encodedBuffer, &state);
    fwrite(encodedBuffer, sizeof(char), numberOfEncodedBytes, encodedFile);
}
void base64DecodeFile(FILE *encodedFile, FILE *decodedFile)
{
    char inputBuffer[1024];
    char decodedBuffer[2048];
    int numberofDecodedBytes;
    base64_decodestate state;
    base64_init_decodestate(&state);
    while (!feof(encodedFile))
    {
        size_t numberOfCharacters = fread(inputBuffer, sizeof(char), 1024, encodedFile);
        numberofDecodedBytes = base64_decode_block(inputBuffer, numberOfCharacters, decodedBuffer, &state);
        fwrite(decodedBuffer, sizeof(char), numberofDecodedBytes, decodedFile);
    }
}

Compilation.bat

gcc -std=c11 CLibb64Demo.c libb64/*.c -o CLibb64Demo
CLibb64Demo

 

在C语言中使用Libb64进行Base64编解码

标签:write   for   pac   bat   语言   color   txt   pil   buffer   

原文地址:https://www.cnblogs.com/buyishi/p/9165216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!