コード例 #1
0
        public static bool VerifySignature(byte[] message, byte[] signature, byte[] pubkey)
        {
            var PublicKey = ThinNeo.Cryptography.ECC.ECPoint.DecodePoint(pubkey, ThinNeo.Cryptography.ECC.ECCurve.Secp256r1);
            var usepk     = PublicKey.EncodePoint(false).Skip(1).ToArray();

            byte[] first = { 0x45, 0x43, 0x53, 0x31, 0x20, 0x00, 0x00, 0x00 };
            usepk = first.Concat(usepk).ToArray();

            using (System.Security.Cryptography.CngKey key = System.Security.Cryptography.CngKey.Import(usepk, System.Security.Cryptography.CngKeyBlobFormat.EccPublicBlob))
                using (System.Security.Cryptography.ECDsaCng ecdsa = new System.Security.Cryptography.ECDsaCng(key))

                //using (var ecdsa = System.Security.Cryptography.ECDsa.Create(new System.Security.Cryptography.ECParameters
                //{
                //    Curve = System.Security.Cryptography.ECCurve.NamedCurves.nistP256,
                //    Q = new System.Security.Cryptography.ECPoint
                //    {
                //        X = usepk.Take(32).ToArray(),
                //        Y = usepk.Skip(32).ToArray()
                //    }
                //}))
                {
                    var hash = sha256.ComputeHash(message);
                    return(ecdsa.VerifyHash(hash, signature));
                }
        }
コード例 #2
0
        public static byte[] Sign(byte[] message, byte[] prikey)
        {
            var Secp256r1_G = NEO.AllianceOfThinWallet.Cryptography.Helper.HexString2Bytes("04" + "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296" + "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");

            var PublicKey = ECC.ECCurve.Secp256r1.G * prikey;
            var pubkey    = PublicKey.EncodePoint(false).Skip(1).ToArray();
            //#if NET461
            const int ECDSA_PRIVATE_P256_MAGIC = 0x32534345;

            prikey = BitConverter.GetBytes(ECDSA_PRIVATE_P256_MAGIC).Concat(BitConverter.GetBytes(32)).Concat(pubkey).Concat(prikey).ToArray();
            using (System.Security.Cryptography.CngKey key = System.Security.Cryptography.CngKey.Import(prikey, System.Security.Cryptography.CngKeyBlobFormat.EccPrivateBlob))
                using (System.Security.Cryptography.ECDsaCng ecdsa = new System.Security.Cryptography.ECDsaCng(key))
                //#else
                //            using (var ecdsa = System.Security.Cryptography.ECDsa.Create(new System.Security.Cryptography.ECParameters
                //            {
                //                Curve = System.Security.Cryptography.ECCurve.NamedCurves.nistP256,
                //                D = prikey,
                //                Q = new System.Security.Cryptography.ECPoint
                //                {
                //                    X = pubkey.Take(32).ToArray(),
                //                    Y = pubkey.Skip(32).ToArray()
                //                }
                //            }))
                //#endif
                {
                    return(ecdsa.SignData(message, System.Security.Cryptography.HashAlgorithmName.SHA256));
                }
        }
コード例 #3
0
        public static byte[] Sign(byte[] message, byte[] prikey)
        {
            var Secp256r1_G = Helper.HexString2Bytes("04" + "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296" + "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");

            var PublicKey = ThinNeo.Cryptography.ECC.ECCurve.Secp256r1.G * prikey;
            var pubkey    = PublicKey.EncodePoint(false).Skip(1).ToArray();

            //#if NET461
            //const int ECDSA_PRIVATE_P256_MAGIC = 0x32534345;
            byte[] first = { 0x45, 0x43, 0x53, 0x32, 0x20, 0x00, 0x00, 0x00 };
            prikey = first.Concat(pubkey).Concat(prikey).ToArray();
            using (System.Security.Cryptography.CngKey key = System.Security.Cryptography.CngKey.Import(prikey, System.Security.Cryptography.CngKeyBlobFormat.EccPrivateBlob))
                using (System.Security.Cryptography.ECDsaCng ecdsa = new System.Security.Cryptography.ECDsaCng(key))

                //using (var ecdsa = System.Security.Cryptography.ECDsa.Create(new System.Security.Cryptography.ECParameters
                //{
                //    Curve = System.Security.Cryptography.ECCurve.NamedCurves.nistP256,
                //    D = prikey,
                //    Q = new System.Security.Cryptography.ECPoint
                //    {
                //        X = pubkey.Take(32).ToArray(),
                //        Y = pubkey.Skip(32).ToArray()
                //    }
                //}))
                {
                    var hash = sha256.ComputeHash(message);
                    return(ecdsa.SignHash(hash));
                }
        }
