This topic has not yet been rated - Rate this topic

ECDsaCng Class

Updated: March 2011

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

Namespace:  System.Security.Cryptography
Assembly:  System.Core (in System.Core.dll)
[HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
public sealed class ECDsaCng : ECDsa

The ECDsaCng type exposes the following members.

  Name Description
Public method ECDsaCng() Initializes a new instance of the ECDsaCng class with a random key pair.
Public method ECDsaCng(CngKey) Initializes a new instance of the ECDsaCng class by using the specified CngKey object.
Public method ECDsaCng(Int32) Initializes a new instance of the ECDsaCng class with a random key pair, using the specified key size.
Top
  Name Description
Public property HashAlgorithm Gets or sets the hash algorithm to use when signing and verifying data.
Public property Key Gets or sets the key to use when signing and verifying data.
Public property KeyExchangeAlgorithm Gets the name of the key exchange algorithm. (Inherited from ECDsa.)
Public property KeySize Gets or sets the size, in bits, of the key modulus used by the asymmetric algorithm. (Inherited from AsymmetricAlgorithm.)
Public property LegalKeySizes Gets the key sizes that are supported by the asymmetric algorithm. (Inherited from AsymmetricAlgorithm.)
Public property SignatureAlgorithm Gets the name of the signature algorithm. (Inherited from ECDsa.)
Top
  Name Description
Public method Clear Releases all resources used by the AsymmetricAlgorithm class. (Inherited from AsymmetricAlgorithm.)
Public method Dispose() Releases all resources used by the current instance of the AsymmetricAlgorithm class. (Inherited from AsymmetricAlgorithm.)
Protected method Dispose(Boolean) Releases the unmanaged resources used by the AsymmetricAlgorithm class and optionally releases the managed resources. (Inherited from AsymmetricAlgorithm.)
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method FromXmlString(String) This method is not implemented. (Overrides AsymmetricAlgorithm.FromXmlString(String).)
Public method FromXmlString(String, ECKeyXmlFormat) Deserializes the key information from an XML string by using the specified format.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method SignData(Byte[]) Generates a signature for the specified data.
Public method SignData(Stream) Generates a signature for the specified data stream, reading to the end of the stream.
Public method SignData(Byte[], Int32, Int32) Generates a digital signature for the specified length of data, beginning at the specified offset.
Public method SignHash Generates a signature for the specified hash value. (Overrides ECDsa.SignHash(Byte[]).)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method ToXmlString(Boolean) This method is not implemented. (Overrides AsymmetricAlgorithm.ToXmlString(Boolean).)
Public method ToXmlString(ECKeyXmlFormat) Serializes the key information to an XML string by using the specified format.
Public method VerifyData(Byte[], Byte[]) Verifies the digital signature of the specified data.
Public method VerifyData(Stream, Byte[]) Verifies the digital signature of the specified data stream, reading to the end of the stream.
Public method VerifyData(Byte[], Int32, Int32, Byte[]) Verifies a signature for the specified length of data, beginning at the specified offset.
Public method VerifyHash Verifies the specified digital signature against a specified hash value. (Overrides ECDsa.VerifyHash(Byte[], Byte[]).)
Top
  Name Description
Protected field KeySizeValue Represents the size, in bits, of the key modulus used by the asymmetric algorithm. (Inherited from AsymmetricAlgorithm.)
Protected field LegalKeySizesValue Specifies the key sizes that are supported by the asymmetric algorithm. (Inherited from AsymmetricAlgorithm.)
Top

This class derives from the ECDsa abstract base class.

Note Note

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: MayLeakOnAbort. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

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");
        }
    }
}


.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Date

History

Reason

March 2011

Added example.

Information enhancement.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