ECDsaCng ECDsaCng ECDsaCng ECDsaCng Class

Definition

Provides a Cryptography Next Generation (CNG) implementation of the Elliptic Curve Digital Signature Algorithm (ECDSA).

public ref class ECDsaCng sealed : System::Security::Cryptography::ECDsa
public sealed class ECDsaCng : System.Security.Cryptography.ECDsa
type ECDsaCng = class
    inherit ECDsa
Public NotInheritable Class ECDsaCng
Inherits ECDsa
Inheritance

Examples

The following example shows how to use the ECDsaCng class to sign a message and how to use the signature to verify that the message has not been altered.

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;


class Alice
{
    public static void Main(string[] args)
    {
        Bob bob = new Bob();
        using (ECDsaCng dsa = new ECDsaCng())
        {
            dsa.HashAlgorithm = CngAlgorithm.Sha256;
             bob.key = dsa.Key.Export(CngKeyBlobFormat.EccPublicBlob);

             byte[] data = new byte[] { 21, 5, 8, 12, 207 };

             byte[] signature = dsa.SignData(data);

             bob.Receive(data, signature);
            }
    }


}
public class Bob 
{
    public byte[] key;

    public void Receive(byte[] data, byte[] signature)
    {
        using (ECDsaCng ecsdKey = new ECDsaCng(CngKey.Import(key, CngKeyBlobFormat.EccPublicBlob)))
        {
            if (ecsdKey.VerifyData(data, signature))
                Console.WriteLine("Data is good");
            else
                Console.WriteLine("Data is bad");
        }
    }
}
Imports System
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text

Class Alice

    Public Shared Sub Main(ByVal args() As String)
        Dim bob As New Bob()
        If (True) Then
            Using dsa As New ECDsaCng()
                    dsa.HashAlgorithm = CngAlgorithm.Sha256
                    bob.key = dsa.Key.Export(CngKeyBlobFormat.EccPublicBlob)
                    Dim data() As Byte = {21, 5, 8, 12, 207}
                    Dim signature As Byte() = dsa.SignData(data)
                    bob.Receive(data, signature)
            End Using
        End If

    End Sub 'Main
End Class 'Alice 


Public Class Bob
    Public key() As Byte

    Public Sub Receive(ByVal data() As Byte, ByVal signature() As Byte)
        Using ecsdKey As New ECDsaCng(CngKey.Import(key, CngKeyBlobFormat.EccPublicBlob))
                If ecsdKey.VerifyData(data, signature) Then
                    Console.WriteLine("Data is good")
                Else
                    Console.WriteLine("Data is bad")
                End If
        End Using

    End Sub 'Receive
End Class 'Bob 

Remarks

This class derives from the ECDsa abstract base class.

Constructors

ECDsaCng() ECDsaCng() ECDsaCng() ECDsaCng()

Initializes a new instance of the ECDsaCng class with a random key pair.

ECDsaCng(CngKey) ECDsaCng(CngKey) ECDsaCng(CngKey) ECDsaCng(CngKey)

Initializes a new instance of the ECDsaCng class by using the specified CngKey object.

ECDsaCng(ECCurve) ECDsaCng(ECCurve) ECDsaCng(ECCurve) ECDsaCng(ECCurve)

Initializes a new instance of the ECDsaCng class whose public/private key pair is generated over the specified curve.

ECDsaCng(Int32) ECDsaCng(Int32) ECDsaCng(Int32) ECDsaCng(Int32)

Initializes a new instance of the ECDsaCng class with a random key pair, using the specified key size.

Properties

HashAlgorithm HashAlgorithm HashAlgorithm HashAlgorithm

Gets or sets the hash algorithm to use when signing and verifying data.

Key Key Key Key

Gets or sets the key to use when signing and verifying data.

KeyExchangeAlgorithm KeyExchangeAlgorithm KeyExchangeAlgorithm KeyExchangeAlgorithm

Gets the name of the key exchange algorithm.

(Inherited from ECDsa)
KeySize KeySize KeySize KeySize
LegalKeySizes LegalKeySizes LegalKeySizes LegalKeySizes
SignatureAlgorithm SignatureAlgorithm SignatureAlgorithm SignatureAlgorithm

Gets the name of the signature algorithm.

(Inherited from ECDsa)

Methods

Clear() Clear() Clear() Clear()

Releases all resources used by the AsymmetricAlgorithm class.

(Inherited from AsymmetricAlgorithm)
Dispose() Dispose() Dispose() Dispose()

