Public and private keys are a mathematically linked key pair generated by an asymmetric encryption algorithm. Together with an SSL/TLS certificate, they enable secure communication between a client and a server — the public key encrypts data and verifies signatures, while the private key decrypts data and creates signatures.
This topic explains how these keys work in SSL/TLS, and shows you how to generate a private key and a Certificate Signing Request (CSR) using OpenSSL or keytool.
Key concepts
Public key and private key
A public key and a private key are always generated as an inseparable pair:
Public key — shared publicly. Encrypts data sent to the key owner, and verifies signatures created by the private key.
Private key — kept strictly confidential. Decrypts data encrypted by the public key, and creates digital signatures that prove the sender's identity.
Keeping the private key secure is essential. If it is compromised, all encrypted communications are at risk, and the associated certificate becomes unusable.
For details on how Certificate Management Service protects private keys, see How does Certificate Management Service protect private keys?
How it works
SSL/TLS establishes a secure channel in five steps. Steps 1–3 use asymmetric encryption (public/private key); steps 4–5 switch to faster symmetric encryption using a session key.
Certificate presentation — the server sends its SSL certificate to the client. The certificate contains the server's public key, identity information (domain name), a certification authority (CA) digital signature, and the certificate validity period.
Certificate verification (client-side) — the client verifies the certificate chain in order: server certificate → intermediate certificate → root certificate. The chain is trusted if it traces back to a root certificate in the client's local trust store. The client also checks the validity period and confirms the domain name matches the endpoint being accessed.
NoteThis process relies on the root certificate store pre-installed in the client's operating system or browser.
Private key proof — the server proves it owns the private key that matches the certificate. The mechanism depends on the key exchange method in use:
Key exchange How the server proves private key ownership RSA Decrypts the pre-master secret sent by the client DH/ECDH Signs the ServerKeyExchangemessageTLS 1.3 Signs the CertificateVerifymessageSession key negotiation — both parties derive an identical session key from the exchanged information.
Encrypted communication — all subsequent data is encrypted with the session key using symmetric encryption, combining efficiency with security.
Generate a private key
Certificate Management Service requires private keys to use the RSA algorithm with a minimum key length of 2048 bits.
If you purchase a certificate through Certificate Management Service with CSR Generation set to Automatic, Certificate Management Service generates a key pair on your behalf. You can skip key generation and CSR creation.
Generate a new private key
Install OpenSSL. Download the installation package from the OpenSSL website. Use an officially supported, stable version — 1.1.1 or 3.x. To check your current version:
openssl versionGenerate the private key file. The commands below create a Privacy-Enhanced Mail (PEM) formatted RSA private key named
myprivate.pem. Run one command based on your security requirements:# 2048-bit key (minimum requirement) openssl genrsa -out myprivate.pem 2048 # 4096-bit key (stronger security) openssl genrsa -out myprivate.pem 4096 # 4096-bit key with AES-256 password protection (recommended for high-security environments) openssl genrsa -aes256 -out myprivate.pem 4096
Export a private key from a Java Keystore (JKS)
Use this method if you have a key pair in a .jks file (created with keytool) and need to export it in PEM format — for example, to configure Nginx, Apache, or a CDN.
Use the Certificate Management Service format conversion tool
Certificate Management Service provides a free online tool that converts a JKS keystore to PEM-formatted key and certificate files in a single step. See Convert the format of a certificate.
Use the OpenSSL CLI
Convert JKS to PKCS12 format. The
keytoolCLI cannot export private keys directly to PEM. First convert the JKS to the more portable PKCS12 (.p12) format. Replacemydomainwith your actual alias:keytool -importkeystore \ -srckeystore mydomain.jks \ -destkeystore mydomain.p12 \ -deststoretype PKCS12 \ -srcalias mydomain \ -destalias mydomainYou will be prompted for the source JKS password and a new password for the
.p12file.Extract the PEM-formatted private key. Use OpenSSL to extract the private key from the
.p12file. You will be prompted for the.p12password set in the previous step:-nodes— do not encrypt the output private key-nocerts— output only the private key, not the certificate
# Standard command openssl pkcs12 -in mydomain.p12 -nodes -nocerts -out myprivate.pem # If you get an "unsupported algorithm" error in OpenSSL 3.x, add -legacy # Note: -legacy enables deprecated encryption algorithms openssl pkcs12 -in mydomain.p12 -nodes -nocerts -out myprivate.pem -legacyFlags:
The resulting myprivate.pem is the file you need for certificate configuration.
Private key file formats
A PEM private key file appears in one of two formats:
PKCS#1
(RSA-specific, defined by the RSA cryptography standard)
-----BEGIN RSA PRIVATE KEY-----
(Base64-encoded key content)
-----END RSA PRIVATE KEY-----PKCS#8
(algorithm-agnostic, supports RSA, ECC, and other private key algorithms)
-----BEGIN PRIVATE KEY-----
(Base64-encoded key content)
-----END PRIVATE KEY-----Store your private key file securely. If it is lost or compromised, the associated certificate becomes unusable and any data encrypted with the corresponding public key is at risk.
Create a Certificate Signing Request (CSR)
A CSR packages your public key and identity information for submission to a CA. To submit a CSR manually in Certificate Management Service:
Generate the CSR file using your private key.
# -key myprivate.pem Specifies the private key file generated earlier # -out mydomain.csr Specifies the output CSR filename openssl req -new -key myprivate.pem -out mydomain.csrThe command prompts for: country, state or province, city, organization, and Common Name. The Common Name must exactly match the domain name for the certificate — for example,
www.aliyun.com.Submit the CSR in Certificate Management Service. When purchasing, creating, or applying for a certificate, set CSR Generation to Manual and paste the contents of
mydomain.csr. For details, see:
What's next
Learn how Certificate Management Service protects your private keys: How does Certificate Management Service protect private keys?
Convert an existing certificate to a different format: Convert the format of a certificate