RSASignature

Namespace: Babel.Licensing Assembly: Babel.Licensing.dll

Creates and verifies digital signature using RSA-SH1 algorithm. This class cannot be inherited.

public sealed class RSASignature : ISignatureProvider, ILicenseSerializable, ISignatureWriter, IRSAKeys, ISignatureKeyInfo

Inheritance

objectRSASignature

Implements

ISignatureProvider, ILicenseSerializable, ISignatureWriter, IRSAKeys, ISignatureKeyInfo

Inherited Members

object.GetType(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

Constructors

RSASignature(RSA)

Initializes a new instance of the class with the specified RSA algorithm.

public RSASignature(RSA rsa)

Parameters

NameDescription

rsa RSA

The RSA algorithm.

Exceptions

NameDescription

Thrown when the rsa argument is null.

RSASignature(RSA, bool)

Initializes a new instance of the class with the specified RSA algorithm and a flag which specify to whether to add public key information to the signature.

public RSASignature(RSA rsa, bool generateKeyInfo)

Parameters

NameDescription

rsa RSA

The RSA algorithm.

generateKeyInfo bool

true to add public key information to signature element, false if not.

Remarks

If the public key information is not added to the signature, the RSA public key must be provided before calling the method VerifyData.

RSASignature(int)

Initializes a new instance of the class with the specified RSA key size.

public RSASignature(int keySize)

Parameters

NameDescription

keySize int

Size of the RSA key.

Examples

// Sign data using RSA algorithm with 768 bit key size
RSASignature signer = new RSASignature(768);

// A private key is needed to sign data
signer.CreateKeyPair();

byte[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
byte[] signature = signer.SignData(data);

// Create a new RSASignature with the public key needed 
// to verify the signature
string publicKey = signer.ExportKeys(true);
RSASignature verifier = RSASignature.FromKeys(publicKey);

// Returns true
bool result = verifier.VerifyData(data, signature);

RSASignature()

Initializes a new instance of the class.

public RSASignature()

Examples

// Sign data using RSA algorithm with 1024 bit key size
RSASignature signer = new RSASignature();

// A private key is needed to sign data
signer.CreateKeyPair();

byte[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
byte[] signature = signer.SignData(data);

// Create a new RSASignature with the public key needed 
// to verify the signature
string publicKey = signer.ExportKeys(true);
RSASignature verifier = RSASignature.FromKeys(publicKey);

// Returns true
bool result = verifier.VerifyData(data, signature);

Properties

AlgorithmName

Gets the name of the sign algorithm.

public string AlgorithmName { get; }

Property Value

string

GenerateKeyInfo

Gets or sets a value indicating whether the generate key information.

public bool GenerateKeyInfo { get; set; }

Property Value

bool

HasKeySize

Gets a value indicating whether this object has key size.

public bool HasKeySize { get; }

Property Value

bool

HasPrivateKey

Gets a value indicating whether this object has private key.

public bool HasPrivateKey { get; }

Property Value

bool

HasPublicKey

Gets a value indicating whether this object has public key.

public bool HasPublicKey { get; }

Property Value

bool

KeyPair

Gets or sets the RSA key pair.

public byte[] KeyPair { get; set; }

Property Value

byte[]

KeySize

Gets or sets the RSA key size.

public int KeySize { get; set; }

Property Value

int

RSA

Gets or sets the RSA key pair.

public RSA RSA { get; set; }

Property Value

RSA

Methods

CreateFromKeyContainer(string)

Creates an instance of from the specified key container.

public static RSASignature CreateFromKeyContainer(string container)

Parameters

NameDescription

container string

The key container name.

Returns

NameDescription

The created object.

Exceptions

NameDescription

Thrown when the container argument is null.

CreateFromKeyFile(string, string)

Creates an instance of from the specified key pair file.

public static RSASignature CreateFromKeyFile(string keyFile, string password = null)

Parameters

NameDescription

keyFile string

The key pair file path. This can be an SNK or PFX file.

password string

(Optional) the key password.

Returns

NameDescription

The created object.

Exceptions

NameDescription

Thrown when the keyFile argument is null.

CreateFromRawData(byte[], string)

Creates an instance of from the specified key buffer.

public static RSASignature CreateFromRawData(byte[] keyData, string password = null)

Parameters

NameDescription

keyData byte[]

The key pair buffer. This can be an SNK or PFX key buffer.

password string

(Optional) the key password.

Returns

NameDescription

The created object.

Exceptions

NameDescription

Thrown when the keyData argument is null.

CreateKeyPair()

Creates a new key pair.

public void CreateKeyPair()

See Also

RSASignature

ExportKeys(bool)

Export RSA keys as base 64 encoded string.

public string ExportKeys(bool publicKeyOnly)

Parameters

NameDescription

publicKeyOnly bool

Whether to write only public key data.

Returns

NameDescription

The base 64 encoded RSA key.

FromKeys(string)

Creates an object from the given public and private keys.

public static RSASignature FromKeys(string rsaKey)

Parameters

NameDescription

rsaKey string

The encoded string RSA key.

Returns

NameDescription

An instance of RSASignature.

Examples

// Sign data using RSA algorithm with 768 bit key size
RSASignature signer = new RSASignature(768);

// A private key is needed to sign data
signer.CreateKeyPair();

byte[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
byte[] signature = signer.SignData(data);

// Create a new RSASignature with the public key needed 
// to verify the signature
string publicKey = signer.ExportKeys(true);
RSASignature verifier = RSASignature.FromKeys(publicKey);

// Returns true
bool result = verifier.VerifyData(data, signature);

Exceptions

NameDescription

Thrown when one or more required arguments are null.

See Also

RSASignature

FromStrongNamePublicKey(byte[])

Creates an object from the given strong name public key buffer.

public static RSASignature FromStrongNamePublicKey(byte[] publicKey)

Parameters

NameDescription

publicKey byte[]

The strong name assembly public key.

Returns

NameDescription

An instance of RSASignature.

Exceptions

NameDescription

Thrown when one or more required arguments are null.

ImportKeys(string)

Import RSA keys.

public void ImportKeys(string rsaKey)

Parameters

NameDescription

rsaKey string

The base64 encoded string RSA key.

Exceptions

NameDescription

Thrown when one or more required arguments are null.

Thrown when the requested operation is invalid.

ImportRSA(RSA)

Import RSA keys.

public void ImportRSA(RSA rsa)

Parameters

NameDescription

rsa RSA

The RSA algorithm.

Exceptions

NameDescription

Thrown when one or more required arguments are null.

SignData(byte[])

Generate the signature for the given data.

public byte[] SignData(byte[] data)

Parameters

NameDescription

data byte[]

The data to sign.

Returns

NameDescription

Returns the generated signature.

See Also

RSASignature

ToRSA()

Converts this object to a .

public RSA ToRSA()

Returns

NameDescription

This object as a RSA.

Exceptions

NameDescription

Thrown when the requested operation is invalid.

ToXmlDigitalSignature()

Converts this object to an object.

public XmlDigitalSignature ToXmlDigitalSignature()

Returns

NameDescription

The object.

VerifyData(byte[], byte[])

Verify the signature of a given data buffer.

public bool VerifyData(byte[] data, byte[] signature)

Parameters

NameDescription

data byte[]

The data to verify.

signature byte[]

The signature buffer.

Returns

NameDescription

true if the signature is valid, false if is not valid.

See Also

RSASignature

WritePem(string, bool)

Writes the signature using PEM format to the specified file.

public void WritePem(string path, bool publicKeyOnly)

Parameters

NameDescription

path string

The file path.

publicKeyOnly bool

Whether to write only public key data.

Examples

// Sign data using RSA algorithm with 768 bit key size
RSASignature signer = new RSASignature(768);

// A private key is needed to sign data
signer.CreateKeyPair();

// Save RSA key pair to a PEM file
signer.WritePem("RSAkeys.pem", false);

// File content
// -----BEGIN RSA PRIVATE KEY-----
// MIIByQIBAAJhALsbgbJEQYSnIBZeauJ0AEIVqSlXiAhrZ+kir0A23lpxGY0xfKdo
// Sgtce+eu39+NEXiXE/XqGFGmTq3VVomYEnoSmgmfhsLvGWkI6dnLtdOfxaBvPFGy
// 6YvDDk7qMopV8QIBAwJgfL0BIYLWWG9quZRHQaKq1rkbcOUFWvJFRhcfgCSUPEtm
// XiD9xPAxXOhSmnSVP7Nf1RPt3B3syRApg/o3Cd4+B17bIqv5BbNrt3Ow5+/eFyK7
// gtRobuWu4gBfH9X5neejAjEA9zlKt/tahtXOsHoJsRg84qUnpNfCMrNfi9AmWny+
// 7AwSndeqcDkMYk7Tv1z8WKJrAjEAwb/kc8HanThBt2P6SbJ4jF8psMXPB66YSgs6
// I2cpxN+Zvlj1OyFW1nOr38w/xNgTAjEApNDceqeRrzk0daaxILrTQcNvwzqBdyI/
// sorEPFMp8rK3E+UcStCy7DSNKj39kGxHAjEAgSqYTSvnE3rWekKm28xQXZTGddk0
// r8m63Ad8F5ob2JURKZCjfMDkjvfH6ogqgzq3AjA6hGF6znnuDZTrF4iO1rVXMj1I
// lRpQ0nE0svBu9kqhoYkd17LVVfzt9u2+tcD81Wo=
// -----END RSA PRIVATE KEY-----

Exceptions

NameDescription

Thrown when one or more required arguments are null.

Thrown when one or more arguments have unsupported or illegal values.

WritePem(Stream, bool)

Writes the signature using PEM format to the specified stream.

public void WritePem(Stream stream, bool publicKeyOnly)

Parameters

NameDescription

stream Stream

The stream to write.

publicKeyOnly bool

Whether to write only public key data.

Exceptions

NameDescription

Thrown when one or more required arguments are null.

See Also

ECDsaSignature, XmlDigitalSignature

Last updated