Generate certificates and private keys in PKCS#12 format using OpenSSL

To generate a certificate and private key in PKCS12 format using OpenSSL, you can follow these steps:

1. Install OpenSSL

First, make sure OpenSSL is installed on your computer. The version appropriate for your operating system can be downloaded and installed from the official OpenSSL website (https://www.openssl.org/). Here is a brief summary of the steps to install OpenSSL on Windows.

1.1 Download OpenSSL

Download the latest version of OpenSSL from the official OpenSSL website (https://www.openssl.org/).

1.2 Install OpenSSL

Run the downloaded OpenSSL installer and follow the prompts. By default, OpenSSL will be installed in the `C:\Program Files\OpenSSL` directory.

1.3 Configure system environment variables

In Windows operating systems, the path to OpenSSL needs to be added to the system's environment variables to be able to execute OpenSSL commands from any location. Please follow these steps:

   a. Right-click Computer or My Computer and select Properties.

   b. Click "Advanced System Settings" and select "Environment Variables" in the pop-up dialog box.

   c. Find the "Path" variable under "System Variables" and double-click to edit.

   d. Enter the OpenSSL installation path at the end of the variable value, for example `;C:\Program Files\OpenSSL\bin` (please note the semicolon and spaces in the path). Then click "OK".

1.4 Verify installation

Open a command prompt and run the following command to verify that OpenSSL was installed successfully:

openssl version

If you see the OpenSSL version number output, OpenSSL has been successfully installed and configured.


2. Generate private key

Open a terminal or command prompt and run the following command to generate the private key file:

openssl genpkey -algorithm RSA -out private.key

This will generate a private key file named private.key.


3. Generate Certificate Signing Request (CSR)

Using the generated private key, run the following command to generate the CSR file:

openssl req -new -key private.key -out csr.csr

Here you will need to provide some certificate-related information, such as organization name, common name, etc. Enter the required information as prompted.


4. Self-signed certificate

If you want to generate a self-signed certificate, you can use the following command to generate the certificate:

openssl x509 -req -days 3650 -in csr.csr -signkey private.key -out certificate.crt

This will generate a self-signed certificate file named certificate.crt, and the certificate will expire in 3650 days. You can adjust the validity period as needed.


5. Generate PKCS12 file

Now, use the following command to merge the private key and certificate into the PKCS12 file. There are two ways to set a password:

5.1 Custom password method

openssl pkcs12 -export -out certificate.p12 -inkey private.key -in certificate.crt

You will be asked to set a password to protect the PKCS12 file.

5.2 Forced password method

Enforcing password length is not a good practice because it makes the password less secure. If you insist on setting 8 1's as your password, you can use the following command:

openssl pkcs12 -export -out certificate.p12 -inkey private.key -in certificate.crt -passout pass:11111111

This command will generate a `.p12` file from the private key file named `private.key` and the certificate file named `certificate.crt`, and set the password to eight digits 1.

    Please note that enforcing a password length of 8 digits is not a good practice as it makes the password easier to guess or crack. It is recommended that you use longer, more complex passwords and follow best practices to keep your passwords secure.


6. Use OpenSSL to view the contents of the .p12 file

Use the OpenSSL tool to view the contents of the .p12  file. Follow these steps:

  • Open a terminal or command prompt and navigate to the directory containing the.p12 file.
  •  Run the following command to view the contents of the .p12  file:

openssl pkcs12 -info -in certificate.p12

   This command will display the details of the certificate and private key contained in the .p12 file, such as the issuing authority, validity period, etc.

  Check the content of the certificate.p12 file just generated as follows:


E:\RJ\openssl\installed\OpenSSL-Win64\bin>
E:\RJ\openssl\installed\OpenSSL-Win64\bin>openssl pkcs12 -info -in certificate.p12
Enter Import Password:
MAC: sha256, Iteration 2048
MAC length: 32, salt length: 8
PKCS7 Encrypted data: PBES2, PBKDF2, AES-256-CBC, Iteration 2048, PRF hmacWithSHA256
Certificate bag
Bag Attributes
    localKeyID: AF C4 DE AF A0 97 88 29 AB 3E 45 B2 AE 50 E5 85 47 E7 27 77
subject=C = CN, ST = SC, L = CD, O = CETCCST, OU = CETCCST, CN = CETCCST
issuer=C = CN, ST = SC, L = CD, O = CETCCST, OU = CETCCST, CN = CETCCST
-----BEGIN CERTIFICATE-----
MIIDQTCCAikCFCbkxe+aDst30+2ajis12WN5oq3oMA0GCSqGSIb3DQEBCwUAMF0x
CzAJBgNVBAYTAkNOMQswCQYDVQQIDAJTQzELMAkGA1UEBwwCQ0QxEDAOBgNVBAoM
B0NFVENDU1QxEDAOBgNVBAsMB0NFVENDU1QxEDAOBgNVBAMMB0NFVENDU1QwHhcN
MjMxMjE3MDUzNzM3WhcNMjQxMjE2MDUzNzM3WjBdMQswCQYDVQQGEwJDTjELMAkG
A1UECAwCU0MxCzAJBgNVBAcMAkNEMRAwDgYDVQQKDAdDRVRDQ1NUMRAwDgYDVQQL
DAdDRVRDQ1NUMRAwDgYDVQQDDAdDRVRDQ1NUMIIBIjANBgkqhkiG9w0BAQEFAAOC
AQ8AMIIBCgKCAQEApCugCrfRarsT6hVxQiBGdFokur5VOVPyy9Hvfp4AM7SGZnHL
d38IByXWzSqgqbrD+hamKEY77X33/oWueSFB7tFd5Vxspydv6dfCqxRVTp35AyNj
0dzmitjcazNRphqeM/XAqTot95p6fJpykdHWT0v1ekYZyH58ilhxLM+Y24FILVAs
NCsOsSwXIpNzPOwJfIyE2tGGPE4RXVpaysRiPYx9LzwzHwpA6o7AZfl20w1IPNTy
6Ov+spOcDnq5/wmIBcfKxyiQNUM68Sni9FtC/5UjmigsnHDCj3dZKsPQgGf1DYBZ
TiV5DwOpuQA7q3+hUwMaCRZqZG4NDN+27ok9SwIDAQABMA0GCSqGSIb3DQEBCwUA
A4IBAQAsfajDkA5maKJZxW/Gl3hL0gIij6j346lf9RWJMnUX6SDc0mtpg033CzPN
Y3H4Is1VQ7vBMWVZqCQmfQgdmL9UTl8oGprsi3srhjRRWqxslivX+5bAa7f/lIcR
qk8N42CXJ6w+IkyHKArBEG21iz/nLbb2OVcvPu3tVO/I0GLjVjdwr0FJ6GnBH+vv
0QhjKZLR8zCAKhbplqX0pWIMuZdsrekoU/lRTVwGvB0G3oK3HW9EXMmUDiHcPPhC
W16/0zsIHwRfRm6GjuiTHXZxamX9Nnbw5G3BETjdKJdvXwcDl+veZUzl1pOAnutG
VipTLXifLFCD7lADMyIxQV7xcPiX
-----END CERTIFICATE-----
PKCS7 Data
Shrouded Keybag: PBES2, PBKDF2, AES-256-CBC, Iteration 2048, PRF hmacWithSHA256
Bag Attributes
    localKeyID: AF C4 DE AF A0 97 88 29 AB 3E 45 B2 AE 50 E5 85 47 E7 27 77
Key Attributes: <No Attributes>
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIFLT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uG1imNk6hL4IwEpzKN+lcZ2
tDkyc/IGPG9o0O08qgjuQHAoNvK70kRCCqzy9OF8HnKe63n0aX0In0FVGGTIchjm
89/MsGVTKihK+rOyDgCWYhWil9fNhuHt+qdNPXMTvuNkqdDe2XulnN9xkQ8UCXlv
T8GnaBv0GkspgNXABFQ7cvDSSAmrtr+9QvGuEgUOSVvSk8+n5eMOdfD/M4916ezw
Wpo01kgHjptPjpZWveS2spB7VQiNGIEbhU2msmk8ZbQ/F0olzJ/KMWoUgZfl+RLA
5yTlWTEW+dobliWcHLJX/TAyeBICzLU5mPRyE0b3btim9vHVFreC4TWS+sfHiocC
uNvPX0J7XAYvQY188Oq/EbPp8CSRB2Yu0LBS4ilVyooY8tdzVJ2d6aroA4YVSkMe
vM/RZnoLGsezkW+16mP1+TmHAWEk0gLc9BzToHRHI9WJJmv7D+p5XunaeSRSqYro
j8fQK0TrDSvfgHP5Asc1tziRRAUQ1cy9sQV1DUlPGzvRbpQD7RmNJ/yfUt94K2+X
6Vbo9Vgt/iTO9+pjzNRW05HwpkjPajBWNkbze95vSPVFIVhxrtS3tQlEyD26JHw3
6XJvtnOJaRTODUWloIsE7zhDxKk2YhqnlSqfIYzkA3fTGa6lgvIoDyuSv0Ln1g5C
uVwIOK+aag8w5nArZx6vGWulYwILVWEGT40u+GPedWs8
-----END ENCRYPTED PRIVATE KEY-----

E:\RJ\openssl\installed\OpenSSL-Win64\bin>
  • You can also check whether the password for the .p12 file is correct by running the following command:

openssl pkcs12 -nokeys -info -in certificate.p12

   This command will only display the certificate details, not the private key information.

Note: Before running these commands, you need to make sure you have the OpenSSL tools installed.


7. End

Now, a PKCS12 file named certificate.p12 has been generated, which contains the certificate and private key.

Notice:

The above steps only provide a basic example. We can make more configurations and adjustments as needed. Additionally, if you have the CA certificate and private key available, you can use a similar command to generate a PKCS12 file. Make sure to keep and protect your private keys and certificates.

————————————————————

DONE

おすすめ

転載: blog.csdn.net/qq_27706119/article/details/135042789