/// <summary> /// 公開鍵と秘密鍵を作成して返す /// </summary> /// <param name="publicKey">作成された公開鍵</param> /// <param name="privateKey">作成された秘密鍵</param> public static void CreateKeys(out byte[] publicKey, out byte[] privateKey) { #if Mono throw new NotSupportedException(); #else CngKeyCreationParameters ckcp = new CngKeyCreationParameters(); ckcp.ExportPolicy = CngExportPolicies.AllowPlaintextExport; ckcp.KeyUsage = CngKeyUsages.Signing; using (CngKey ck = CngKey.Create(CngAlgorithm.ECDsaP521, null, ckcp)) using (ECDsaCng ecdsa = new ECDsaCng(ck)) { publicKey = Encoding.ASCII.GetBytes(ecdsa.ToXmlString(ECKeyXmlFormat.Rfc4050)); privateKey = ecdsa.Key.Export(CngKeyBlobFormat.Pkcs8PrivateBlob); } #endif }
public ECDsaCng LoadRemoteKey(JsonWebTokenHeader header) { var dsa = new ECDsaCng(); var cached = _cache.GetPublicKeyBytes(header.KeyUri.ToString(), header.KeyId); if (null != cached) { dsa.FromXmlString(Encoding.UTF8.GetString(cached), ECKeyXmlFormat.Rfc4050); return(dsa); } string data; using (var wc = new WebClient()) { try { data = wc.DownloadString(header.KeyUri); } catch (WebException e) { throw new RemoteKeyInaccessibleException("Unable to download the public key from URI " + header.KeyUri, e); } } switch (header.KeyFormat) { case KeyFormat.Rfc4050: dsa.FromXmlString(data, ECKeyXmlFormat.Rfc4050); _cache.Cache(Encoding.UTF8.GetBytes(data), header.KeyId, header.KeyUri.ToString()); return(dsa); case KeyFormat.X509: var ms = new MemoryStream(Encoding.ASCII.GetBytes(data)); var reader = new CngBuilder(new PemReader(ms)); dsa = new ECDsaCng(reader.Build()); _cache.Cache(Encoding.UTF8.GetBytes(dsa.ToXmlString(ECKeyXmlFormat.Rfc4050)), header.KeyId, header.KeyUri.ToString()); return(dsa); } throw new NotSupportedException("Can not open an ECC key with the keyformat " + header.KeyFormat); }
private static void Main(string[] args) { string hexPublic = "45435333300000007617E192615E8C24D353E8BD11DE21E9C54DC2D9D64C21AEC35A372B0EB3C205597BA20C0944FA6AFE871D23076F1D9711B5AAE0817BEAA6B953E9DF186B6BE1048050759005396B0A2856CF464C2E927916B0958CACEF8132A79C62456C8421"; string hexPrivate = "45435334300000007617E192615E8C24D353E8BD11DE21E9C54DC2D9D64C21AEC35A372B0EB3C205597BA20C0944FA6AFE871D23076F1D9711B5AAE0817BEAA6B953E9DF186B6BE1048050759005396B0A2856CF464C2E927916B0958CACEF8132A79C62456C842115772EBB4C4A3078DE768FCE8A8156380A6047E160BCFCAD24FB70416CFCCE0E693961D7C0F1198BA8B25CC2A6EF4C6D"; hexPublic = File.ReadAllLines(@"ServerCert\ECDSA.public")[0]; hexPrivate = File.ReadAllLines(@"ServerCert\ECDSAPrivate.key")[0]; string msg0 = "poruka"; byte[] data = Encoding.ASCII.GetBytes(msg0); var bytesKey = Enumerable.Range(0, hexPrivate.Length).Where(x => x % 2 == 0).Select(x => Convert.ToByte(hexPrivate.Substring(x, 2), 16)).ToArray(); CngKey key = CngKey.Import(bytesKey, CngKeyBlobFormat.EccPrivateBlob); /* * CngKey key = CngKey.Create(CngAlgorithm.ECDsaP384, null, new CngKeyCreationParameters { ExportPolicy = CngExportPolicies.AllowPlaintextExport }); * * var pubK = key.Export(CngKeyBlobFormat.EccPublicBlob); * var privK = key.Export(CngKeyBlobFormat.EccPrivateBlob); * * var hexpub = BitConverter.ToString(pubK).Replace("-", ""); * var hexpriv = BitConverter.ToString(privK).Replace("-", ""); */ ECDsaCng dsa = new ECDsaCng(key); String xmlExport = dsa.ToXmlString(ECKeyXmlFormat.Rfc4050); byte[] signature = dsa.SignData(data); /******************************/ var bytesKey2 = Enumerable.Range(0, hexPublic.Length).Where(x => x % 2 == 0).Select(x => Convert.ToByte(hexPublic.Substring(x, 2), 16)).ToArray(); CngKey key2 = CngKey.Import(bytesKey2, CngKeyBlobFormat.EccPublicBlob); ECDsaCng eccImporter = new ECDsaCng(key2); //eccImporter.FromXmlString(xmlExport, ECKeyXmlFormat.Rfc4050); if (eccImporter.VerifyData(data, signature)) { Console.WriteLine("Verified using .NET"); } #region early testing /* * * string msg = "The quick brown fox jumps over the lazy dog"; * string msg2 = "1vo je test"; * var a = Encoding.ASCII.GetBytes(msg); * var b = Encoding.ASCII.GetBytes(msg2); * var keey = makeGostKey(); * * var e1 = Compute_gost28147(a, keey); * var e2 = Compute_gost28147(b, keey); * * AsymmetricCipherKeyPair keyPair = ecdh_sha2_nistp521.getKeyPair(); * var o = keyPair.Public as ECPublicKeyParameters; * var ui = o.PublicKeyParamSet; * var senderPrivate = ((ECPrivateKeyParameters)keyPair.Private).D.ToByteArrayUnsigned(); * var senderPublic = ((ECPublicKeyParameters)keyPair.Public).Q.GetEncoded(); * * var p = (ECPrivateKeyParameters)PrivateKeyFactory.CreateKey(senderPrivate); * var pub = ((ECPublicKeyParameters)keyPair.Public).Q.ToString(); * var pub2 = BitConverter.ToString(senderPublic).Replace("-", "").ToLower(); * * var par = GenerateParameters(); * Debug.WriteLine(par.P.ToString()); * Debug.WriteLine(par.G.ToString()); * * TestBouncy(par); * //TestMethod(); * * Console.WriteLine("Server start call!"); * StartServer(); * * Thread.Sleep(1500); * Console.WriteLine("Klijent start call!"); * StartKlijent("pa kako je, ša ima?"); * */ #endregion early testing Console.ReadKey(); }