概要
cscの作法、調べてみた。
BouncyCastle.dll使ってみた。
キーストア(p12)を叩いてみた。
サンプルコード
using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Net.Security;
using System.Security.Authentication;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.IO;
using Org.BouncyCastle.X509;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Crypto.Operators;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Prng;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Pkcs;
namespace app
{
public class test0 {
public static int Main(string[] args) {
string pfxPath = "test5.p12";
if (args == null ||args.Length < 1)
{
Console.WriteLine("p2 .p12");
}
pfxPath = args[0];
FileStream fs = File.OpenRead(pfxPath);
char[] passwd = "password".ToCharArray();
Pkcs12Store store = new Pkcs12StoreBuilder().Build();
store.Load(fs, passwd);
string alias = null;
foreach (string str in store.Aliases)
{
if (store.IsKeyEntry(str))
alias = str;
}
if (alias == null)
{
Console.WriteLine("alias is empty");
}
else
Console.WriteLine(alias);
AsymmetricKeyEntry keyEntry = store.GetKey(alias);
RsaKeyParameters priKey = (RsaKeyParameters) keyEntry.Key;
X509CertificateEntry certEntry = store.GetCertificate(alias);
Org.BouncyCastle.X509.X509Certificate x509cert = certEntry.Certificate;
RsaKeyParameters pubKey = (RsaKeyParameters) x509cert.GetPublicKey();
x509cert.Verify(pubKey);
byte[] certByte = x509cert.GetEncoded();
X509Certificate2 cert2 = new X509Certificate2(certByte);
Console.WriteLine(cert2.ToString());
return 0;
}
}
}
実行結果
c>p2 pkcs12.p12
localhost
[Subject]
CN=localhost
[Issuer]
CN=localhost
[Serial Number]
562BE42564875E964A04E8468651092B631782B82B7EF0E5D14249E7B28D1D01
[Not Before]
2022/06/24 9:00:00
[Not After]
2023/06/24 9:00:00
[Thumbprint]
6A9E328D46074BBCBCE93EE72576D7434161D242
以上。