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.

cscの作法 その217

Posted at

概要

cscの作法、調べてみた。
練習問題やってみた。

練習問題

自己署名証明書を作れ。

サンプルコード

using System;
using System.IO;
using System.Security.Cryptography;
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 System.Collections.Generic;
using System.Text;
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;

static class X509CreateTest {
	static void Main(string[] args) {
		DateTime notBefore = DateTime.Now;
		DateTime notAfter = DateTime.Now.AddYears(1);
		RSA privateKey = new RSACryptoServiceProvider(2048);
		string issuerDN = "CN=127.0.0.1";
		string serialString = "123456789";
		var x509gen = new X509V3CertificateGenerator();
		var serial = new BigInteger(serialString);
		var keyPair = DotNetUtilities.GetKeyPair(privateKey);
		x509gen.SetSerialNumber(serial);
		x509gen.SetIssuerDN(new X509Name(issuerDN));
		x509gen.SetSubjectDN(new X509Name(issuerDN));
		x509gen.SetNotBefore(notBefore);
		x509gen.SetNotAfter(notAfter);
		x509gen.SetPublicKey(keyPair.Public);
		var signerFactory = new Asn1SignatureFactory(PkcsObjectIdentifiers.Sha256WithRsaEncryption.Id, keyPair.Private);
		var cert = x509gen.Generate(signerFactory);
		using (var sw = new StreamWriter("test0.crt"))
		{
			var writer = new PemWriter(sw);
			writer.WriteObject(cert);
		}
		Console.WriteLine("OK");
	}
}

以上。

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?