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


C# Cryptography.RSAPKCS1SignatureFormatter类代码示例

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


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

示例1: RSAEncrypt

        static public byte[] RSAEncrypt(int bits, byte[] dataToEncrypt, RSAParameters rsaKeyInfo, bool doOAEPPadding)
        {
            try
            {
                byte[] encryptedData;
                //Create a new instance of RSACryptoServiceProvider.
                using (var rsa = new RSACryptoServiceProvider(bits))
                {

                    //Import the RSA Key information. This only needs
                    //toinclude the public key information.
                    rsa.ImportParameters(rsaKeyInfo);

                    var rsaExportParameters = rsa.ExportParameters(true);

                    var rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
                    rsaFormatter.SetHashAlgorithm("SHA256");

                    //Encrypt the passed byte array and specify OAEP padding.  
                    //OAEP padding is only available on Microsoft Windows XP or
                    //later.  
                    encryptedData = rsa.Encrypt(dataToEncrypt, doOAEPPadding);
                }
                return encryptedData;
            }
            //Catch and display a CryptographicException  
            //to the console.
            catch (CryptographicException e)
            {
                Console.WriteLine(e.Message);

                return null;
            }

        }
开发者ID:dr1s,项目名称:rom_tool,代码行数:35,代码来源:RSA.cs

示例2: SignData

        public byte[] SignData(byte[] hashOfDataToSign)
        {            
            var rsaFormatter = new RSAPKCS1SignatureFormatter(mRsa);
            rsaFormatter.SetHashAlgorithm("SHA256");

            return rsaFormatter.CreateSignature(hashOfDataToSign);            
        }
开发者ID:corebob,项目名称:skulls-bones-and-digital-signatures,代码行数:7,代码来源:DigitalSignature.cs

示例3: SignN3Rsa

        public string SignN3Rsa(string data)
        {
            var cspParams = new CspParameters {KeyContainerName = "XML_DSIG_RSA_KEY"};
            var key = new RSACryptoServiceProvider(cspParams);
            var cspBlob = key.ExportCspBlob(false);
            var base64Blob = Convert.ToBase64String(cspBlob);

            var rsaFormatter = new RSAPKCS1SignatureFormatter(key);
            rsaFormatter.SetHashAlgorithm("MD5");

            var hash = Md5Helper.GetMd5Hash(data);
            var base64Hash = Convert.ToBase64String(hash);
            var sign = rsaFormatter.CreateSignature(hash);
            var base64Sign = Convert.ToBase64String(sign);

            var signData = new SignData
                           {
                               data = data,
                               public_key = base64Blob,
                               hash = base64Hash,
                               sign = base64Sign
                           };

            return new SerializationHelper<SignData>().Serialize(signData);
        }
开发者ID:nbIxMaN,项目名称:RestTest,代码行数:25,代码来源:SignatureHelper.cs

示例4: Sign

        public DigitalSignatureCreationResult Sign(DigitalSignatureCreationArguments arguments)
        {
            var res = new DigitalSignatureCreationResult();
            try
            {
                var rsaProviderReceiver = new RSACryptoServiceProvider();
                rsaProviderReceiver.FromXmlString(arguments.PublicKeyForEncryption.ToString());
                var encryptionResult = rsaProviderReceiver.Encrypt(Encoding.UTF8.GetBytes(arguments.Message), false);
                var hashed = _hashingService.Hash(Convert.ToBase64String(encryptionResult));

                var rsaProviderSender = new RSACryptoServiceProvider();
                rsaProviderSender.FromXmlString(arguments.FullKeyForSignature.ToString());
                var signatureFormatter = new RSAPKCS1SignatureFormatter(rsaProviderSender);
                signatureFormatter.SetHashAlgorithm(_hashingService.HashAlgorithmCode());
                var signature = signatureFormatter.CreateSignature(hashed.HashedBytes);

                res.Signature = signature;
                res.CipherText = Convert.ToBase64String(encryptionResult);
                res.Success = true;
            }
            catch (Exception ex)
            {
                res.ExceptionMessage = ex.Message;
            }

            return res;
        }
开发者ID:richardkundl,项目名称:SampleDI-IoC,代码行数:27,代码来源:RsaPkcs1DigitalSignatureService.cs

示例5: Sign

        private byte[] Sign(SHA1CryptoServiceProvider hash)
        {
            var formatter = new RSAPKCS1SignatureFormatter(_certificate.PrivateKey).
                Tap(it => it.SetHashAlgorithm("MD5"));

            return formatter.CreateSignature(hash);
        }
开发者ID:ChrisRomp,项目名称:Xero-Net,代码行数:7,代码来源:RsaSha1.cs

