LoginSignup
0
0

More than 1 year has passed since last update.

C# でタイムスタンプリクエストを作成する

Posted at

C#でタイムスタンプリクエストを作成するには、Rfc3161TimestampRequest クラスを利用します。

// See https://aka.ms/new-console-template for more information

using System.Security.Cryptography;
using System.Security.Cryptography.Pkcs;
using System.Text;

// 文字列
string str = "Hello World";
byte[] beforeByteArray = Encoding.UTF8.GetBytes(str);


//タイムスタンプリクエストの作成
Rfc3161TimestampRequest req = Rfc3161TimestampRequest.CreateFromData(beforeByteArray, HashAlgorithmName.SHA256, null, null, true);
byte[] reqByteArray = req.Encode();

// バイト配列を16進数文字列に変換、16進文字列で標準出力に出力
StringBuilder sb = new StringBuilder();
foreach (byte b in reqByteArray)
{
    sb.Append(b.ToString("x2"));
}
Console.WriteLine(sb);


// ファイルに出力
string fileName = @"HelloWorld.req";

using (var writer = new BinaryWriter(new FileStream(fileName, FileMode.Create)))
{
    //書き込む処理
    writer.Write(reqByteArray);
}

作成したタイムスタンプリクエストを以下のOpenSSLのコマンドで表示してみました。

>openssl ts -query -in HelloWorld.req -text
Using configuration from C:\Program Files\Common Files\SSL/openssl.cnf
Version: 1
Hash Algorithm: sha256
Message data:
    0000 - a5 91 a6 d4 0b f4 20 40-4a 01 17 33 cf b7 b1 90   ...... @J..3....
    0010 - d6 2c 65 bf 0b cd a3 2b-57 b2 77 d9 ad 9f 14 6e   .,e....+W.w....n
Policy OID: unspecified
Nonce: unspecified
Certificate required: yes
Extensions:

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