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の作法 その226

Posted at

概要

cscの作法、調べてみた。
jwtのhashを暗号化してみた。

サンプルコード

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Operators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Prng;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;

public class test0 {
	static void Main() {
		
		Console.WriteLine("ok0");
		AsymmetricCipherKeyPair privateKey;
		using (var reader = new StreamReader(@"privatekey.pem", Encoding.ASCII))
		{
			var pemReader = new PemReader(reader);
			privateKey = (AsymmetricCipherKeyPair) pemReader.ReadObject();
		}
		Console.WriteLine("ok1");
		var rsaParams = DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters) privateKey.Private);
		RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
		rsa.ImportParameters(rsaParams);
		Console.WriteLine("ok2");
		string token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJIZWxsbyxXb3JsZCEiLCJuYW1lIjoiS29zdWtlIE1hdHN1aSIsImlhdCI6ODc1Njc0ODM5Mjg0fQ.N_zzSrKu4emL6OzpAQ7nBsR6XkV3I2TEkkrqM4Ldm7m42a78HEXsHx5X7AQQlApSPzCuIL3elXfrr4Tl2IRlstlsU6Im79V7hpWigAWnT4HyINEX74LKGeGHwve1iJpXKvTYIsjwTaKWwVGmHG2CPqVz_gVlNJJoe9PyMGzLnzZcUIYj20ATaE1NgdSoZEc9xA4T7EQWdTS4WRtSffPTREPG1Wgf0LVQZueW2P1kYsf9-_ItJTJk2GRqnzqaob-5hRANrCqWcxEr-HKw4PIftnxiVN3WqT3NWG7qT6UKfsKVNfFJZJOWcmr0UJGIpHavuvjnd-5P_9AbYchkKCucMg";
		var tokenBytes = Encoding.UTF8.GetBytes(token);
		var tokenHash = SHA256.Create().ComputeHash(tokenBytes);
		Console.WriteLine(Convert.ToBase64String(tokenHash));
		var rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
		rsaFormatter.SetHashAlgorithm("SHA256");
		var signBytes = rsaFormatter.CreateSignature(tokenHash);
		var signBase64url = Convert.ToBase64String(signBytes).TrimEnd('=').Replace('+', '-').Replace('/', '_');
		Console.WriteLine(signBase64url);
		var jwt = token + "." + signBase64url;
		//Console.WriteLine(jwt);
		Console.WriteLine("ok3");
	}
}


実行結果

ok0
ok1
ok2
oLN0oMLxb/PcMJXQ1F3lCCB9WiGw6b7QQiGj2n43NrA=
IA9bjBC8L82AmZQNEcBJv37Fp5GAk4NAXjIt-Ry8awn4xNy_Gk7cHAOsVVrT3VoV1vJOwGG0OYwabrF3gIGV3zGms0jO6rN0k0gL238GAa0dysys8mb73pd6sriyPxvbeLTApeBwAVZ88CM0sW8m1u-eH3u-CjBKBbg2oGKKOkjTR_bAHv-Ab6asOzN1gT6xTrBH6gR6g24V_DywgronJeVEIY7I4AzixoECoPf7wYOtskEuJSHbcyozt4Fl1i94eWVx5A-nMipuZ3e5LtVarAPZlo5GzLrFZHAXnbROdY44WQMa6Jqw3GHWPKUPLqvVv6IRnDyyMUMMoxrPqmIjV9pyD8FdA0-jx-1Dn1BDwuWnBhozKC5x_m5hyczhalBA3FaHm1VLz00z0mKhcRZXSP-hYnGbfa6L-2Mskh0_TVOVvV1iVT-J68BJUOMI56Au7nKz7Q90q7RtAU_9srZp_deHvPPXtovxzB29qkmGcUigJOkgCyBfEKDdsZ6uqaGTQhVEYlZ3y87tpKauWr5GtruwgrXNh5w6SGATugLhyAFATmh6IgSn0vvmemJnhNpC2icThrlX5yX2z_KcgJGBQ7zv24USf0Fv1ygUZwAPsjQDghiVgvfnpJ_iYX897ykMj-fqRCNJYz6RHfOThwkDD34YZwAWl8OIBRQcq_BPOI8
ok3

以上。

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?