示例6: CreateFormatter

        public override AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            var provider = (RSACryptoServiceProvider)key;

            // The provider is probably using the default ProviderType. That's
            // a problem, because it doesn't support SHA256. Let's do some
            // black magic and create a new provider of a type that supports
            // SHA256 without the user ever knowing we fix this. This is what
            // is done in X509AsymmetricKey.GetSignatureFormatter if
            // http://www.w3.org/2001/04/xmldsig-more#rsa-sha256 isn't
            // a known algorithm, so users kind of expect this to be handled
            // for them magically.

            var cspParams = new CspParameters();
            cspParams.ProviderType = 24; //PROV_RSA_AES
            cspParams.KeyContainerName = provider.CspKeyContainerInfo.KeyContainerName;
            cspParams.KeyNumber = (int)provider.CspKeyContainerInfo.KeyNumber;
            SetMachineKeyFlag(provider, cspParams);

            cspParams.Flags |= CspProviderFlags.UseExistingKey;

            provider = new RSACryptoServiceProvider(cspParams);

            var f = new RSAPKCS1SignatureFormatter(provider);
            f.SetHashAlgorithm(typeof(SHA256Managed).FullName);
            return f;
        }
开发者ID:CDHDeveloper,项目名称:authservices,代码行数:32,代码来源:ManagedSha256SignatureDescription.cs

示例7: Signature

 public static void Signature(Stream input, Stream output, string privateKey)
 {
     using (var sha = new SHA256CryptoServiceProvider())
     using (var rsa = new RSACryptoServiceProvider())
     {
         // Compute hash
         var buffer = ReadAllBytes(input);
         var hash = sha.ComputeHash(buffer);
         // RSA Initialize
         rsa.FromXmlString(privateKey);
         // format
         var formatter = new RSAPKCS1SignatureFormatter(rsa);
         formatter.SetHashAlgorithm("SHA256");
         var signature = formatter.CreateSignature(hash);
         // Krile Signature Package
         var magic = MagicStr + ":" + signature.Length + ":";
         var magicbytes = Encoding.UTF8.GetBytes(magic);
         if (magicbytes.Length > 64)
             throw new Exception("Magic bits too long.");
         output.Write(magicbytes, 0, magicbytes.Length);
         var padding = new byte[64 - magicbytes.Length];
         output.Write(padding, 0, padding.Length);
         output.Write(signature, 0, signature.Length);
         output.Write(buffer, 0, buffer.Length);
     }
 }
开发者ID:Kei-Nanigashi,项目名称:StarryEyes,代码行数:26,代码来源:Cryptography.cs

示例8: Sign

        public static byte[] Sign(byte[] privateKey, Stream stream)
        {
            #if Windows
            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.FromXmlString(Encoding.ASCII.GetString(privateKey));

                var rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
                rsaFormatter.SetHashAlgorithm("SHA256");

                using (var Sha256 = SHA256.Create())
                {
                    return rsaFormatter.CreateSignature(Sha256.ComputeHash(stream));
                }
            }
            #endif

            #if Unix
            lock (_lockObject)
            {
                using (var rsa = new RSACryptoServiceProvider())
                {
                    rsa.FromXmlString(Encoding.ASCII.GetString(privateKey));

                    var rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
                    rsaFormatter.SetHashAlgorithm("SHA256");

                    using (var Sha256 = SHA256.Create())
                    {
                        return rsaFormatter.CreateSignature(Sha256.ComputeHash(stream));
                    }
                }
            }
            #endif
        }
开发者ID:Alliance-Network,项目名称:Library,代码行数:35,代码来源:Rsa2048_Sha512.cs

示例9: CreateSignature

        public byte[] CreateSignature(byte[] hash)
	    {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
            RSAFormatter.SetHashAlgorithm("MD5");
            m_public = RSA.ExportParameters(false);
            return RSAFormatter.CreateSignature(hash);
        }
开发者ID:sasha2567,项目名称:8--,代码行数:8,代码来源:DigitalSignatureHelper+.cs

示例10: CreateFormatter

 public override AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm key)
 {
     if (key == null)
         throw new ArgumentNullException(nameof(key));
     var f = new RSAPKCS1SignatureFormatter(key);
     f.SetHashAlgorithm(SHA_512);
     return f;
 }
开发者ID:nofuture-git,项目名称:31g,代码行数:8,代码来源:Misc.cs

示例11: CreateFormatter

        public override AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm key)
        {
            if (key == null)
                throw new ArgumentNullException("key");

            RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(key);
            formatter.SetHashAlgorithm("SHA256");
            return formatter;
        }
开发者ID:scholtz,项目名称:FastZep,代码行数:9,代码来源:RSAPKCS1SHA256SignatureDescription.cs