Releases all resources used by the current instance of the AsymmetricAlgorithm class.

(Inherited from AsymmetricAlgorithm)
Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

Releases the unmanaged resources used by the AsymmetricAlgorithm class and optionally releases the managed resources.

(Inherited from AsymmetricAlgorithm)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
ExportExplicitParameters(Boolean) ExportExplicitParameters(Boolean) ExportExplicitParameters(Boolean) ExportExplicitParameters(Boolean)

Exports the key and explicit curve parameters used by the Elliptic curve cryptography (ECC) object into an ECParameters object.

ExportParameters(Boolean) ExportParameters(Boolean) ExportParameters(Boolean) ExportParameters(Boolean)

Exports the key used by the Elliptic curve cryptography (ECC) object into an ECParameters object. If the key was created as a named curve, the Curve field contains named curve parameters; otherwise, it contains explicit parameters.

FromXmlString(String) FromXmlString(String) FromXmlString(String) FromXmlString(String)

This method is not implemented.

FromXmlString(String, ECKeyXmlFormat) FromXmlString(String, ECKeyXmlFormat) FromXmlString(String, ECKeyXmlFormat) FromXmlString(String, ECKeyXmlFormat)

Deserializes the key information from an XML string by using the specified format.

GenerateKey(ECCurve) GenerateKey(ECCurve) GenerateKey(ECCurve) GenerateKey(ECCurve)

Generates a key to use for the ECDsaCng algorithm.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
HashData(Byte[], Int32, Int32, HashAlgorithmName) HashData(Byte[], Int32, Int32, HashAlgorithmName) HashData(Byte[], Int32, Int32, HashAlgorithmName) HashData(Byte[], Int32, Int32, HashAlgorithmName)

When overridden in a derived class, computes the hash value of the specified portion of a byte array by using the specified hashing algorithm.

(Inherited from ECDsa)
HashData(Stream, HashAlgorithmName) HashData(Stream, HashAlgorithmName) HashData(Stream, HashAlgorithmName) HashData(Stream, HashAlgorithmName)

When overridden in a derived class, computes the hash value of the specified binary stream by using the specified hashing algorithm.

(Inherited from ECDsa)
ImportParameters(ECParameters) ImportParameters(ECParameters) ImportParameters(ECParameters) ImportParameters(ECParameters)

Replaces the existing key that the current instance is working with by creating a new CngKey for the parameters structure.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
SignData(Byte[]) SignData(Byte[]) SignData(Byte[]) SignData(Byte[])

Generates a signature for the specified data.

SignData(Byte[], HashAlgorithmName) SignData(Byte[], HashAlgorithmName) SignData(Byte[], HashAlgorithmName) SignData(Byte[], HashAlgorithmName)

Computes the hash value of the specified byte array using the specified hash algorithm and signs the resulting hash value.

(Inherited from ECDsa)
SignData(Byte[], Int32, Int32) SignData(Byte[], Int32, Int32) SignData(Byte[], Int32, Int32) SignData(Byte[], Int32, Int32)

Generates a digital signature for the specified length of data, beginning at the specified offset.

SignData(Byte[], Int32, Int32, HashAlgorithmName) SignData(Byte[], Int32, Int32, HashAlgorithmName) SignData(Byte[], Int32, Int32, HashAlgorithmName) SignData(Byte[], Int32, Int32, HashAlgorithmName)

Computes the hash value of a portion of the specified byte array using the specified hash algorithm and signs the resulting hash value.

(Inherited from ECDsa)
SignData(Stream) SignData(Stream) SignData(Stream) SignData(Stream)

Generates a signature for the specified data stream, reading to the end of the stream.

SignData(Stream, HashAlgorithmName) SignData(Stream, HashAlgorithmName) SignData(Stream, HashAlgorithmName) SignData(Stream, HashAlgorithmName)

Computes the hash value of the specified stream using the specified hash algorithm and signs the resulting hash value.

(Inherited from ECDsa)
SignHash(Byte[]) SignHash(Byte[]) SignHash(Byte[]) SignHash(Byte[])

Generates a signature for the specified hash value.

ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)
ToXmlString(Boolean) ToXmlString(Boolean) ToXmlString(Boolean) ToXmlString(Boolean)

This method is not implemented.

ToXmlString(ECKeyXmlFormat) ToXmlString(ECKeyXmlFormat) ToXmlString(ECKeyXmlFormat) ToXmlString(ECKeyXmlFormat)

Serializes the key information to an XML string by using the specified format.

