0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

OpenSSLを使わずにp12→pem変換Pythonコード

0
Last updated at Posted at 2024-05-31

OpenSSLが使えない時でもcryptographyでp12→pem変換

自分用
必要になったので書きます。windows11環境です。

pip install cryptography
from cryptography.hazmat.primitives import serialization

def convert_p12_to_pem(p12_data, password):
    # p12データを読み込み、秘密鍵、証明書、追加の証明書を取得
    private_key, cert, additional_certs = serialization.load_pkcs12_data(p12_data, password)
    
    # 秘密鍵をPEM形式でエンコード
    pem_data = private_key.private_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PrivateFormat.TraditionalOpenSSL,
        encryption_algorithm=serialization.NoEncryption()
    ) + cert.public_bytes(serialization.Encoding.PEM)  # 証明書をPEM形式で追加
    
    # 追加の証明書があれば、それらもPEM形式で追加
    for additional_cert in additional_certs:
        pem_data += additional_cert.public_bytes(serialization.Encoding.PEM)
    
    return pem_data
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?