示例12: sign

 public byte[] sign()
 {
     SSC.RSA rsa = SSC.RSA.Create();
     rsa.ImportParameters(RSAparams);
     SSC.RSAPKCS1SignatureFormatter signer = new SSC.RSAPKCS1SignatureFormatter(rsa);
     signer.SetHashAlgorithm("SHA1");
     sc.Close();
     return signer.CreateSignature(md.Hash);
 }
开发者ID:JamesHagerman,项目名称:sftprelay,代码行数:9,代码来源:SignatureRSA.cs

示例13: sign

        public byte[] sign()
        {
            m_cs.Close();
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            RSA.ImportParameters(m_RSAKeyInfo);
            RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
            RSAFormatter.SetHashAlgorithm("SHA1");

            return RSAFormatter.CreateSignature(m_sha1);
        }
开发者ID:x893,项目名称:SharpSSH,代码行数:10,代码来源:SignatureRSA.cs

示例14: CreateSignature

 public static string CreateSignature(string textToSign, string XMLprivateKey)
 {
     RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
     RSA.FromXmlString(XMLprivateKey);
     RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
     RSAFormatter.SetHashAlgorithm("SHA1");
     SHA1Managed SHhash = new SHA1Managed();
     byte[] SignedHashValue = RSAFormatter.CreateSignature(SHhash.ComputeHash(new UnicodeEncoding().GetBytes(textToSign)));
     string signature = System.Convert.ToBase64String(SignedHashValue);
     return signature;
 }
开发者ID:fiveohhh,项目名称:Client-Tracker,代码行数:11,代码来源:Encryption.cs

示例15: GenerateLicense

        public static void GenerateLicense(int months)
        {
            // The license key, it must be 20 bytes long to be compatible with RSA
            string licenseKey = DateTime.Now.Date.AddMonths(months).ToShortDateString();
            // If a current license exists, add the new license length onto the old one.
            if (File.Exists(Environment.CurrentDirectory + @"\licenseinfo.xml"))
            {
                XmlDocument licenseFile = new XmlDocument();
                licenseFile.Load(Environment.CurrentDirectory + @"\licenseinfo.xml");
                string savedLicenseKey = licenseFile.DocumentElement.SelectSingleNode(@"/LicenseInfo/KEY").InnerText;
                string licenseExpiry = savedLicenseKey.Substring(0, savedLicenseKey.LastIndexOf(@"/") + 5);
                if (Convert.ToDateTime(licenseExpiry) > DateTime.Today)
                {
                    licenseKey = (Convert.ToDateTime(licenseExpiry).AddMonths(months)).ToShortDateString();
                }
            }
            string machineName = Environment.MachineName;
            licenseKey += machineName;
            // Adds trailing 0's if the date+machinename is less than 20 charachters
            while (licenseKey.Length < 20)
            {
                licenseKey += "0";
            }
            // Cuts the key down to 20 charachters if the date+machinename are too large.
            licenseKey = licenseKey.Substring(0, 20);

            // Byte arrays to store the license key
            byte[] byteLicenseKey = Encoding.ASCII.GetBytes(licenseKey);

            // The RSA handling and public key creation
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            RSAPKCS1SignatureFormatter rsaFormatter = new RSAPKCS1SignatureFormatter(RSA);
            rsaFormatter.SetHashAlgorithm("SHA1");
            string publicKey = RSA.ToXmlString(false);

            // The digital signature for the license
            byte[] digitalSignature = rsaFormatter.CreateSignature(byteLicenseKey);

            //An XML file that holds the public key, license key, and the Digital Signature
            XmlDocument licenseDocument = new XmlDocument();
            XmlElement parentNode = licenseDocument.CreateElement("LicenseInfo");
            licenseDocument.AppendChild(parentNode);
            XmlElement xmlDocLicenseKey = licenseDocument.CreateElement("KEY");
            xmlDocLicenseKey.InnerText = Encoding.Default.GetString(byteLicenseKey);
            parentNode.AppendChild(xmlDocLicenseKey);
            XmlElement xmlDocLicenseSignature = licenseDocument.CreateElement("SignedKey");
            xmlDocLicenseSignature.InnerText = Convert.ToBase64String(digitalSignature);
            parentNode.AppendChild(xmlDocLicenseSignature);
            XmlDocumentFragment publicKeyNode = licenseDocument.CreateDocumentFragment();
            publicKeyNode.InnerXml = publicKey;
            parentNode.AppendChild(publicKeyNode);
            licenseDocument.Save(Environment.CurrentDirectory + @"\licenseinfo.xml");
        }
开发者ID:JordanVlieg,项目名称:LicenseEnforcement,代码行数:53,代码来源:LicenseConfirm.cs


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