OPENSSL Base64编码和解码

 

BOOL Base64Encode(unsigned char *pData, int nLeng, int linebreaks, char * pOutBufffer, int *pBufferLenth)

{

    int res = FALSE;

    BIO *bmem, *b64;

    BUF_MEM *bptr;

 

    b64 = BIO_new(BIO_f_base64());

    if (!b64) return res;

    if (!linebreaks)

    {

       BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);

    }

    bmem = BIO_new(BIO_s_mem());

    if (bmem) {

       b64 = BIO_push(b64, bmem);

       if (BIO_write(b64, pData, nLeng)==nLeng)

       {

           (void)BIO_flush(b64);

           BIO_get_mem_ptr(b64, &bptr);

           if (*pBufferLenth > bptr->length)

           {

              memcpy(pOutBufffer, bptr->data, bptr->length);

              pOutBufffer[bptr->length] = 0;

              res = TRUE;

           }

           *pBufferLenth = bptr->length + 1;

       }

    }

 

    BIO_free_all(b64);

    return res;

}

 

BOOL Base64Decode(char *pData, int nLeng, int linebreaks, unsigned char * pOutBufffer, int *pBufferLenth)

{

    int res = FALSE;

    BIO *bmem;

    BIO *b64;

    if (nLeng == 0)

       nLeng = strlen(pData);

    int nMaxLen=(nLeng*6+7)/8;

    int nMiniLen;

    unsigned char *buf = new unsigned char[nMaxLen];

    if (buf)

    {

       b64 = BIO_new(BIO_f_base64());

       if (b64)

       {

           if (!linebreaks)

           {

              BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);

           }

           bmem = BIO_new_mem_buf((char*)pData, nLeng);

           b64 = BIO_push(b64, bmem);

           nMiniLen = BIO_read(b64, buf, nMaxLen);

           if(*pBufferLenth >= nMiniLen)

           {

              memcpy(pOutBufffer, buf, nMiniLen);

              res = TRUE;

           }

           *pBufferLenth = nMiniLen;

           BIO_free_all(b64);

       }

       delete []buf;

    }

    return res;

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
由于Base64编码解码涉及到字符编码和位运算等细节,实现起来比较复杂,因此我们可以使用现成的库函数来实现。以下是使用OpenSSL库函数实现Base64编码解码的示例程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/bio.h> #include <openssl/evp.h> #define MAX_BUF_LEN 1024 int base64_encode(const char *src, int src_len, char *dst, int dst_len) { BIO *b64 = NULL; BIO *bio = NULL; BUF_MEM *bptr = NULL; int ret = -1; if (src == NULL || dst == NULL) { return -1; } b64 = BIO_new(BIO_f_base64()); if (b64 == NULL) { return -1; } bio = BIO_new(BIO_s_mem()); if (bio == NULL) { BIO_free_all(b64); return -1; } bio = BIO_push(b64, bio); BIO_write(bio, src, src_len); BIO_flush(bio); BIO_get_mem_ptr(bio, &bptr); if (bptr == NULL) { BIO_free_all(bio); return -1; } if (bptr->length > dst_len) { BIO_free_all(bio); return -1; } memcpy(dst, bptr->data, bptr->length); dst[bptr->length] = '\0'; ret = bptr->length; BIO_free_all(bio); return ret; } int base64_decode(const char *src, int src_len, char *dst, int dst_len) { BIO *b64 = NULL; BIO *bio = NULL; int ret = -1; if (src == NULL || dst == NULL) { return -1; } b64 = BIO_new(BIO_f_base64()); if (b64 == NULL) { return -1; } bio = BIO_new_mem_buf(src, src_len); if (bio == NULL) { BIO_free_all(b64); return -1; } bio = BIO_push(b64, bio); ret = BIO_read(bio, dst, dst_len); BIO_free_all(bio); return ret; } int main() { char src[MAX_BUF_LEN] = "Hello, world!"; char dst[MAX_BUF_LEN] = {0}; char out[MAX_BUF_LEN] = {0}; int src_len = strlen(src); int dst_len = MAX_BUF_LEN; int out_len = MAX_BUF_LEN; // Base64编码 int len = base64_encode(src, src_len, dst, dst_len); if (len >= 0) { printf("Base64 encoded: %s\n", dst); } else { printf("Base64 encode failed.\n"); return -1; } // Base64解码 len = base64_decode(dst, len, out, out_len); if (len >= 0) { printf("Base64 decoded: %s\n", out); } else { printf("Base64 decode failed.\n"); return -1; } return 0; } ``` 在上面的示例程序中,我们使用了OpenSSL库中的BIO和EVP模块来实现Base64编码解码。其中,`base64_encode`函数接受原始数据和长度,输出Base64编码后的数据和长度。`base64_decode`函数接受Base64编码后的数据和长度,输出解码后的原始数据和长度。在主函数中,我们先对原始数据进行Base64编码,然后再将编码后的数据解码回原始数据。如果编码解码都成功,则输出编码后的数据和解码后的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值