Exemple #1
0
        /// <summary>
        /// 签名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSign_Click(object sender, EventArgs e)
        {
            btnSign.Enabled = false;
            try
            {
                var signData = "";
                if (cmbProviderType.Text == "哈希加密")
                {
                    var hashingProviderType = cmbEncryptType.Text;
                    var hashingProvider     = CryptoFactory.CreateHashing(hashingProviderType);
                    signData = hashingProvider.Signature(txtPost.Text, txtSecretKey.Text);
                }
                else //非对称加密
                {
                    var asymmetricProviderType = cmbEncryptType.Text;
                    var asymmetricProvide      = CryptoFactory.CreateAsymmetric(asymmetricProviderType);
                    asymmetricProvide.OutType = Vive.Crypto.Core.OutType.Hex;
                    signData = asymmetricProvide.SignData(txtPost.Text, txtprivateKey.Text);
                }
                txtResponse.Text = signData;
                //txtResponse.Text = "验名成功";

                INIHelper.Write(cmbEncryptType.Text + "_SignData.dat", signData);
                saveData();
            }
            catch (Exception ex) { txtResponse.Text = ex.ToString(); }
            btnSign.Enabled = true;
        }
Exemple #2
0
        /// <summary>
        /// 获取公钥 私钥
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGenerateKeyPair_Click(object sender, EventArgs e)
        {
            btnGenerateKeyPair.Enabled = false;
            try
            {
                if (cmbProviderType.Text == "对称加密")
                {
                    var symmetricProviderType = cmbEncryptType.Text.Split('-')[0];
                    var symmetricProvider     = CryptoFactory.CreateSymmetric(symmetricProviderType);

                    var keypair = symmetricProvider.CreateKey();

                    txtSecretKey.Text = keypair.Key;
                    if (txtprivateKey.Enabled)
                    {
                        txtprivateKey.Text = keypair.IV;
                    }
                    else
                    {
                        txtprivateKey.Text = "";
                    }
                }
                else if (cmbProviderType.Text == "哈希加密")
                {
                    if (cmbEncryptType.Text.StartsWith("HMAC"))
                    {
                        txtSecretKey.Text = Vive.Crypto.Core.Internals.RandomStringGenerator.Generate(32);
                    }
                    else
                    {
                        txtSecretKey.Text = "";
                    }
                    txtprivateKey.Text = "";
                }
                else //非对称加密
                {
                    var asymmetricProviderType = cmbEncryptType.Text;
                    var asymmetricProvide      = CryptoFactory.CreateAsymmetric(asymmetricProviderType);
                    var keypair = asymmetricProvide.CreateKey();

                    txtSecretKey.Text  = keypair.PublickKey;
                    txtprivateKey.Text = keypair.PrivateKey;
                }

                saveData();
            }
            catch (Exception ex) { txtResponse.Text = ex.ToString(); }
            btnGenerateKeyPair.Enabled = true;
        }
Exemple #3
0
        private string Decrypt(String cipherText)
        {
            var response = "";

            try
            {
                if (cmbProviderType.Text == "对称加密")
                {
                    var symmetricProviderType = cmbEncryptType.Text.Split('-')[0];
                    var symmetricProvider     = CryptoFactory.CreateSymmetric(symmetricProviderType);
                    response = symmetricProvider.Decrypt(cipherText, txtSecretKey.Text, (cmbEncryptType.Text.Contains("ECB") || txtprivateKey.Text.IsNullOrEmpty() ? null : txtprivateKey.Text));
                }
                else //非对称加密
                {
                    var asymmetricProviderType = cmbEncryptType.Text;
                    var asymmetricProvide      = CryptoFactory.CreateAsymmetric(asymmetricProviderType);
                    response = asymmetricProvide.Decrypt(cipherText, txtprivateKey.Text);
                }
            }
            catch (Exception ex) { txtResponse.Text = ex.ToString(); }
            return(response);
        }
Exemple #4
0
 /// <summary>
 /// 验签
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btnVerify_Click(object sender, EventArgs e)
 {
     btnSign.Enabled = false;
     try
     {
         var signData = INIHelper.Read(cmbEncryptType.Text + "_SignData.dat");
         if (signData.IsNullOrEmpty())
         {
             txtResponse.Text = "无验名信息,请先签名";
             return;
         }
         var ret = false;
         if (cmbProviderType.Text == "哈希加密")
         {
             var hashingProviderType = cmbEncryptType.Text;
             var hashingProvider     = CryptoFactory.CreateHashing(hashingProviderType);
             ret = hashingProvider.Verify(signData, txtPost.Text, txtSecretKey.Text);
         }
         else //非对称加密
         {
             var asymmetricProviderType = cmbEncryptType.Text;
             var asymmetricProvide      = CryptoFactory.CreateAsymmetric(asymmetricProviderType);
             asymmetricProvide.OutType = Vive.Crypto.Core.OutType.Hex;
             ret = asymmetricProvide.VerifyData(txtPost.Text, signData, txtSecretKey.Text);
         }
         if (ret)
         {
             txtResponse.Text = "验签成功";
         }
         else
         {
             txtResponse.Text = "验签失败";
         }
     }
     catch (Exception ex) { txtResponse.Text = ex.ToString(); }
     btnSign.Enabled = true;
 }