• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# SignatureDescription类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: Main

//引入命名空间
using System;
using System.Security;
using System.Security.Cryptography;

class SignatureDescriptionImpl
{
    [STAThread]
    static void Main(string[] args)
    {
        // Create a digital signature based on RSA encryption.
        SignatureDescription rsaSignature = CreateRSAPKCS1Signature();
        ShowProperties(rsaSignature);

        // Create a digital signature based on DSA encryption.
        SignatureDescription dsaSignature = CreateDSASignature();
        ShowProperties(dsaSignature);

        // Create a HashAlgorithm using the digest algorithm of the signature.
        HashAlgorithm hashAlgorithm = dsaSignature.CreateDigest();
        Console.WriteLine("\nHash algorithm for the DigestAlgorithm property:"
            + " " + hashAlgorithm.ToString());

        // Create an AsymmetricSignatureFormatter instance using the DSA key.
        DSA dsa = DSA.Create();
        AsymmetricSignatureFormatter asymmetricFormatter =
            CreateDSAFormatter(dsa);
        
        // Create an AsymmetricSignatureDeformatter instance using the
        // DSA key.
        AsymmetricSignatureDeformatter asymmetricDeformatter =
            CreateDSADeformatter(dsa);

        Console.WriteLine("This sample completed successfully; " +
            "press Enter to exit.");
        Console.ReadLine();
    }

    // Create a SignatureDescription for RSA encryption.
    private static SignatureDescription CreateRSAPKCS1Signature()
    {
        SignatureDescription signatureDescription = 
            new SignatureDescription();

        // Set the key algorithm property for RSA encryption.
        signatureDescription.KeyAlgorithm =
            "System.Security.Cryptography.RSACryptoServiceProvider";

        // Set the digest algorithm for RSA encryption using the
        // SHA1 provider.
        signatureDescription.DigestAlgorithm =
            "System.Security.Cryptography.SHA1CryptoServiceProvider";

        // Set the formatter algorithm with the RSAPKCS1 formatter.
        signatureDescription.FormatterAlgorithm =
            "System.Security.Cryptography.RSAPKCS1SignatureFormatter";

        // Set the formatter algorithm with the RSAPKCS1 deformatter.
        signatureDescription.DeformatterAlgorithm =
            "System.Security.Cryptography.RSAPKCS1SignatureDeformatter";

        return signatureDescription;
    }

    // Create a SignatureDescription using a constructed SecurityElement for 
    // DSA encryption.
    private static SignatureDescription CreateDSASignature()
    {
        SecurityElement securityElement = new SecurityElement("DSASignature");

        // Create new security elements for the four algorithms.
        securityElement.AddChild(new SecurityElement(
            "Key",
            "System.Security.Cryptography.DSACryptoServiceProvider"));
        securityElement.AddChild(new SecurityElement(
            "Digest",
            "System.Security.Cryptography.SHA1CryptoServiceProvider")); 
        securityElement.AddChild(new SecurityElement(
            "Formatter",
            "System.Security.Cryptography.DSASignatureFormatter"));
        securityElement.AddChild(new SecurityElement(
            "Deformatter",
            "System.Security.Cryptography.DSASignatureDeformatter"));

        SignatureDescription signatureDescription = 
            new SignatureDescription(securityElement);

        return signatureDescription;
    }

    // Create a signature formatter for DSA encryption.
    private static AsymmetricSignatureFormatter CreateDSAFormatter(DSA dsa)
    {
        // Create a DSA signature formatter for encryption.
        SignatureDescription signatureDescription = 
            new SignatureDescription();
        signatureDescription.FormatterAlgorithm =
            "System.Security.Cryptography.DSASignatureFormatter";

        AsymmetricSignatureFormatter asymmetricFormatter =
            signatureDescription.CreateFormatter(dsa);

        Console.WriteLine("\nCreated formatter : " + 
            asymmetricFormatter.ToString());
        return asymmetricFormatter;
    }

    // Create a signature deformatter for DSA decryption.
    private static AsymmetricSignatureDeformatter CreateDSADeformatter(
        DSA dsa)
    {
        // Create a DSA signature deformatter to verify the signature.
        SignatureDescription signatureDescription = 
            new SignatureDescription();
        signatureDescription.DeformatterAlgorithm =
            "System.Security.Cryptography.DSASignatureDeformatter";

        AsymmetricSignatureDeformatter asymmetricDeformatter =
            signatureDescription.CreateDeformatter(dsa);

        Console.WriteLine("\nCreated deformatter : " + 
            asymmetricDeformatter.ToString());
        return asymmetricDeformatter;
    }

    // Display to the console the properties of the specified
    // SignatureDescription.
    private static void ShowProperties(
        SignatureDescription signatureDescription)
    {
        // Retrieve the class path for the specified SignatureDescription.
        string classDescription = signatureDescription.ToString();

        string deformatterAlgorithm = 
            signatureDescription.DeformatterAlgorithm;
        string formatterAlgorithm = signatureDescription.FormatterAlgorithm;
        string digestAlgorithm = signatureDescription.DigestAlgorithm;
        string keyAlgorithm = signatureDescription.KeyAlgorithm;

        Console.WriteLine("\n** " + classDescription + " **");
        Console.WriteLine("DeformatterAlgorithm : " + deformatterAlgorithm);
        Console.WriteLine("FormatterAlgorithm : " + formatterAlgorithm);
        Console.WriteLine("DigestAlgorithm : " + digestAlgorithm);
        Console.WriteLine("KeyAlgorithm : " + keyAlgorithm);
    }
}
//
开发者ID:.NET开发者,项目名称:System.Security.Cryptography,代码行数:147,代码来源:SignatureDescription

输出:

** System.Security.Cryptography.SignatureDescription **
DeformatterAlgorithm : System.Security.Cryptography.
RSAPKCS1SignatureDeformatter

FormatterAlgorithm : System.Security.Cryptography.
RSAPKCS1SignatureFormatter
DigestAlgorithm : System.Security.Cryptography.SHA1CryptoServiceProvider
KeyAlgorithm : System.Security.Cryptography.RSACryptoServiceProvider

** System.Security.Cryptography.SignatureDescription **
DeformatterAlgorithm : System.Security.Cryptography.DSASignatureDeformatter
FormatterAlgorithm : System.Security.Cryptography.DSASignatureFormatter
DigestAlgorithm : System.Security.Cryptography.SHA1CryptoServiceProvider
KeyAlgorithm : System.Security.Cryptography.DSACryptoServiceProvider

Hash algorithm for the DigestAlgorithm property: 
System.Security.Cryptography.SH
A1CryptoServiceProvider

Created formatter : System.Security.Cryptography.DSASignatureFormatter

Created deformatter : System.Security.Cryptography.DSASignatureDeformatter
This sample completed successfully; press Enter to exit.



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# SymmetricAlgorithm类代码示例发布时间:2022-05-24
下一篇:
C# SHA512Managed类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap