首页 文章

在P中将PEM转换为DER

提问于
浏览
2

我有PEM格式的证书,我想使用C语言中的OpenSLL函数将其转换为DER格式 .

我该怎么做?

谢谢 .

2 回答

  • 1

    你可以这样做 -

    #include <stdio.h>
    #include <openssl/x509.h>
    #include <openssl/pem.h>
    #include <openssl/err.h>
    
    void convert(char* cert_filestr,char* certificateFile)
    {
        X509* x509 = NULL;
        FILE* fd = NULL,*fl = NULL;
    
        fl = fopen(cert_filestr,"rb");
        if(fl) 
        {
            fd = fopen(certificateFile,"w+");
            if(fd) 
            {
                x509 = PEM_read_X509(fl,&x509,NULL,NULL);
                if(x509) 
                {
                     i2d_X509_fp(fd, x509);
                }
                else 
                {
                    printf("failed to parse to X509 from fl");
                }
                fclose(fd);
            }
            else
            {
                 printf("can't open fd");
            }
            fclose(fl);
        }
        else 
        {
             printf("can't open f");
        }
    }
    
    
    int main()
    {
        convert("abc.pem","axc.der");
        return 0;
    }
    
  • 0

    试试这个 -

    void convert(const unsigned char * pem_string_cert,char* certificateFile)
    {
        X509* x509 = NULL;
        FILE* fd = NULL;
    
        BIO *bio;
    
        bio = BIO_new(BIO_s_mem());
        BIO_puts(bio, pem_string_cert);
        x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
    
        fd = fopen(certificateFile,"w+");
        if(fd) 
        {
                i2d_X509_fp(fd, x509);
        }
        else 
        {
             printf("can't open fd");
        }
        fclose(fd);
    }
    

相关问题