private static byte[] CreateSignature(byte[] data, CngKey key)
        {
            ECDsaCng signingAlgo = new ECDsaCng(key);

            byte[] signature = signingAlgo.SignData(data);
            signingAlgo.Clear();

            return(signature);
        }
示例#2
0
        static byte[] CreateSignature(byte[] data, CngKey key)
        {
            byte[] signature;
            var    signingAlg = new ECDsaCng(key);

            signature = signingAlg.SignData(data);
            signingAlg.Clear();
            return(signature);
        }
示例#3
0
 public byte[] CreateSignature(byte[] data, CngKey key)
 {
     byte[] signature;
     using (var signingAlg = new ECDsaCng(key))
     {
         signature = signingAlg.SignData(data, HashAlgorithmName.SHA512);
         signingAlg.Clear();
     }
     return(signature);
 }
示例#4
0
 public static byte[] CreateSignature(byte[] data, CngKey key)
 {
     // 创建签名
     byte[] signature;
     using (var signingAlg = new ECDsaCng(key))
     {
         signature = signingAlg.SignData(data);
         signingAlg.Clear();
     }
     return(signature);
 }
示例#5
0
 private void удалениеПарыКлючейToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (MessageBox.Show("Будет удалена пара ключей авторизованного пользователя.\nБудет произведена деавторизация.\nТекстовое поле будет очищено. Продолжить?", "Предупреждение", MessageBoxButtons.OKCancel) == DialogResult.OK)
     {
         Visible = false;
         DeleteDirectory(PK_store_path + "\\" + current_user);
         ecdsaCng.Key.Delete();
         ecdsaCng.Clear();
         Auth();
         Visible = true;
     }
 }
示例#6
0
        static bool VerifySignature(byte[] data, byte[] signature, byte[] pubKey)
        {
            bool retValue = false;

            using (CngKey key = CngKey.Import(pubKey, CngKeyBlobFormat.GenericPublicBlob))
                using (var signingAlg = new ECDsaCng(key))
                {
                    retValue = signingAlg.VerifyData(data, signature);
                    signingAlg.Clear();
                }
            return(retValue);
        }
示例#7
0
        /// <summary>
        /// 使用私钥签名
        /// </summary>
        public static byte[] SignData(byte[] data, string keyName)
        {
            // 打开密钥
            CngKey cngKey = CngKey.Open(keyName);
            // 签名
            ECDsaCng ecdsa = new ECDsaCng(cngKey);

            byte[] signature = ecdsa.SignData(data);
            ecdsa.Clear();
            cngKey.Dispose();
            return(signature);
        }
示例#8
0
        private bool VerifySignaure(CngKey key, byte[] data, byte[] signature, byte[] pubkey)
        {
            bool retValue = false;

            using (ECDsaCng signingAlg = new ECDsaCng(key))
            {
                retValue = signingAlg.VerifyData(data, signature);
                signingAlg.Clear();
            }

            return(retValue);
        }
 //创建签名
 private static byte[] CreateSignature(byte[] data, CngKey key)
 {
     byte[] signature;
     //key是CngKey类型,包含了公钥和私钥
     //将key传入ECDsaCng得到加密算法
     using (var signingAlg = new ECDsaCng(key))
     {
         //使用 signingAlg.SignData 对 要签名的内容以及私钥进行签名
         signature = signingAlg.SignData(data, HashAlgorithmName.SHA512);
         signingAlg.Clear();
     }
     return(signature);
 }
示例#10
0
        /// <summary>
        /// 使用公钥验证签名
        /// </summary>
        public static bool VerifyData(byte[] data, byte[] signature, byte[] publicKey)
        {
            bool verified = false;
            // 导入公钥
            CngKey cngKey = CngKey.Import(publicKey, CngKeyBlobFormat.EccPublicBlob);
            // 验证签名
            ECDsaCng ecdsa = new ECDsaCng(cngKey);

            verified = ecdsa.VerifyData(data, signature);
            ecdsa.Clear();
            cngKey.Dispose();
            return(verified);
        }
