首页 > 其他分享 >Base64编码、解码 C语言例子(使用OpenSSL库)

Base64编码、解码 C语言例子(使用OpenSSL库)

时间:2023-11-29 11:32:42浏览次数:43  
标签:BIO bio Base64 OpenSSL C语言 b64 str out size

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <openssl/pem.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
int base64_encode(char *in_str, int in_len, char *out_str)
{
    BIO *b64, *bio;
    BUF_MEM *bptr = NULL;
    size_t size = 0;
    if (in_str == NULL || out_str == NULL)
        return -1;
    b64 = BIO_new(BIO_f_base64());
    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
    bio = BIO_new(BIO_s_mem());
    bio = BIO_push(b64, bio);
    BIO_write(bio, in_str, in_len);
    BIO_flush(bio);
    BIO_get_mem_ptr(bio, &bptr);
    memcpy(out_str, bptr->data, bptr->length);
    out_str[bptr->length] = '\0';
    size = bptr->length;
    BIO_free_all(bio);
    return size;
}
int base64_decode(char *in_str, int in_len, char *out_str)
{
    BIO *b64, *bio;
    BUF_MEM *bptr = NULL;
    int counts;
    int size = 0;
    if (in_str == NULL || out_str == NULL)
        return -1;
    b64 = BIO_new(BIO_f_base64());
    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
    bio = BIO_new_mem_buf(in_str, in_len);
    bio = BIO_push(b64, bio);
    size = BIO_read(bio, out_str, in_len);
    out_str[size] = '\0';
    BIO_free_all(bio);
    return size;
}
int main()
{
    char instr[] = "hello";
    char outstr1[1024] = {0};
    base64_encode(instr,5,outstr1);
    printf("base64:%s\n",outstr1);
    char outstr2[1024] = {0};
    base64_decode(outstr1,strlen(outstr1),outstr2);
    printf("str:%s\n",outstr2);
    return 0;
}

 



标签:BIO,bio,Base64,OpenSSL,C语言,b64,str,out,size
From: https://blog.51cto.com/u_3078781/8613052

相关文章

  • C语言字节对齐 __align(),__attribute((aligned (n))),#pragma pack(n)
    例子:__align(),__attribute((aligned(n))),#pragmapack(n)#include<stdio.h>main(){structA{inta;charb;shortc;};structB{charb;inta;shortc;};#pragmapack(2)/*指定按2字节对齐*/......
  • [good]c语言数组的运算
    #include<stdio.h>#include<stdlib.h>#include<time.h>#defineMAX10int**createRandom2DArray(introws,intcols){srand(time(NULL));//初始化随机数生成器int**arr=(int**)(malloc(sizeof(int*)*rows));if(arr==NULL)......
  • 《初学C语言第9天》
    //如何写出好的、易调试的代码//#define_CRT_SECURE_NO_WARNINGS//#include<stdio.h>//#include<string.h>//strcpy所需头文件//intmain()//{// //strcpy——字符串拷贝// chararr1[]="##########";// chararr2[]="bit";// strcpy(arr1,arr2);//将arr......
  • 《初学C语言第9天》
    //如何写出好的、易调试的代码//#define_CRT_SECURE_NO_WARNINGS//#include<stdio.h>//#include<string.h>//strcpy所需头文件//intmain()//{// //strcpy——字符串拷贝// chararr1[]="##########";// chararr2[]="bit";// strcpy(arr1,arr2);//将arr......
  • 学习C语言的感想
     进入大学,接触了C语言这一门课程,感觉新奇又有趣,作为一名C语言初学者,带着零基础的好奇心,踏上学习C语言之旅。通过这大半个学期的学习,对C语言有了新的认识,跟着老师的步伐走,学习了C语言程序设计概述、数据类型、运算符、表达式等知识,对这些知识有了一下想法:一、 数据类型,运算符和......
  • 高级语言程序设计(C语言)
    第一章.计算机的发展世界上第一台计算机1946年美国:ENIAC电子数字积分计算机电子管为主要元件,冯.诺依曼提出存储程序概念和二进制思想;1.四个发展时代1>第一代:20世纪50年代,主要采用真空电子管制造计算机2>第二代:20世纪50年代末期:出现了以晶体管为主要元件的第二代......
  • c语言中函数指针用法
    #include<stdio.h>#defineMAX10voidswap(int*x,int*y){inttemp;temp=*x;*x=*y;*y=temp;}voidfun(int*height,int*age){intn=10;*height=n*10;*age=n*2000;}int*createArray(intsize){......
  • C语言(二):整型变量的数值范围
    #include<stdio.h>intmain(){ inta=0; while(a<a+1) { a++; } printf("int类型的最大值是:%d\n\n",a); printf("int类型的最大值+1是:%d\n\n",a+1); intb=0; while(b>b-1) { b--; } printf("int类型的最小值是:%d\n\n",b);......
  • 求解--如何把图片的base64编码转换成图片(未解决)
    问题描述将图片弄好之后,并且显示我弄成功了,但是就是找不到图片保存到哪里了;然后发现可以将base64编码转换成图片,但是不会转~~~求解救呀~~~问题解决未解决!!!......
  • 《初学C语言第8天》
    ////代码调试——调用堆栈//#include<stdio.h.>//voidtest2()//{// printf("hehe");//}//voidtest1()//{// test2();//}//voidtest()//{// test1();//}//intmain()//{// test();// return0;//}////研究程序死循环的原因//#include<stdio.h>//#in......