public static System.Security.Cryptography.CngKey Create(System.Security.Cryptography.CngAlgorithm algorithm, string keyName, System.Security.Cryptography.CngKeyCreationParameters creationParameters)
 {
     return(default(System.Security.Cryptography.CngKey));
 }
 public static System.Security.Cryptography.CngKey Create(System.Security.Cryptography.CngAlgorithm algorithm, string keyName, System.Security.Cryptography.CngKeyCreationParameters creationParameters)
 {
     throw null;
 }
예제 #3
0
        public static void GenerateEcdsaKey()
        {
            byte[] myhash = null;

            using (System.Security.Cryptography.SHA256 sha = System.Security.Cryptography.SHA256.Create())
            {
                myhash = sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes("Hello world"));
            } // End Using sha


            // Assembly: System.Core
            System.Security.Cryptography.CngKeyCreationParameters keyCreationParameters = new System.Security.Cryptography.CngKeyCreationParameters();
            // keyCreationParameters.ExportPolicy = System.Security.Cryptography.CngExportPolicies.AllowExport;
            keyCreationParameters.ExportPolicy = System.Security.Cryptography.CngExportPolicies.AllowPlaintextExport;


            keyCreationParameters.KeyUsage = System.Security.Cryptography.CngKeyUsages.Signing;


            System.Security.Cryptography.CngKey key =
                System.Security.Cryptography.CngKey.Create(
                    System.Security.Cryptography.CngAlgorithm.ECDsaP256, null, keyCreationParameters
                    );


            byte[] publicKeyBytes  = null;
            byte[] privateKeyBytes = null;

            using (System.Security.Cryptography.ECDsaCng dsa = new System.Security.Cryptography.ECDsaCng(key))
            {
                publicKeyBytes  = dsa.Key.Export(System.Security.Cryptography.CngKeyBlobFormat.EccPublicBlob);
                privateKeyBytes = dsa.Key.Export(System.Security.Cryptography.CngKeyBlobFormat.EccPrivateBlob);

                // http://stackoverflow.com/questions/34618755/verify-bouncycastle-ecdsa-signature-with-net-libraries-ecdsacng
                // string xmlExport = dsa.ToXmlString(true); // Include PK in export: not implemented exception...
                string xmlExport = dsa.ToXmlString(System.Security.Cryptography.ECKeyXmlFormat.Rfc4050);

                System.Console.WriteLine(xmlExport);
            } // End Using dsa


            byte[] mysignature = null;

            System.Security.Cryptography.CngKey privateKey = System.Security.Cryptography.CngKey.Import(privateKeyBytes, System.Security.Cryptography.CngKeyBlobFormat.EccPrivateBlob);
            using (System.Security.Cryptography.ECDsaCng dsa2 = new System.Security.Cryptography.ECDsaCng(privateKey))
            {
                // http://stackoverflow.com/questions/34618755/verify-bouncycastle-ecdsa-signature-with-net-libraries-ecdsacng
                // dsa2.FromXmlString("");
                // dsa.HashAlgorithm = CngAlgorithm.Sha256;
                //byte[] signature = dsa.SignData

                mysignature = dsa2.SignHash(myhash);           // Requires private key
                bool b = dsa2.VerifyHash(myhash, mysignature); // Verifying can be done with publicKey or privateKey, signing only with privateKey
                System.Console.WriteLine(b);
            } // End Using dsa2


            System.Security.Cryptography.CngKey publicKey = System.Security.Cryptography.CngKey.Import(publicKeyBytes, System.Security.Cryptography.CngKeyBlobFormat.EccPublicBlob);
            using (System.Security.Cryptography.ECDsaCng dsa3 = new System.Security.Cryptography.ECDsaCng(publicKey))
            {
                bool b = dsa3.VerifyHash(myhash, mysignature); // Verifying can be done with publicKey or privateKey, signing only with privateKey
                System.Console.WriteLine(b);
            } // End Using dsa3
        }