コード例 #4
0
 public RSACng(System.Security.Cryptography.CngKey key)
 {
 }
コード例 #5
0
 public ECDsaCng(System.Security.Cryptography.CngKey key)
 {
 }
 public Microsoft.Win32.SafeHandles.SafeNCryptSecretHandle DeriveSecretAgreementHandle(System.Security.Cryptography.CngKey otherPartyPublicKey)
 {
     throw null;
 }
 public byte[] DeriveKeyMaterial(System.Security.Cryptography.CngKey otherPartyPublicKey)
 {
     throw null;
 }
コード例 #8
0
 public ECDiffieHellmanCng(System.Security.Cryptography.CngKey key)
 {
 }
コード例 #9
0
        static void Main(string[] args)
        {
            //neo 私钥
            string neoprivate = "f97b7434ffa981eb809ea6795e0cb3046850a65be2f9f67b92053f6f2838adca";

            Console.WriteLine("Neo 私钥=" + neoprivate);
            Console.WriteLine("    Neo私钥是预先生成的");

            //neo公钥
            byte[] prikey    = HexString2Bytes(neoprivate);
            var    PublicKey = NEO.AllianceOfThinWallet.Cryptography.ECC.ECCurve.Secp256r1.G * prikey;

            byte[] pubkey = PublicKey.EncodePoint(false).Skip(1).ToArray();
            Console.WriteLine("Neo 公钥=" + Bytes2HexString(pubkey));
            Console.WriteLine("    Neo公钥是用Neo私钥计算出来的");

            //签名的私钥
            byte[] first      = { 0x45, 0x43, 0x53, 0x32, 0x20, 0x00, 0x00, 0x00 };
            byte[] signprikey = first.Concat(pubkey).Concat(prikey).ToArray();
            Console.WriteLine("签名算法用的私钥=" + Bytes2HexString(signprikey));
            Console.WriteLine("    签名算法用的私钥 是 用 neo 私钥 和 heo公钥 组合出来的");


            Console.WriteLine("");

            //随机数据
            Random r = new Random();

            byte[] srcdata = new byte[65536];
            for (var i = 0; i < srcdata.Length; i++)
            {
                srcdata[i] = (byte)(r.Next() % 256);
            }
            Console.WriteLine("填充64k数据用于签名");

            //签名
            byte[] signdata = null;

            using (System.Security.Cryptography.CngKey key = System.Security.Cryptography.CngKey.Import(signprikey, System.Security.Cryptography.CngKeyBlobFormat.EccPrivateBlob))
            {
                using (System.Security.Cryptography.ECDsaCng ecdsa = new System.Security.Cryptography.ECDsaCng(key))
                {
                    var hashsrc = sha256.ComputeHash(srcdata);
                    signdata = ecdsa.SignHash(hashsrc);
                }
            }
            Console.WriteLine("签名结果:" + Bytes2HexString(signdata));



            Console.WriteLine("");
            byte[] first2     = { 0x45, 0x43, 0x53, 0x31, 0x20, 0x00, 0x00, 0x00 };
            byte[] signpubkey = first2.Concat(pubkey).ToArray();
            Console.WriteLine("验证签名公钥=" + Bytes2HexString(signpubkey));
            Console.WriteLine("    验证签名公钥是用NEO公钥计算出来的");
            using (System.Security.Cryptography.CngKey key = System.Security.Cryptography.CngKey.Import(signpubkey, System.Security.Cryptography.CngKeyBlobFormat.EccPublicBlob))
            {
                using (System.Security.Cryptography.ECDsaCng ecdsa = new System.Security.Cryptography.ECDsaCng(key))
                {
                    Console.WriteLine("验证签名结果=" + ecdsa.VerifyData(srcdata, signdata));

                    var hashsrc = sha256.ComputeHash(srcdata);
                    Console.WriteLine("验证签名结果2=" + ecdsa.VerifyHash(hashsrc, signdata));
                }
            }
            Console.WriteLine("回车继续");
            Console.ReadLine();
        }
コード例 #10
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
        }