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


C# RSACryptoServiceProvider.SignData方法代码示例

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


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

示例1: 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

示例2: SignData

        public static string SignData(byte[] message, RSAParameters privateKey)
        {
            //// The array to store the signed message in bytes
            byte[] signedBytes;
            using (var rsa = new RSACryptoServiceProvider())
            {
                byte[] originalData = message;

                try
                {
                    //// Import the private key used for signing the message
                    rsa.ImportParameters(privateKey);

                    //// Sign the data, using SHA512 as the hashing algorithm
                    signedBytes = rsa.SignData(originalData, CryptoConfig.MapNameToOID("SHA512"));
                }
                catch (CryptographicException e)
                {
                    Console.WriteLine(e.Message);
                    return null;
                }
                finally
                {
                    //// Set the keycontainer to be cleared when rsa is garbage collected.
                    rsa.PersistKeyInCsp = false;
                }
            }
            //// Convert the a base64 string before returning
            return Convert.ToBase64String(signedBytes);
        }
开发者ID:MasterQ32,项目名称:Microblog,代码行数:30,代码来源:MainWindow.xaml.cs

示例3: SignAuthCookieV1

        public string SignAuthCookieV1(string cookie)
        {
            byte[] privateKey = this.GetPrivateKey(KeyType.AuthCookieV1);

            //// The array to store the signed message in bytes
            byte[] signedBytes;
            using (var rsa = new RSACryptoServiceProvider())
            {
                // Write the message to a byte array using UTF8 as the encoding.

                byte[] originalData = System.Text.Encoding.UTF8.GetBytes(cookie);

                try
                {
                    // Import the private key used for signing the message
                    rsa.ImportParameters(SecureSigningService.FromBinaryToRSAParameters(privateKey));

                    signedBytes = rsa.SignData(originalData, CryptoConfig.MapNameToOID("SHA512"));
                }
                catch (CryptographicException e)
                {
                    Console.WriteLine(e.Message);
                    return null;
                }
                finally
                {
                    //// Set the keycontainer to be cleared when rsa is garbage collected.
                    rsa.PersistKeyInCsp = false;
                }
            }

            // Convert the a base64 string before returning
            return Convert.ToBase64String(signedBytes);
        }
开发者ID:nrag,项目名称:yapper,代码行数:34,代码来源:SecureKeyService.cs

示例4: SignData

        public static string SignData(string originalMessage, RSAParameters Parameters)
        {
            string success = "";
            var encoder = new UTF8Encoding();
            byte[] bytesToSign = encoder.GetBytes(originalMessage);

            RSAParameters parameters = Parameters;

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

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

示例5: 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

示例6: 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

示例7: MakeDigitalSignature

        /// <summary>
        /// Metoda za digitalno potpisivanje dokumenta
        /// </summary>
        /// <param name="file"></param>
        public void MakeDigitalSignature(string file)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            StreamReader streamReader = new StreamReader("privatni_kljuc.txt");
            string publicOnlyKeyXml = streamReader.ReadToEnd();
            rsa.FromXmlString(publicOnlyKeyXml);

            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);
            byte[] sign = rsa.SignData(data, "SHA1");

            binReader.Close();
            binReader.Dispose();
            dat.Close();
            dat.Dispose();

            string datName = file + ".dp";

            TextWriter tw = new StreamWriter(datName);
            tw.WriteLine(Convert.ToBase64String(sign));
            tw.Close();
            tw.Dispose();
        }
开发者ID:ivpusic,项目名称:cryptography_algorithms,代码行数:32,代码来源:DigSignatureHelper.cs

示例8: Calculate

        public string Calculate(string input, RSACryptoServiceProvider provider)
        {
            byte[] podatki = Encoding.ASCII.GetBytes(input);
              byte[] signature = provider.SignData(podatki, CryptoConfig.MapNameToOID("SHA256"));

              return this.CalculateMD5Hash(signature);
        }
开发者ID:dstrucl,项目名称:Tangenta40,代码行数:7,代码来源:ProtectiveMark.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: AsymmetricSign

        public static byte[] AsymmetricSign(byte[] Data, string xmlPrivateKey)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(xmlPrivateKey);

            byte[] signData = rsa.SignData(Data, new SHA1CryptoServiceProvider());

            return signData;
        }
开发者ID:Zoadian,项目名称:md-config-tool,代码行数:9,代码来源:UtilityCryptography.cs

示例11: ComputeSignature

 private static byte[] ComputeSignature(string outputFileName, RSACryptoServiceProvider cryptoServiceProvider)
 {
     byte[] signature;
     using( var packageFileStream = new FileStream(outputFileName, FileMode.Open))
     {
         signature = cryptoServiceProvider.SignData(packageFileStream, CreateHashAlgorithm());
     }
     return signature;
 }
