Skip to content

Commit 1bfb29c

Browse files
nschonniThraka
authored andcommitted
typo: signture -> signiture (#11168)
Fix code fence start/stop as well
1 parent 89b2ffd commit 1bfb29c

File tree

1 file changed

+149
-144
lines changed

1 file changed

+149
-144
lines changed

docs/standard/security/cryptographic-signatures.md

Lines changed: 149 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
title: "Cryptographic Signatures"
33
ms.date: "03/30/2017"
44
ms.technology: dotnet-standard
5-
dev_langs:
5+
dev_langs:
66
- "csharp"
77
- "vb"
8-
helpviewer_keywords:
8+
helpviewer_keywords:
99
- "digital signatures"
1010
- "cryptography [.NET Framework], signatures"
1111
- "digital signatures, XML signing"
@@ -23,149 +23,154 @@ ms.assetid: aa87cb7f-e608-4a81-948b-c9b8a1225783
2323
author: "mairaw"
2424
ms.author: "mairaw"
2525
---
26+
2627
# Cryptographic Signatures
27-
<a name="top"></a> Cryptographic digital signatures use public key algorithms to provide data integrity. When you sign data with a digital signature, someone else can verify the signature, and can prove that the data originated from you and was not altered after you signed it. For more information about digital signatures, see [Cryptographic Services](../../../docs/standard/security/cryptographic-services.md).
28-
29-
This topic explains how to generate and verify digital signatures using classes in the <xref:System.Security.Cryptography?displayProperty=nameWithType> namespace.
30-
31-
- [Generating Signatures](#generate)
32-
33-
- [Verifying Signatures](#verify)
34-
35-
<a name="generate"></a>
36-
## Generating Signatures
37-
Digital signatures are usually applied to hash values that represent larger data. The following example applies a digital signature to a hash value. First, a new instance of the <xref:System.Security.Cryptography.RSACryptoServiceProvider> class is created to generate a public/private key pair. Next, the <xref:System.Security.Cryptography.RSACryptoServiceProvider> is passed to a new instance of the <xref:System.Security.Cryptography.RSAPKCS1SignatureFormatter> class. This transfers the private key to the <xref:System.Security.Cryptography.RSAPKCS1SignatureFormatter>, which actually performs the digital signing. Before you can sign the hash code, you must specify a hash algorithm to use. This example uses the SHA1 algorithm. Finally, the <xref:System.Security.Cryptography.AsymmetricSignatureFormatter.CreateSignature%2A> method is called to perform the signing.
38-
39-
```vb
40-
Imports System
41-
Imports System.Security.Cryptography
42-
43-
Module Module1
44-
Sub Main()
45-
'The hash value to sign.
46-
Dim hashValue As Byte() = {59, 4, 248, 102, 77, 97, 142, 201, 210, 12, 224, 93, 25, 41, 100, 197, 213, 134, 130, 135}
47-
48-
'The value to hold the signed value.
49-
Dim signedHashValue() As Byte
50-
51-
'Generate a public/private key pair.
52-
Dim rsa As New RSACryptoServiceProvider()
53-
54-
'Create an RSAPKCS1SignatureFormatter object and pass it
55-
'the RSACryptoServiceProvider to transfer the private key.
56-
Dim rsaFormatter As New RSAPKCS1SignatureFormatter(rsa)
57-
58-
'Set the hash algorithm to SHA1.
59-
rsaFormatter.SetHashAlgorithm("SHA1")
60-
61-
'Create a signature for hashValue and assign it to
62-
'signedHashValue.
63-
signedHashValue = rsaFormatter.CreateSignature(hashValue)
64-
End Sub
65-
End Module
66-
67-
using System;
68-
using System.Security.Cryptography;
69-
```
70-
71-
```csharp
72-
class Class1
73-
{
74-
static void Main()
75-
{
76-
//The hash value to sign.
77-
byte[] hashValue = {59,4,248,102,77,97,142,201,210,12,224,93,25,41,100,197,213,134,130,135};
78-
79-
//The value to hold the signed value.
80-
byte[] signedHashValue;
81-
82-
//Generate a public/private key pair.
83-
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
84-
85-
//Create an RSAPKCS1SignatureFormatter object and pass it the
86-
//RSACryptoServiceProvider to transfer the private key.
87-
RSAPKCS1SignatureFormatter rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
88-
89-
//Set the hash algorithm to SHA1.
90-
rsaFormatter.SetHashAlgorithm("SHA1");
91-
92-
//Create a signature for hashValue and assign it to
93-
//signedHashValue.
94-
signedHashValue = rsaFormatter.CreateSignature(hashValue);
95-
}
96-
}
97-
```
98-
99-
### Signing XML Files
100-
The .NET Framework provides the <xref:System.Security.Cryptography.Xml> namespace, which enables you sign XML. Signing XML is important when you want to verify that the XML originates from a certain source. For example, if you are using a stock quote service that uses XML, you can verify the source of the XML if it is signed.
101-
102-
The classes in this namespace follow the [XML-Signature Syntax and Processing recommendation](https://www.w3.org/TR/xmldsig-core/) from the World Wide Web Consortium.
103-
104-
[Back to top](#top)
105-
106-
<a name="verify"></a>
107-
## Verifying Signatures
108-
To verify that data was signed by a particular party, you must have the following information:
109-
110-
- The public key of the party that signed the data.
111-
112-
- The digital signature.
113-
114-
- The data that was signed.
115-
116-
- The hash algorithm used by the signer.
117-
118-
To verify a signature signed by the <xref:System.Security.Cryptography.RSAPKCS1SignatureFormatter> class, use the <xref:System.Security.Cryptography.RSAPKCS1SignatureDeformatter> class. The <xref:System.Security.Cryptography.RSAPKCS1SignatureDeformatter> class must be supplied the public key of the signer. You will need the values of the modulus and the exponent to specify the public key. (The party that generated the public/private key pair should provide these values.) First create an <xref:System.Security.Cryptography.RSACryptoServiceProvider> object to hold the public key that will verify the signature, and then initialize an <xref:System.Security.Cryptography.RSAParameters> structure to the modulus and exponent values that specify the public key.
119-
120-
The following code shows the creation of an <xref:System.Security.Cryptography.RSAParameters> structure. The `Modulus` property is set to the value of a byte array called `modulusData` and the `Exponent` property is set to the value of a byte array called `exponentData`.
121-
122-
```vb
123-
Dim rsaKeyInfo As RSAParameters
124-
rsaKeyInfo.Modulus = modulusData
125-
rsaKeyInfo.Exponent = exponentData
126-
```
127-
128-
```csharp
129-
RSAParameters rsaKeyInfo;
130-
rsaKeyInfo.Modulus = modulusData;
131-
rsaKeyInfo.Exponent = exponentData;
132-
```
133-
134-
After you have created the <xref:System.Security.Cryptography.RSAParameters> object, you can initialize a new instance of the <xref:System.Security.Cryptography.RSACryptoServiceProvider> class to the values specified in <xref:System.Security.Cryptography.RSAParameters>. The <xref:System.Security.Cryptography.RSACryptoServiceProvider> is, in turn, passed to the constructor of an <xref:System.Security.Cryptography.RSAPKCS1SignatureDeformatter> to transfer the key.
135-
136-
The following example illustrates this process. In this example, `hashValue` and `signedHashValue` are arrays of bytes provided by a remote party. The remote party has signed the `hashValue` using the SHA1 algorithm, producing the digital signature `signedHashValue`. The
137-
138-
<xref:System.Security.Cryptography.RSAPKCS1SignatureDeformatter.VerifySignature%2A?displayProperty=nameWithType> method verifies that the digital signature is valid and was used to sign the `hashValue`.
139-
140-
```vb
141-
Dim rsa As New RSACryptoServiceProvider()
142-
rsa.ImportParameters(rsaKeyInfo)
143-
Dim rsaDeformatter As New RSAPKCS1SignatureDeformatter(rsa)
144-
rsaDeformatter.SetHashAlgorithm("SHA1")
145-
If rsaDeformatter.VerifySignature(hashValue, signedHashValue) Then
146-
Console.WriteLine("The signature is valid.")
147-
Else
148-
Console.WriteLine("The signture is not valid.")
149-
End If
150-
```
151-
152-
```csharp
153-
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
154-
rsa.ImportParameters(rsaKeyInfo);
155-
RSAPKCS1SignatureDeformatter rsaDeformatter = new RSAPKCS1SignatureDeformatter(rsa);
156-
rsaDeformatter.SetHashAlgorithm("SHA1");
157-
if(rsaDeformatter.VerifySignature(hashValue, signedHashValue))
158-
{
159-
Console.WriteLine("The signature is valid.");
160-
}
161-
else
162-
{
163-
Console.WriteLine("The signature is not valid.");
164-
}
165-
```
166-
167-
This code fragment will display "`The signature is valid`" if the signature is valid and "`The signature is not valid`" if it is not.
168-
28+
29+
<a name="top"></a> Cryptographic digital signatures use public key algorithms to provide data integrity. When you sign data with a digital signature, someone else can verify the signature, and can prove that the data originated from you and was not altered after you signed it. For more information about digital signatures, see [Cryptographic Services](../../../docs/standard/security/cryptographic-services.md).
30+
31+
This topic explains how to generate and verify digital signatures using classes in the <xref:System.Security.Cryptography?displayProperty=nameWithType> namespace.
32+
33+
- [Generating Signatures](#generate)
34+
35+
- [Verifying Signatures](#verify)
36+
37+
<a name="generate"></a>
38+
39+
## Generating Signatures
40+
41+
Digital signatures are usually applied to hash values that represent larger data. The following example applies a digital signature to a hash value. First, a new instance of the <xref:System.Security.Cryptography.RSACryptoServiceProvider> class is created to generate a public/private key pair. Next, the <xref:System.Security.Cryptography.RSACryptoServiceProvider> is passed to a new instance of the <xref:System.Security.Cryptography.RSAPKCS1SignatureFormatter> class. This transfers the private key to the <xref:System.Security.Cryptography.RSAPKCS1SignatureFormatter>, which actually performs the digital signing. Before you can sign the hash code, you must specify a hash algorithm to use. This example uses the SHA1 algorithm. Finally, the <xref:System.Security.Cryptography.AsymmetricSignatureFormatter.CreateSignature%2A> method is called to perform the signing.
42+
43+
```vb
44+
Imports System
45+
Imports System.Security.Cryptography
46+
47+
Module Module1
48+
Sub Main()
49+
'The hash value to sign.
50+
Dim hashValue As Byte() = {59, 4, 248, 102, 77, 97, 142, 201, 210, 12, 224, 93, 25, 41, 100, 197, 213, 134, 130, 135}
51+
52+
'The value to hold the signed value.
53+
Dim signedHashValue() As Byte
54+
55+
'Generate a public/private key pair.
56+
Dim rsa As New RSACryptoServiceProvider()
57+
58+
'Create an RSAPKCS1SignatureFormatter object and pass it
59+
'the RSACryptoServiceProvider to transfer the private key.
60+
Dim rsaFormatter As New RSAPKCS1SignatureFormatter(rsa)
61+
62+
'Set the hash algorithm to SHA1.
63+
rsaFormatter.SetHashAlgorithm("SHA1")
64+
65+
'Create a signature for hashValue and assign it to
66+
'signedHashValue.
67+
signedHashValue = rsaFormatter.CreateSignature(hashValue)
68+
End Sub
69+
End Module
70+
```
71+
72+
```csharp
73+
using System;
74+
using System.Security.Cryptography;
75+
76+
class Class1
77+
{
78+
static void Main()
79+
{
80+
//The hash value to sign.
81+
byte[] hashValue = {59,4,248,102,77,97,142,201,210,12,224,93,25,41,100,197,213,134,130,135};
82+
83+
//The value to hold the signed value.
84+
byte[] signedHashValue;
85+
86+
//Generate a public/private key pair.
87+
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
88+
89+
//Create an RSAPKCS1SignatureFormatter object and pass it the
90+
//RSACryptoServiceProvider to transfer the private key.
91+
RSAPKCS1SignatureFormatter rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
92+
93+
//Set the hash algorithm to SHA1.
94+
rsaFormatter.SetHashAlgorithm("SHA1");
95+
96+
//Create a signature for hashValue and assign it to
97+
//signedHashValue.
98+
signedHashValue = rsaFormatter.CreateSignature(hashValue);
99+
}
100+
}
101+
```
102+
103+
### Signing XML Files
104+
105+
The .NET Framework provides the <xref:System.Security.Cryptography.Xml> namespace, which enables you sign XML. Signing XML is important when you want to verify that the XML originates from a certain source. For example, if you are using a stock quote service that uses XML, you can verify the source of the XML if it is signed.
106+
107+
The classes in this namespace follow the [XML-Signature Syntax and Processing recommendation](https://www.w3.org/TR/xmldsig-core/) from the World Wide Web Consortium.
108+
109+
[Back to top](#top)
110+
111+
<a name="verify"></a>
112+
113+
## Verifying Signatures
114+
115+
To verify that data was signed by a particular party, you must have the following information:
116+
117+
- The public key of the party that signed the data.
118+
119+
- The digital signature.
120+
121+
- The data that was signed.
122+
123+
- The hash algorithm used by the signer.
124+
125+
To verify a signature signed by the <xref:System.Security.Cryptography.RSAPKCS1SignatureFormatter> class, use the <xref:System.Security.Cryptography.RSAPKCS1SignatureDeformatter> class. The <xref:System.Security.Cryptography.RSAPKCS1SignatureDeformatter> class must be supplied the public key of the signer. You will need the values of the modulus and the exponent to specify the public key. (The party that generated the public/private key pair should provide these values.) First create an <xref:System.Security.Cryptography.RSACryptoServiceProvider> object to hold the public key that will verify the signature, and then initialize an <xref:System.Security.Cryptography.RSAParameters> structure to the modulus and exponent values that specify the public key.
126+
127+
The following code shows the creation of an <xref:System.Security.Cryptography.RSAParameters> structure. The `Modulus` property is set to the value of a byte array called `modulusData` and the `Exponent` property is set to the value of a byte array called `exponentData`.
128+
129+
```vb
130+
Dim rsaKeyInfo As RSAParameters
131+
rsaKeyInfo.Modulus = modulusData
132+
rsaKeyInfo.Exponent = exponentData
133+
```
134+
135+
```csharp
136+
RSAParameters rsaKeyInfo;
137+
rsaKeyInfo.Modulus = modulusData;
138+
rsaKeyInfo.Exponent = exponentData;
139+
```
140+
141+
After you have created the <xref:System.Security.Cryptography.RSAParameters> object, you can initialize a new instance of the <xref:System.Security.Cryptography.RSACryptoServiceProvider> class to the values specified in <xref:System.Security.Cryptography.RSAParameters>. The <xref:System.Security.Cryptography.RSACryptoServiceProvider> is, in turn, passed to the constructor of an <xref:System.Security.Cryptography.RSAPKCS1SignatureDeformatter> to transfer the key.
142+
143+
The following example illustrates this process. In this example, `hashValue` and `signedHashValue` are arrays of bytes provided by a remote party. The remote party has signed the `hashValue` using the SHA1 algorithm, producing the digital signature `signedHashValue`. The <xref:System.Security.Cryptography.RSAPKCS1SignatureDeformatter.VerifySignature%2A?displayProperty=nameWithType> method verifies that the digital signature is valid and was used to sign the `hashValue`.
144+
145+
```vb
146+
Dim rsa As New RSACryptoServiceProvider()
147+
rsa.ImportParameters(rsaKeyInfo)
148+
Dim rsaDeformatter As New RSAPKCS1SignatureDeformatter(rsa)
149+
rsaDeformatter.SetHashAlgorithm("SHA1")
150+
If rsaDeformatter.VerifySignature(hashValue, signedHashValue) Then
151+
Console.WriteLine("The signature is valid.")
152+
Else
153+
Console.WriteLine("The signature is not valid.")
154+
End If
155+
```
156+
157+
```csharp
158+
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
159+
rsa.ImportParameters(rsaKeyInfo);
160+
RSAPKCS1SignatureDeformatter rsaDeformatter = new RSAPKCS1SignatureDeformatter(rsa);
161+
rsaDeformatter.SetHashAlgorithm("SHA1");
162+
if(rsaDeformatter.VerifySignature(hashValue, signedHashValue))
163+
{
164+
Console.WriteLine("The signature is valid.");
165+
}
166+
else
167+
{
168+
Console.WriteLine("The signature is not valid.");
169+
}
170+
```
171+
172+
This code fragment will display "`The signature is valid`" if the signature is valid and "`The signature is not valid`" if it is not.
173+
169174
## See also
170175

171176
- [Cryptographic Services](../../../docs/standard/security/cryptographic-services.md)

0 commit comments

Comments
 (0)