1、将rsa pem文件,删除首行和尾行,删除换行符
#include <stdio.h>#include <string.h>#include <stdlib.h>#define PUB_KEY_MAX 2048#define PUB_PATH"test4_priv.pem"//#define PUB_PATH"prikey_4.pem"static int read_data_from_file(void *pathname,void *buf, size_t *len){FILE *fp;fp = fopen((char *)pathname, "rb");if (fp == NULL) {printf("%s fopen failed\n", __func__);return -1;}printf("%s %s\n", __func__, pathname);*len = fread(buf, 1,PUB_KEY_MAX, fp);if (*len == 0) {printf("%s fread failed\n", __func__);fclose(fp);return -1;}fclose(fp);return 0;}static void format_key_pem(void *key_pem, size_t *key_pem_len){char temp_buf[PUB_KEY_MAX] = {0};char *p = key_pem;char *line_header;int count;*key_pem_len = 0;while(1){if (memcmp(p, "-----BEGIN", 10) == 0) {while(*p++ != '\n');} else if (memcmp(p, "-----END", 8) == 0) {break;}else {/*0xd : 回车键 \n0xa : 换行键盘*/line_header = p;while(*p++ != '\n');count = p-line_header-1;if (*(line_header + count -1) == 0xd)count --;strncat(temp_buf,line_header,count);*key_pem_len += count;}}memcpy(key_pem,temp_buf,*key_pem_len);printf("*key_pem format len=%d\n",*key_pem_len);printf("key_pem format=%s\n",key_pem);return;}int main(){char buf[PUB_KEY_MAX]={0};unsigned int len;read_data_from_file(PUB_PATH,buf, &len);format_key_pem(buf, &len);return 0;}