#Step1 自己証明書の作成
# RSA鍵ペアの作成
$rsa = [System.Security.Cryptography.RSA]::Create()
#所有者名を指定
#所有者情報を CN=example.com,O=Example Organization,C=US として証明書を作成しています。
#CN は証明書の名前、 O は組織名、 C は国名を表します。
#所有者情報を設定することで、作成された証明書の所有者は CN=example.com,O=Example Organization,C=US となります。
$subject = "CN=example.com,O=Example Organization,C=US" # 所有者情報
# 証明書要求の作成
$request = New-Object System.Security.Cryptography.X509Certificates.CertificateRequest `
-ArgumentList @($subject, $rsa, [System.Security.Cryptography.HashAlgorithmName]::SHA256, `
[System.Security.Cryptography.RSASignaturePadding]::Pkcs1)
# 自己署名証明書の生成
$cert = $request.CreateSelfSigned([System.DateTimeOffset]::Now, [System.DateTimeOffset]::Now.AddYears(1))
# 証明書のPFX形式のエクスポート
$certData = $cert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Pfx, "password")
$currentDir = Get-Location
$Fullpath = $currentDir.ToString() + "\" + "certificate.pfx"
[System.IO.File]::WriteAllBytes($Fullpath, $certData)
#Step1 自己証明書の確認
#証明書ファイルを読み込み、証明書オブジェクトを作成
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
#X509Certificate2クラスのImportメソッドを使って、PFXファイルを証明書オブジェクトに読み込む
#引数1: 読み込むPFXファイルのフルパス
#引数2: PFXファイルのパスワード
#引数3: キーストアに追加するためのフラグを指定
$cert.Import($Fullpath, "password", [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::DefaultKeySet)
#証明書の情報を表示
$cert.Subject # 証明書に記載された主体名を表示
$cert.Issuer # 証明書に記載された発行者名を表示
$cert.SerialNumber # 証明書のシリアル番号を表示
$cert.NotBefore # 証明書の有効期限開始日を表示
$cert.NotAfter # 証明書の有効期限終了日を表示
$cert.PublicKey.Key # 証明書に関連付けられた公開鍵を表示
コメント