示例#11
0
        private byte[] CreateSignature(byte[] data, CngKey key)
        {
            /*Имея в распоряжении пару ключей, отправитель может создать подпись с помощью класса ECDsaCng.
             * Конструктор этого класса принимает объект CngKey, в котором содержится открытый и секретный ключи.
             * Далее этот секретный ключ используется для подписания данных вызовом метода SignData()
             */
            byte[] signature;
            var    signingAlg = new ECDsaCng(key);

            signature = signingAlg.SignData(data);
            signingAlg.Clear();
            return(signature);
        }
示例#12
0
        private byte[] CreateSignature(byte[] data, CngKey key)
        {
            byte[] signature;
            using (var signingAlg = new ECDsaCng(key))
            {
#if NET46
                signature = signingAlg.SignData(data);
                signingAlg.Clear();
#else
                signature = signingAlg.SignData(data, HashAlgorithmName.SHA512);
#endif
            }
            return(signature);
        }
        /// <summary>
        /// 有了密钥对,Alice就可以使用ECDsaCng类创建签名了。
        /// 这个类的构造函数从Alice那里接收包含公钥和私钥的CngKey类。
        /// 再使用私钥,通过SignData0方法给数据签名。SignData()方法在.NET core中略有不同。.NETcore需要如下算法
        /// </summary>
        /// <param name="aliceData"></param>
        /// <param name="_aliceKeySignature"></param>
        /// <returns></returns>
        private static byte[] CreateSignature(byte[] aliceData, CngKey _aliceKeySignature)
        {
            byte[] signature;
            using (var signingAlg = new ECDsaCng(_aliceKeySignature))
            {
#if NET46
                signature = signingAlg.SignData(aliceData);
                signingAlg.Clear();
#else
                signature = signingAlg.SignData(aliceData, HashAlgorithmName.SHA512);
#endif
            }
            return(signature);
        }
示例#14
0
        public bool VerifySignature(byte[] data, byte[] signature, byte[] pubKeys)
        {
            bool retValue = false;

            using (CngKey key = CngKey.Import(pubKeys, CngKeyBlobFormat.GenericPrivateBlob))
            {
                using (var signungAlg = new ECDsaCng(key))
                {
                    retValue = signungAlg.VerifyData(data, signature, HashAlgorithmName.SHA512);
                    signungAlg.Clear();
                }
                return(retValue);
            }
        }
        //验证签名
        private static bool VerifySignature(byte[] data, byte[] signature, byte[] pubKey)
        {
            bool retValue = false;

            //使用 CngKey.Import 导入CngKey对象,的到公钥
            using (CngKey key = CngKey.Import(pubKey, CngKeyBlobFormat.GenericPublicBlob))
                //将公钥传入ECDsaCng得到解密签名算法
                using (var signinAlg = new ECDsaCng(key))
                {
                    //然后使用signinAlg.VerifyData来验证签名
                    retValue = signinAlg.VerifyData(data, signature, HashAlgorithmName.SHA512);
                    signinAlg.Clear();
                }
            return(retValue);
        }
示例#16
0
        public bool VerifySignature(string password, byte[] signature, byte[] salt)
        {
            bool retValue = false;

            using (CngKey key = CngKey.Import(salt, CngKeyBlobFormat.GenericPublicBlob))
                using (var signingAlg = new ECDsaCng(key))
                {
                    //#if NET46
                    retValue = signingAlg.VerifyData(Encoding.UTF8.GetBytes(password), signature);
                    signingAlg.Clear();
                    //#else
                    //    retValue = signingAlg.VerifyData(data, signature, HashAlgorithmName.SHA512);
                    //#endif
                }
            return(retValue);
        }
        /// <summary>
        /// 要验证签名是否真的来自于Alice,Bob使用Alice的公钥检查签名。包含公钥blob的字节数组可以用静态方法Import()导入CngKey对象。
        /// 然后使用ECDsaCng类,调用VerifyData()方法来验证签名。
        /// </summary>
        /// <param name="aliceData"></param>
        /// <param name="aliceSignature"></param>
        /// <param name="_alicePublicKeyBob"></param>
        /// <returns></returns>
        private static bool VerifySignature(byte[] data, byte[] signature, byte[] pubKey)
        {
            bool retValue = false;

            using (CngKey key = CngKey.Import(pubKey, CngKeyBlobFormat.GenericPublicBlob))
                using (var signingAlg = new ECDsaCng(key))
                {
#if NET46
                    retValue = signingAlg.VerifyData(data, signature);
                    signingAlg.Clear();
#else
                    retValue = signingAlg.VerifyData(data, signature, HashAlgorithmName.SHA512);
#endif
                }
            return(retValue);
        }
