. Net (C #): Use the asypolicricsignatureformatter class and asypolicricsignaturedeformatter class.

Source: Internet
Author: User

In the previous articleArticle:. Net (C #): The signhash and verifyhash of RSA. I mentioned that using signhash and verifyhash of RSA can also achieve signdata and verifydata. In fact, there is also a method for signature authentication of public key encryption. Is asypolicricsignatureformatter and asypolicricsignaturedeformatter in. net. Direct translation is "asymmetric encryption signature formatter and anti-Formatter ". It is clearly used to create a signature and verify the signature.

 

They all need to operate on an asymmetric encryption type (that is, the public key encryption type: asypolicricalgorithm type in. net ). To set this type, you need to use setkey and sethashalgorithm to respectively set the public key encryption key and calculate the signature hash.Algorithm. In fact, the asyuncricalgorithm class does not have the key attribute as the aggregricalgorithm class does. Setkey execution usually assigns an asyuncricalgorithm object to a field in formatter or deformatter.

 

After setting the corresponding public key encryption object and hash algorithm, you can use createsignature or verifysignature to create or verify the signature.

Currently,. NET Framework (. NET 4) has rsapkcs1signature (formatter and deformatter) for RSA and dsasignature (formatter and deformatter) for DSA.

 

Note that the sethashalgorithm method parameter does not need to be in the oId format, while the signhash/verifyhash method requires oid.

ViewCode(Using RSA as an example): Use the asymmetricsignatureformatter class to compare the asymmetricsignaturedeformatter class with signhash and verifyhash. Both outputs the same result.

// + Using system. Security. Cryptography

 

// Create RSA and sha1

Using(VaRRSA= New Rsacryptoserviceprovider())

Using(VaRSha= Sha1.Create ())

{

// Data Hash Value

VaRHash=Sha.Computehash (New Byte[] {1,2,3});

// Create the sha1 OID

VaROid= New Oid("Sha1");

 

// Use signhash

VaRSig1=RSA.Signhash (hash, oId.Friendlyname );

// Output 1

Console.Writeline (Bitconverter.Tostring (sig1 ));

 

// Use asypolicricsignatureformatter (rsapkcs1signatureformatter). createsignature

VaRFMT= New Rsapkcs1signatureformatter();

FMT.Setkey (RSA );

FMT.Sethashalgorithm ("Sha1");

VaRSig2=FMT.Createsignature (hash );

 

// Output 2

Console.Writeline (Bitconverter.Tostring (sig2 ));

 

// Use verifyhash

// Output 3

Console.Writeline (RSA.Verifyhash (hash, oId.Friendlyname, sig1 ));

 

 

// Use asypolicricsignaturedeformatter (rsapkcs1signaturedeformatter). verifysignature

VaRDefmt= New Rsapkcs1signaturedeformatter();

Defmt.Setkey (RSA );

Defmt.Sethashalgorithm ("Sha1");

// Output 4

Console.Writeline (defmt.Verifysignature (hash, sig2 ));

}

 

Output:

Bytes

AF-60-71-16-61-71-5F-60-65-8A-17-C9-D3-A2-CE-EB-A3-FE-58-0F-45-98-07-62-71-18-A

Slave-

Bytes

-Timeout

 

Bytes

AF-60-71-16-61-71-5F-60-65-8A-17-C9-D3-A2-CE-EB-A3-FE-58-0F-45-98-07-62-71-18-A

Slave-

Bytes

-Timeout

 

True

True

The result is correct!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.