当前位置: 首页>>代码示例>>C#>>正文


C# RSACryptoServiceProvider.VerifyData方法代码示例

本文整理汇总了C#中System.Security.Cryptography.RSACryptoServiceProvider.VerifyData方法的典型用法代码示例。如果您正苦于以下问题:C# RSACryptoServiceProvider.VerifyData方法的具体用法?C# RSACryptoServiceProvider.VerifyData怎么用?C# RSACryptoServiceProvider.VerifyData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Security.Cryptography.RSACryptoServiceProvider的用法示例。


在下文中一共展示了RSACryptoServiceProvider.VerifyData方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: VerifySignedHash

        public static string VerifySignedHash(string str_DataToVerify, string str_SignedData, string str_publicKeyFilePath)
        {
            byte[] SignedData = Convert.FromBase64String(str_SignedData);

            UTF8Encoding ByteConverter = new UTF8Encoding();
            byte[] DataToVerify = ByteConverter.GetBytes(str_DataToVerify);
            try
            {
                string sPublicKeyPEM = File.ReadAllText(str_publicKeyFilePath);
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

                rsa.PersistKeyInCsp = false;
                rsa.LoadPublicKeyPEM(sPublicKeyPEM);

                if (rsa.VerifyData(DataToVerify, "SHA256", SignedData))
                {
                    return "verify success";
                }
                else
                {
                    return "verify fail";
                }

            }
            catch (CryptographicException e)
            {
                Console.WriteLine(e.Message);

                return "verify error";
            }
        }
开发者ID:ethanly61,项目名称:pingpp-csharp,代码行数:31,代码来源:Webhooks.aspx.cs

示例2: LoadFromString

        public static bool LoadFromString(string proFile)
        {
            try
            {
                string proData = ReadTag(proFile, "Pro");
                proData = proData.Replace("\r\n", "");

                string signature = ReadTag(proFile, "Signature");

                var pubRsa = new RSACryptoServiceProvider();
                pubRsa.FromXmlString(JmgPublicKey);

                // check signature
                Verified = pubRsa.VerifyData(
                                UTF8Encoding.UTF8.GetBytes(proData),
                                new SHA1CryptoServiceProvider(),
                                Convert.FromBase64String(signature));

                if (!Verified)
                    return false;

                SignedFile = proFile;
                ID = ReadTag(proData, "ID");
                Name = ReadTag(proData, "Name");
                Company = ReadTag(proData, "Company");
                Date = ReadTag(proData, "Date");

                return true;
            }
            catch { return false; }
        }
开发者ID:dpvreony-forks,项目名称:CodePerspective,代码行数:31,代码来源:Pro.cs

示例3: Main

        static void Main(string[] args)
        {
            // Create message and signature on your end
            string message = "Here is the license message";
            var secret = "wangchunlei";
            var converter = new ASCIIEncoding();
            byte[] plainText = converter.GetBytes(secret);

            var rsaWrite = new RSACryptoServiceProvider();
            var privateParams = rsaWrite.ExportParameters(true);

            // Generate the public key / these can be sent to the user.
            var publicParams = rsaWrite.ExportParameters(false);

            byte[] signature =
                rsaWrite.SignData(plainText, new SHA1CryptoServiceProvider());

            // Verify from the user's side. Note that only the public parameters
            // are needed.
            var rsaRead = new RSACryptoServiceProvider();
            rsaRead.ImportParameters(publicParams);
            if (rsaRead.VerifyData(plainText,
                                new SHA1CryptoServiceProvider(),
                                signature))
            {
                Console.WriteLine("Verified!");
            }
            else
            {
                Console.WriteLine("NOT verified!");
            }
        }
开发者ID:CuneytKukrer,项目名称:TestProject,代码行数:32,代码来源:Program.cs

示例4: Main

        static void Main(string[] args)
        {
            // To idendify the Smart Card CryptoGraphic Providers on your
            // computer, use the Microsoft Registry Editor (Regedit.exe).
            // The available Smart Card CryptoGraphic Providers are listed
            // in HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider.

            // Create a new CspParameters object that identifies a
            // Smart Card CryptoGraphic Provider.
            // The 1st parameter comes from HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider Types.
            // The 2nd parameter comes from HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider.
            CspParameters csp = new CspParameters(1);
            csp.KeyContainerName = "MyKeyContainer ya!!";
            csp.Flags = CspProviderFlags.UseDefaultKeyContainer;

            // Initialize an RSACryptoServiceProvider object using
            // the CspParameters object.
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp);
            Console.WriteLine("KeyName:{0}", rsa.ToXmlString(true));

            // Create some data to sign.
            byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };

            Console.WriteLine("Data			: " + BitConverter.ToString(data, 0, data.Length));

            // Sign the data using the Smart Card CryptoGraphic Provider.
            byte[] signData = rsa.SignData(data, "SHA1");

            Console.WriteLine("Signature:" + BitConverter.ToString(signData));

            bool verify = rsa.VerifyData(data, "SHA1", signData);//原始資料和sign過的資料作SHA1比對
            Console.WriteLine("驗證資料:" + verify);

            Console.ReadKey();
        }
开发者ID:knarf7112,项目名称:Proxy,代码行数:35,代码来源:Program.cs

示例5: Main

        static void Main(string[] args)
        {
            RSACryptoServiceProvider signer = new RSACryptoServiceProvider();

            FileStream file = new FileStream(args[0], FileMode.Open, FileAccess.Read);
            BinaryReader reader = new BinaryReader(file);
            byte[] data = reader.ReadBytes((int)file.Length);

            byte[] signature = signer.SignData(data, new SHA256CryptoServiceProvider());

            string publicKey = signer.ToXmlString(false);

            Console.WriteLine("Signature: " + Convert.ToBase64String(signature));
            reader.Close();
            file.Close();

            RSACryptoServiceProvider verifier = new RSACryptoServiceProvider();

            verifier.FromXmlString(publicKey);

            FileStream file2 = new FileStream(args[0], FileMode.Open, FileAccess.Read);
            BinaryReader reader2 = new BinaryReader(file2);
            byte[] data2 = reader2.ReadBytes((int)file2.Length);

            if (verifier.VerifyData(data2, new SHA256CryptoServiceProvider(), signature))
                Console.WriteLine("Signature verified");
            else 
                Console.WriteLine("Signature NOT verified");
            reader2.Close();
            file2.Close();
        }
开发者ID:oblivious,项目名称:Oblivious,代码行数:31,代码来源:Program.cs

示例6: IsValid

        /// <summary>
        /// Проверяет, действительна ли лицензия.
        /// Это включает проверку подписи на ней,
        /// а также сроков ее действия.
        /// </summary>
        /// <param name="license">Лицензия</param>
        /// <returns>true если лицензия действительна. Иначе false.</returns>
        public bool IsValid(LicenseInfo license)
        {
            if (license == null)
                throw new ArgumentNullException("license cannot be null");

            try
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.ImportParameters(_publicKey);
                if (!rsa.VerifyData(
                    GetBytesToSign(license),
                    "SHA1",
                    license.Signature
                    )
                    )
                    return false;

                if (!(license.IssueDate <= DateTime.Now &&
                    DateTime.Now <= license.IssueDate + license.Duration)
                    )
                    return false;
            }
            catch
            {
                return false;
            }

            return true;
        }
开发者ID:virl,项目名称:yttrium,代码行数:36,代码来源:LicenseValidator.cs