开发者ID:SzymonPobiega,项目名称:PackMan,代码行数:9,代码来源:PackageSigner.cs

示例12: RsaTest_ExportImportTest

        public MFTestResults RsaTest_ExportImportTest()
        {
            bool testResult = true;

            try
            {
                using (Session session = new Session("", MechanismType.RSA_PKCS))
                using (CryptoKey privateKey = CryptoKey.LoadKey(session, m_importKeyPrivate))
                {
                    string dataToSign = "This is a simple message to be encrypted";

                    byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(dataToSign);

                    using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(privateKey))
                    {
                        RSAParameters kp1 = rsa.ExportParameters(true);

                        byte[] sig = rsa.SignData(data);

                        rsa.ImportParameters(kp1);

                        RSAParameters kp2 = rsa.ExportParameters(true);

                        testResult &= CompareByteArray(kp1.D, kp2.D);
                        testResult &= CompareByteArray(kp1.DP, kp2.DP);
                        testResult &= CompareByteArray(kp1.DQ, kp2.DQ);
                        testResult &= CompareByteArray(kp1.Exponent, kp2.Exponent);
                        testResult &= CompareByteArray(kp1.InverseQ, kp2.InverseQ);
                        testResult &= CompareByteArray(kp1.Modulus, kp2.Modulus);
                        testResult &= CompareByteArray(kp1.P, kp2.P);
                        testResult &= CompareByteArray(kp1.Q, kp2.Q);

                        testResult &= CompareByteArray(m_importKeyPrivate[2].Value, kp1.Modulus);
                        testResult &= CompareByteArray(m_importKeyPrivate[3].Value, kp1.Exponent);
                        testResult &= CompareByteArray(m_importKeyPrivate[4].Value, kp1.D);
                        testResult &= CompareByteArray(m_importKeyPrivate[5].Value, kp1.P);
                        testResult &= CompareByteArray(m_importKeyPrivate[6].Value, kp1.Q);
                        testResult &= CompareByteArray(m_importKeyPrivate[7].Value, kp1.DP);
                        testResult &= CompareByteArray(m_importKeyPrivate[8].Value, kp1.DQ);
                        testResult &= CompareByteArray(m_importKeyPrivate[9].Value, kp1.InverseQ);


                        testResult &= rsa.VerifyData(data, sig);
                    }
                }

            }
            catch (Exception ex)
            {
                Log.Exception("Unexpected Exception", ex);
                testResult = false;
            }

            return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
        }
开发者ID:koson,项目名称:.NETMF_for_LPC17xx,代码行数:55,代码来源:RSATest.cs

示例13: SignData

        /// <summary>
        /// Create a crytographic signature for a block of data.
        /// </summary>
        /// <param name="data">The data to be signed.</param>
        /// <param name="rsa">The Crypto Service Provider to be used.</param>
        /// <returns>The signature string for the data block.</returns>
        public static string SignData(this byte[] data, RSACryptoServiceProvider rsa)
        {
            if(data == null) {
                throw new ArgumentNullException("data");
            }
            if(rsa == null) {
                throw new ArgumentNullException("rsa");
            }

            // sign data and prepend signature type
            return "rsa-sha1:" + Convert.ToBase64String(rsa.SignData(data, "SHA1"));
        }
开发者ID:sdether,项目名称:DReAM,代码行数:18,代码来源:CryptoEx.cs

示例14: CreateSignature

        public static string CreateSignature(string text, string my_private_key)
        {
            RSACryptoServiceProvider rsacp = new RSACryptoServiceProvider(2048);
            rsacp.FromXmlString(my_private_key);

            ASCIIEncoding ByteConverter = new ASCIIEncoding();
            byte[] sign_this = ByteConverter.GetBytes(text);
            byte[] signature = rsacp.SignData(sign_this, new SHA1CryptoServiceProvider());
            string base64_string = Convert.ToBase64String(signature);

            return base64_string;
        }
开发者ID:uQr,项目名称:Visual-NHibernate,代码行数:12,代码来源:LicenseHelper.cs

示例15: SignLicense

        private static string SignLicense(string privateKeyFile, string licenseFile)
        {
            string keyXml = GetKeyXml(privateKeyFile);

            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            provider.FromXmlString(keyXml);
            using (FileStream stream = File.OpenRead(licenseFile))
            {
                string signature = EncodeToBase64(provider.SignData(stream, new SHA1CryptoServiceProvider()));
                return signature;
            }
        }
开发者ID:ekepes,项目名称:DemoCode,代码行数:12,代码来源:Program.cs


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