protected static void The_key_is(CngAlgorithm algorithm) { // create a new key pair The_original_key = CngKey.Create(algorithm); the_data = Encoding.UTF8.GetBytes("Hello world"); // write the key pair to a stream in PEM format var ms = new MemoryStream(); new PemWriter(ms).WritePublicKey(The_original_key); ms.Seek(0, SeekOrigin.Begin); // read the key back from the stream var builder = new CngBuilder(new PemReader(ms)); The_roundtripped_key = builder.Build(); }
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); }