示例7: VerifyDigitalSignature

        /// <summary>
        /// Metoda za verifikaciju ispravnosti digitalnog potpisa dokumenta
        /// </summary>
        /// <param name="file"></param>
        public void VerifyDigitalSignature(string file)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            StreamReader streamReader = new StreamReader("javni_kljuc.txt");
            string publicKey = streamReader.ReadToEnd();
            rsa.FromXmlString(publicKey);
            streamReader.Close();
            streamReader.Dispose();

            FileStream dat = new FileStream(file, FileMode.Open, FileAccess.Read);
            BinaryReader binReader = new BinaryReader(dat);
            byte[] data = binReader.ReadBytes((int)dat.Length);
            string nameP = file + ".dp";

            TextReader streamreader = new StreamReader(nameP);
            string sign = streamreader.ReadLine();
            streamreader.Close();
            streamreader.Dispose();

            if (rsa.VerifyData(data, "SHA1", Convert.FromBase64String(sign)))
            {
                MessageBox.Show("Datoteka je ispravno potpisana", "My Application",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
                MessageBox.Show("Datoteka nije ispravno potpisana", "My Application", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            binReader.Close();
            binReader.Dispose();
            dat.Close();
            dat.Dispose();
        }
开发者ID:ivpusic,项目名称:cryptography_algorithms,代码行数:36,代码来源:DigSignatureHelper.cs

示例8: VerifyData

        public static bool VerifyData(string originalMessage, string signedMessage, RSAParameters Parameters)
        {
            bool success = false;
            var encoder = new UTF8Encoding();
            byte[] bytesToVerify = encoder.GetBytes(originalMessage);
            byte[] signedBytes = Convert.FromBase64String(signedMessage);

            RSAParameters parameters = Parameters;

            using (var rsa = new RSACryptoServiceProvider())
            {
                try
                {
                    rsa.ImportParameters(parameters);
                    SHA256Managed Hash = new SHA256Managed();

                    byte[] hashedData = Hash.ComputeHash(bytesToVerify);
                    success = rsa.VerifyData(hashedData, CryptoConfig.MapNameToOID("SHA256"), signedBytes);
                }
                catch (CryptographicException e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    rsa.PersistKeyInCsp = false;
                }
            }
            return success;
        }
开发者ID:K0HAX,项目名称:VerificationLibrary,代码行数:30,代码来源:RSAlib.cs

示例9: Main

        static void Main(string[] args)
        {
            // Create digital signature algortihm object
            // This will generate private/public key pair
            RSACryptoServiceProvider signer = new RSACryptoServiceProvider();

            // array to hold signature - will be shared
            byte[] signature = null;
            // string to hold public key - will be shared
            string publicKey = null;

            using(FileStream file = new FileStream(@"info.txt", FileMode.Open,
                FileAccess.Read))
            {
                // read file to be used to create signature into a byte array
                BinaryReader reader = new BinaryReader(file);
                byte[] data = reader.ReadBytes((int)file.Length);

                // create signature by signing data - generates a digital signature by first
                // generating the hash the data and then generate a signature based on the
                // hash and the private key
                // file, signature and public key are then shared with the recipient
                signature = signer.SignData(data,new SHA1CryptoServiceProvider());

                // export public key
                publicKey = signer.ToXmlString(false);

                reader.Close();
                file.Close();
            }

            // Create digital signature algortihm object
            // which will use the public key exported by the signer
            RSACryptoServiceProvider verifier = new RSACryptoServiceProvider();
            verifier.FromXmlString(publicKey);

            using (FileStream file2 = new FileStream(@"info.txt", FileMode.Open,
                FileAccess.Read))
            {
                // read file to be used to verify the signature into a byte array
                BinaryReader reader2 = new BinaryReader(file2);
                byte[] data2 = reader2.ReadBytes((int)file2.Length);

                // verify the signature based on the contents of the file
                // verification will only succeed if the signature was generated from this
                // file using the correct private key, thus confirming the identity of the
                // signer
                if (verifier.VerifyData(data2, new SHA1CryptoServiceProvider(), signature))
                {
                    Console.WriteLine("Verified");
                }
                else
                {
                    Console.WriteLine("NOT verified");
                }

                reader2.Close();
                file2.Close();
            }
        }
开发者ID:BigBearGCU,项目名称:FNDEV-Week10-Cryptography,代码行数:60,代码来源:Program.cs

示例10: verifyMoneyWithCachierPrivateKey

        private void verifyMoneyWithCachierPrivateKey(MoneyFetchResponse response)
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            RSA.FromXmlString(File.ReadAllText(@"D:\CashierKeys\public.xml"));

            foreach (var money in response.moneyList)
            {
                bool verify = RSA.VerifyData(Encoding.ASCII.GetBytes(money.hash), new SHA1Managed(), Convert.FromBase64String(money.signature));
                Assert.IsTrue(verify);
            }
        }
开发者ID:SujaanKanwar,项目名称:Service_MadMoney,代码行数:11,代码来源:MoneyFetchTestCases.cs

示例11: VerifySignature

        public static bool VerifySignature(string text, string base64_encoded_signature, string my_public_key)
        {
            RSACryptoServiceProvider rsacp = new RSACryptoServiceProvider(2048);
            rsacp.FromXmlString(my_public_key);

            ASCIIEncoding ByteConverter = new ASCIIEncoding();
            byte[] verify_this = ByteConverter.GetBytes(text);
            byte[] signature = Convert.FromBase64String(base64_encoded_signature);
            bool ok = rsacp.VerifyData(verify_this, new SHA1CryptoServiceProvider(), signature);
            return ok;
        }
开发者ID:uQr,项目名称:Visual-NHibernate,代码行数:11,代码来源:LicenseHelper.cs

示例12: verify

 /// <summary>
 /// 验签
 /// </summary>
 /// <param name="content">待验签字符串</param>
 /// <param name="signedString">签名</param>
 /// <param name="publicKey">公钥</param>
 /// <param name="input_charset">编码格式</param>
 /// <returns>true(通过),false(不通过)</returns>
 public static bool verify(string content, string signedString, string publicKey, string input_charset)
 {
     bool result = false;
     byte[] Data = Encoding.GetEncoding(input_charset).GetBytes(content);
     byte[] data = Convert.FromBase64String(signedString);
     RSAParameters paraPub = ConvertFromPublicKey(publicKey);
     RSACryptoServiceProvider rsaPub = new RSACryptoServiceProvider();
     rsaPub.ImportParameters(paraPub);
     SHA1 sh = new SHA1CryptoServiceProvider();
     result = rsaPub.VerifyData(Data, sh, data);
     return result;
 }
开发者ID:jingwang109,项目名称:mobile-sdk,代码行数:20,代码来源:RSA.cs

示例13: Main

 static void Main()
 {
     bool isAlreadyRunning = SingleInstance.IsInstanceRunning();
     if (isAlreadyRunning)
     {
         IntPtr hw;
         if ((hw = FindWindowCE(null, "TubeRun")) != IntPtr.Zero)
         {
             SetForegroundWindow((IntPtr)((int)hw | 0x01));
             CloseHandle(hw);
         }
         return;
     }
     if (USEPROTECTION)
     {
         try
         {
             byte[] licenseBytes = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Security\\Software\\Microsoft\\Marketplace\\Licenses\\").GetValue(m_appSku) as byte[];
             string data = m_appSku.ToLower() + " " + GetUniqueDeviceID();
             RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
             //Create a new instance of RSAParameters.
             RSAParameters RSAKeyInfo = new RSAParameters();
             //Set RSAKeyInfo to the public key values.
             RSAKeyInfo.Modulus = Convert.FromBase64String(m_certModulus);
             RSAKeyInfo.Exponent = Convert.FromBase64String(m_certExponent);
             rsa.ImportParameters(RSAKeyInfo);
             byte[] bData = System.Text.Encoding.ASCII.GetBytes(data);
             if ((null == licenseBytes || !rsa.VerifyData(bData, new SHA1CryptoServiceProvider(), licenseBytes)))
             {
                 MessageBox.Show("You don't have a valid licence for TubeRun. Please, reinstall it from Marketplace.", "TubeRun", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                 return;
             }
             else
             {
                 startApp();
             }
         }
         catch (Exception)
         {
             MessageBox.Show("Windows Mobile Marketplace is not installed.", "TubeRun", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
         }
     }
     else
     {
         try
         {
             startApp();
         }
         catch (Exception)
         {
         }
     }
 }
开发者ID:jpapayan,项目名称:tuberun_wm,代码行数:53,代码来源:Program.cs

示例14: VerifyDataRSA

        /// <summary>
        /// Verifies the specified signature data by comparing it to the signature computed for the specified data.
        /// </summary>
        /// <param name="source">The original data to be verified.</param>
        /// <param name="signature">The signature data to be verified.</param>
        /// <param name="publicKey">The public key for System.Security.Cryptography.RSA.</param>
        /// <returns>true if the signature verifies as valid; otherwise, false.</returns>
        public static bool VerifyDataRSA(this byte[] source, byte[] signature, RSAParameters publicKey)
        {
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportParameters(publicKey);

                using (SHA512Managed sha = new SHA512Managed())
                {
                    return rsa.VerifyData(source, sha, signature);
                }
            }
        }
开发者ID:kangwl,项目名称:KANG.Frame,代码行数:19,代码来源:SecurityExtensions.cs

示例15: Der

        public void Der()
        {
            if (Environment.OSVersion.Platform == PlatformID.Unix)
                return; // No DLLs on Unix
            // TODO: Conditional compilation for mono compability

            byte[] publicKeyDER = Resources.GetResource("RSACryptoServiceProviderExtensionPublicKey.der");
            byte[] privateKeyDER = Resources.GetResource("RSACryptoServiceProviderExtensionPrivateKey.der");

            Console.WriteLine("Public key:\n{0}\n", BitConverter.ToString(publicKeyDER).Replace("-", ""));
            Console.WriteLine("Private key:\n{0}\n", BitConverter.ToString(privateKeyDER).Replace("-", ""));

            byte[] signature;

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.PersistKeyInCsp = false;
                rsa.ImportPrivateKeyDer(privateKeyDER);
                using (var sha1 = new SHA1CryptoServiceProvider())
                    signature = rsa.SignData(DataToSign, sha1);
            }

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.PersistKeyInCsp = false;
                rsa.ImportPublicKeyDer(publicKeyDER);

                bool isValidSignature;
                using (var sha1 = new SHA1CryptoServiceProvider())
                    isValidSignature = rsa.VerifyData(DataToSign, sha1, signature);
                Assert.IsTrue(isValidSignature);

                // invalidate signature so the next check must fail
                signature[signature.Length - 1] ^= 0xFF;
                using (var sha1 = new SHA1CryptoServiceProvider())
                    isValidSignature = rsa.VerifyData(DataToSign, sha1, signature);
                Assert.IsFalse(isValidSignature);
            }
        }
开发者ID:nikeee,项目名称:nth,代码行数:39,代码来源:RSACryptoServiceProviderExtensionsTests.cs


注:本文中的System.Security.Cryptography.RSACryptoServiceProvider.VerifyData方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。