Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8747497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:21:56+00:00 2026-06-13T12:21:56+00:00

How to convert rsa private key from .DER format to .PEM format programatically using

  • 0

How to convert rsa private key from .DER format to .PEM format programatically using openssl api. Manually I am able to convert using openssl exe. Kindly suggest programtically how to do this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:21:57+00:00Added an answer on June 13, 2026 at 12:21 pm

    There are some functions in C OpenSSL API with name i2d_* to parse an internal type (for the c programming language are structures with name X509,RSA …) to DER and d2i_* to parse DER to an internal type and this is how you can convert an x509 certificate file from DER format to PEM with the c programming language.

    #include <stdio.h>
    #include <openssl/x509.h>
    
    int main(int argc,char* argv[]){
        if(argc != 3) {
            printf("Usage: der2pem derfile pemfile\n");
            return 1;
        }
        // try open the derfile
        FILE* derfile = fopen(argv[1],"r");
        if(derfile) {
            // try open or create the pemfile
            FILE* pemfile = fopen(argv[2],"w+");
            if(pemfile) {
                // parse DER to internal X509 structure
                // d2i is d for DER and i for internal which means c data
                X509* internal = d2i_X509_fp(derfile,NULL);
                if(internal) {
                    // write from the internal X509 to pemfile
                    PEM_write_X509(pemfile,internal);
                }
                else {
                    printf("wasn't possible to parse to X509 from the 'derfile'\n");
                }
                fclose(pemfile);
            }
            else {
                printf("can't create or open the 'pemfile' file\n");
                return 1;
            }
            fclose(derfile);
        }
        else {
            printf("can't open the 'derfile' file\n");
            return 1;
        }
        return 0;
    }
    

    The X.509 digital certificate is a data structure that contains, at minimum, the following fields:

    • The distinguished name of the owner of the public key, also called
      the subject’s name
    • The distinguished name of the issuer of the certificate, also called
      the issuer’s name
    • The public key itself
    • The time period during which the certificate is valid, also called
      the validity period
    • The certificate’s serial number as designated by the issuer
    • The issuer’s digital signature.

    source http://publib.boulder.ibm.com/infocenter/zos/v1r11/index.jsp?topic=/com.ibm.zos.r11.icha700/xcerts.htm

    Which means this example is not for private keys of any type, certificate requests, certificate revocation lists. If you need to convert from these types of DER files you have to dive more into the OpenSSL API, because for example the private keys have to do with encryption and decryption.

    Something @owlstead said and that is true … the PEM format is the base64 encoding of the DER format plus the header and footer. Then i wrote one more example in c, but you need to know the right header and footer to create a complete PEM file:

    #include <stdio.h>
    #include <string.h>
    #include <sys/stat.h>
    #include <openssl/bio.h>
    #include <openssl/evp.h>
    
    int main(int argc,char* argv[]){
        if(argc != 5) {
            printf("Usage: der2pem_simple derfile pemfile \"-----BEGIN CERTIFICATE-----\" \"-----END CERTIFICATE-----\"\n");
            return 1;
        }
        // to create a buffer as small as possible
        // we need the size of the input file
        struct stat fistat;
        if(stat(argv[1],&fistat)<0) {
            printf("derfile not found\n");
            return 1;
        }
        // open the derfile
        FILE* derfile = fopen(argv[1],"r");
        if (!derfile){
            printf("can't open derfile\n");
            return 1;
        }
        char buff[fistat.st_size];
        // write the derfile to the buffer
        fread(buff,fistat.st_size,1,derfile);
        fclose(derfile);
        // open pemfile
        FILE* pemfile = fopen(argv[2],"w+");
        if (!pemfile){
            printf("can't open or create pemfile\n");
            return 1;
        }
        // create a BIO context with base64 filter
        BIO* bio_base64 = BIO_new(BIO_f_base64());
        // create a BIO for the pemfile
        BIO* bio_out = BIO_new_fp(pemfile,BIO_NOCLOSE);
        // write the header
        BIO_write(bio_out,argv[3],strlen(argv[3]));
        BIO_write(bio_out,"\n",1);
        // combine bio_base64->bio_out : enables base64 filter
        bio_out = BIO_push(bio_base64,bio_out);
        // write the buffer
        BIO_write(bio_out,buff,fistat.st_size);
        // flush before disable base64 filter
        BIO_flush(bio_out);
        // uncombine bio_base64 X bio_out : disables base64 filter
        bio_out = BIO_pop(bio_base64);
        // write the footer
        BIO_write(bio_out,argv[4],strlen(argv[4]));
        // flush to free the BIO resources
        BIO_flush(bio_out);
        BIO_free_all(bio_out);
        fclose(pemfile);
    }
    

    and this example in Java:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import org.apache.commons.codec.binary.Base64;
    //import org.apache.commons.codec.binary.Base64OutputStream;
    
    public class der2pem {
        public static void main(String args[]) {
            if(args.length < 4) {
                System.out.println("Usage: der2pem_simple derfile pemfile \"-----BEGIN CERTIFICATE-----\" \"-----END CERTIFICATE-----\"");
                return;
            }
            FileOutputStream pemfile;
            try {
                // open the pemfile
                pemfile = new FileOutputStream(args[1]);
            } catch (FileNotFoundException e) {
                try {
                    // try to create if not found
                    new File(args[1]).createNewFile();
                } catch (IOException e1) {
                    System.out.println(e1.getMessage());
                    return;
                }
                try {
                    // if created open it
                    pemfile = new FileOutputStream(args[1]);
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                    return;
                }
            }
            FileInputStream derfile;
            try {
                // open the derfile
                derfile = new FileInputStream(args[0]);
            } catch (FileNotFoundException e) {
                // if not found print error and get out
                System.out.println(e.getMessage());
                try {
                    pemfile.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                return;
            }
            try {
    //---- last part
            // write the header to pemfile
            pemfile.write(args[2].getBytes());
            pemfile.write('\n');
            // get the size of the derfile and create a buff that fits to this file
            int derf_size = (int) new File(args[0]).length();
            byte[] buffer = new byte[derf_size];
            // read from derfile and write to buffer
            derfile.read(buffer);
            // create the Base64 object for the encoding
            Base64 base64 = new Base64(64,"\n".getBytes());
            // encode and write to pemfile
            pemfile.write(base64.encode(buffer));
            // write the footer to pemfile
            pemfile.write(args[3].getBytes());
            // flush to cleanup
            pemfile.flush();
            pemfile.close();
            derfile.close();
    //---- last part end
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    I tried to use a method with Base64OutputStream but has some unexpected results on the PEM file. If you wanna check just replace the last part with this:

            // write the header to pemfile
            pemfile.write(args[2].getBytes());
            pemfile.write('\n');
            // get the size of the derfile and create a buff that fits to this file
            int derf_size = (int) new File(args[0]).length();
            byte[] buffer = new byte[derf_size];
            // read from derfile and write to buffer
            derfile.read(buffer);
            // create the Base64OutputStream object for the encoding
            Base64OutputStream b64f_pemfile = new Base64OutputStream(pemfile,true,64,"\n".getBytes());
            // write to b64f_pemfile which encodes to base64
            b64f_pemfile.write(buffer);
            // flush before write the footer
            b64f_pemfile.flush();
            // write the footer
            pemfile.write('\n'); 
            pemfile.write(args[3].getBytes());
            // flush to cleanup
            pemfile.flush();
            b64f_pemfile.close();
            pemfile.close();
            derfile.close();
    

    I compiled it with:

    javac der2pem.java -classpath "$CLASSPATH:/usr/share/java/commons-codec.jar"
    

    and ran with:

    java -classpath "$CLASSPATH:/usr/share/java/commons-codec.jar" der2pem test.der tttttt "-----BEGIN CERTIFICATE-----" "-----END CERTIFICATE-----"
    

    something similar you have to do in your system.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How convert RSA public key, from XML to PEM (PHP)?
I'm using C# to generate an RSA public/private key to encrypt user information. I
I used openSSL command to create 2 files: 1 for RSA public key &
Im creating xml string from my RSA key: var keyXmlString = rsaPrivKey.ToXmlString(true); And now
How can I convert this RSA public key: 109120132967399429278860960508995541528237502902798129123468757937266291492576446330739696001110 6039072308886100726558188253585034290 57592827629436413108566029093628 2126359538366865626758497206207862794310902180176810615217550567108238764764442605581471797071 19674283982419152118103759076030616683978566631413 to
i generated a public/private keypair using java. Since i want to add the key
I've been trying for a while to generate an RSA keypair using PHP's openssl
Who knows how to convert PEM encoded X.509 version 2 certificate with RSA public
Hello everyone I'm trying to convert a PKCS#8 private key that I generate in
I want to be able to generate private/public RSA keys with javascript only... So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.