不说废话了,直接上代码了。。。
/*按行读取一个pem文件所有字符并拼接为一整个字符串返回,去除首行、尾行及换行符*/
char* LoadPemFile(char* pemFilePath,int* length)
{
FILE* file;
size_t size = 0;
char* buff;
file = fopen(pemFilePath,"rb");
if(file==NULL)
{
return NULL;
}
fseek(file,0L,SEEK_END); /*定位至文件尾*/
size = ftell(file); /*获取文件大小*/
buff = (char*)malloc(size);
fseek(file,0L,SEEK_SET); /*定位至文件首*/
size = 0;
char line[1024]; //假设单行最长不超过1024,pem文件单行不会超过此限制
while (!feof(file) && !ferror(file))
{
memset(line,0,sizeof(line));
fgets(line, sizeof(line), file);
int len=strlen(line);
line[len-1]=0;//为了去除行尾的\n
if(line[0]!='-') //去除首行和尾行
{
memcpy(buff+size,line,strlen(line));
size += strlen(line);
}
}
fclose(file);
*length = size;
//printf("%s",buff);
return buff;
}
/*解码Base64,参数bindata需预先分配足够内存。返回值为解码后的实际长度*/
int base64_decode( const char * base64, unsigned char * bindata )
{
int i, j;
unsigned char k;
unsigned char temp[4];
const char * base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for ( i = 0, j = 0; base64[i] != '\0' ; i += 4 )
{
memset( temp, 0xFF, sizeof(temp) );
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i] )
temp[0]= k;
}
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i+1] )
temp[1]= k;
}
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i+2] )
temp[2]= k;
}
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i+3] )
temp[3]= k;
}
bindata[j++] = ((unsigned char)(((unsigned char)(temp[0] << 2))&0xFC)) |
((unsigned char)((unsigned char)(temp[1]>>4)&0x03));
if ( base64[i+2] == '=' )
break;
bindata[j++] = ((unsigned char)(((unsigned char)(temp[1] << 4))&0xF0)) |
((unsigned char)((unsigned char)(temp[2]>>2)&0x0F));
if ( base64[i+3] == '=' )
break;
bindata[j++] = ((unsigned char)(((unsigned char)(temp[2] << 6))&0xF0)) |
((unsigned char)(temp[3]&0x3F));
}
return j;
}
/*从一个公钥pem文件中解析出module、exp数据至rsa_pk_t结构体*/
int ParseRsa2048PublicPemFile(char* pemFilePath,rsa_pk_t *pk)
{
int length=0;
char* srcData = LoadPemFile(pemFilePath,&length);
if(srcData==NULL)
{
return -2; //打开文件错误
}
//uint8_t destData[294];
uint8_t destData[1024];
memset(destData,0,sizeof(destData));
length = base64_decode(srcData,destData);
free(srcData);
if(length>=293)
{
int startIndex = 0;
if(destData[0x1f]==1)
{
startIndex = 33;
}
else
{
startIndex = 32;
}
pk->bits = 2048;
memcpy(&pk->modulus [RSA_MAX_MODULUS_LEN-256 ], destData + startIndex, 256);
memcpy(&pk->exponent [RSA_MAX_MODULUS_LEN-3], destData + startIndex + 256 + 2, 3);
return 0;
}
else
{
return -1; //文件长度错误
}
}
C语言解析pem私钥文件写在下一篇文章里了