示例#18
0
        /// <summary>
        /// 创建签名
        /// </summary>
        /// <param name="data"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        private byte[] CreateSignature(byte[] data, CngKey key)
        {
            byte[] signature;
            //使用ECDsaCng创建签名,ECDsaCng的构造函数接收包含公钥和私钥的CngKey类对象
            using (ECDsaCng signingAlg = new ECDsaCng(key))
            {
#if NET46
                signature = signingAlg.SignData(data);
                signingAlg.Clear();
#else
                //对数据进行签名(加密)
                signature = signingAlg.SignData(data, HashAlgorithmName.SHA512);
#endif
            }
            return(signature);
        }
示例#19
0
        private bool VerifySignature(byte[] data, byte[] signature, byte[] pubKey)
        {
            /*Для проверки, действительно ли подпись принадлежит отправителю, получатель извлекает ее с применением полученного
             * от отправителя открытого ключа. Для этого сначала массив байтов, содержащий этот открытый ключ, импортируется
             * в объект CngKey с помощью статического метода Import(), а затем для верификации подписи вызывается метод VerifyData()
             * класса ECDsaCng
             */
            bool retValue = false;

            using (CngKey key = CngKey.Import(pubKey, CngKeyBlobFormat.GenericPublicBlob))
            {
                var signingAlg = new ECDsaCng(key);
                retValue = signingAlg.VerifyData(data, signature);
                signingAlg.Clear();
            }
            return(retValue);
        }
示例#20
0
文件: ECDsa.cs 项目: abtree/CShapBase
 static byte[] CreateSignature(byte[] data, CngKey key)
 {
     byte[] signature;
     //这里直接用 CngKey 加密
     //using(var signingAlg = new ECDsaCng(key))
     //{
     //    signature = signingAlg.SignData(data);
     //    signingAlg.Clear();
     //}
     //这里用私钥重新生成CngKey对象
     using (CngKey pKey = CngKey.Import(aliceSecKeyBlob, CngKeyBlobFormat.GenericPrivateBlob))
     {
         using (var signingAlg = new ECDsaCng(pKey))
         {
             signature = signingAlg.SignData(data);
             signingAlg.Clear();
         }
     }
     return(signature);
 }
示例#21
0
        public void UseEncrpyting()
        {
            // Init key
            CngKey key = CngKey.Create(CngAlgorithm.ECDsaP521);

            byte[] pubKeyBlob = key.Export(CngKeyBlobFormat.GenericPublicBlob);

            byte[] myData = Encoding.UTF8.GetBytes("Hello there");
            using (var signingAlgorithm = new ECDsaCng(key))
            {
                byte[] signature = signingAlgorithm.SignData(myData, HashAlgorithmName.SHA512);
                Console.WriteLine(Encoding.UTF8.GetString(signature));

                using (CngKey k = CngKey.Import(pubKeyBlob, CngKeyBlobFormat.GenericPublicBlob))
                {
                    bool ret = signingAlgorithm.VerifyData(myData, signature, HashAlgorithmName.SHA512);
                    Console.WriteLine($"Encoding success={ret}");
                }
                signingAlgorithm.Clear();
            }
        }