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 3 years have passed since last update.

cscの作法 その219

Posted at

概要

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

以上。

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?