TryHashData(ReadOnlySpan<Byte>, Span<Byte>, HashAlgorithmName, Int32) TryHashData(ReadOnlySpan<Byte>, Span<Byte>, HashAlgorithmName, Int32) TryHashData(ReadOnlySpan<Byte>, Span<Byte>, HashAlgorithmName, Int32) TryHashData(ReadOnlySpan<Byte>, Span<Byte>, HashAlgorithmName, Int32) Inherited from ECDsa
TrySignData(ReadOnlySpan<Byte>, Span<Byte>, HashAlgorithmName, Int32) TrySignData(ReadOnlySpan<Byte>, Span<Byte>, HashAlgorithmName, Int32) TrySignData(ReadOnlySpan<Byte>, Span<Byte>, HashAlgorithmName, Int32) TrySignData(ReadOnlySpan<Byte>, Span<Byte>, HashAlgorithmName, Int32) Inherited from ECDsa
TrySignHash(ReadOnlySpan<Byte>, Span<Byte>, Int32) TrySignHash(ReadOnlySpan<Byte>, Span<Byte>, Int32) TrySignHash(ReadOnlySpan<Byte>, Span<Byte>, Int32) TrySignHash(ReadOnlySpan<Byte>, Span<Byte>, Int32) Inherited from ECDsa
VerifyData(Byte[], Byte[]) VerifyData(Byte[], Byte[]) VerifyData(Byte[], Byte[]) VerifyData(Byte[], Byte[])

Verifies the digital signature of the specified data.

VerifyData(Byte[], Byte[], HashAlgorithmName) VerifyData(Byte[], Byte[], HashAlgorithmName) VerifyData(Byte[], Byte[], HashAlgorithmName) VerifyData(Byte[], Byte[], HashAlgorithmName)

Verifies that a digital signature is valid by calculating the hash value of the specified data using the specified hash algorithm and comparing it to the provided signature.

(Inherited from ECDsa)
VerifyData(Byte[], Int32, Int32, Byte[]) VerifyData(Byte[], Int32, Int32, Byte[]) VerifyData(Byte[], Int32, Int32, Byte[]) VerifyData(Byte[], Int32, Int32, Byte[])

Verifies a signature for the specified length of data, beginning at the specified offset.

VerifyData(Byte[], Int32, Int32, Byte[], HashAlgorithmName) VerifyData(Byte[], Int32, Int32, Byte[], HashAlgorithmName) VerifyData(Byte[], Int32, Int32, Byte[], HashAlgorithmName) VerifyData(Byte[], Int32, Int32, Byte[], HashAlgorithmName)

Verifies that a digital signature is valid by calculating the hash value of the data in a portion of a byte array using the specified hash algorithm and comparing it to the provided signature.

(Inherited from ECDsa)
VerifyData(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, HashAlgorithmName) VerifyData(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, HashAlgorithmName) VerifyData(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, HashAlgorithmName) VerifyData(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, HashAlgorithmName) Inherited from ECDsa
VerifyData(Stream, Byte[]) VerifyData(Stream, Byte[]) VerifyData(Stream, Byte[]) VerifyData(Stream, Byte[])

Verifies the digital signature of the specified data stream, reading to the end of the stream.

VerifyData(Stream, Byte[], HashAlgorithmName) VerifyData(Stream, Byte[], HashAlgorithmName) VerifyData(Stream, Byte[], HashAlgorithmName) VerifyData(Stream, Byte[], HashAlgorithmName)

Verifies that a digital signature is valid by calculating the hash value of the specified stream using the specified hash algorithm and comparing it to the provided signature.

(Inherited from ECDsa)
VerifyHash(Byte[], Byte[]) VerifyHash(Byte[], Byte[]) VerifyHash(Byte[], Byte[]) VerifyHash(Byte[], Byte[])

Verifies the specified digital signature against a specified hash value.

VerifyHash(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>) VerifyHash(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>) VerifyHash(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>) VerifyHash(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>) Inherited from ECDsa

Fields

KeySizeValue KeySizeValue KeySizeValue KeySizeValue

Represents the size, in bits, of the key modulus used by the asymmetric algorithm.

(Inherited from AsymmetricAlgorithm)
LegalKeySizesValue LegalKeySizesValue LegalKeySizesValue LegalKeySizesValue

Specifies the key sizes that are supported by the asymmetric algorithm.

(Inherited from AsymmetricAlgorithm)

Explicit Interface Implementations

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose()

For a description of this member, see Dispose().

(Inherited from AsymmetricAlgorithm)

Applies to