Table of Contents

Class Certificate

Namespace
Arc4u.Security.Cryptography
Assembly
Arc4u.dll

Extension methods to encrypt and decrypt strings with an X509Certificate2. Short texts are encrypted directly with RSA (OAEP, SHA-256); longer texts are encrypted with a random AES key that is itself encrypted with the certificate. The result is a base64 string, or two base64 parts separated by a dot (key.data) in the AES case, where data is produced by Encrypt(string, byte[]). With an EC certificate the text is encrypted with ECIES: an ephemeral ECDH key agreement with the certificate key, HKDF-SHA256 to derive the AES key, then AES-GCM. The result is ephemeralPublicKey.data, the ephemeral public key being a base64 SubjectPublicKeyInfo. The algorithm is chosen from the certificate key, so a text must be decrypted with the certificate it was encrypted for. Only certificates with an RSA or an EC key are supported.

public static class Certificate
Inheritance
Certificate
Inherited Members

Methods

Decrypt(X509Certificate2, string)

Decrypt an formated 64 string encrypted and return the text in clear.

public static string Decrypt(this X509Certificate2 x509, string base64Cypherstring)

Parameters

x509 X509Certificate2

The certificate used to encrypt

base64Cypherstring string

The cypher text to decrypt in a base 64 format.

Returns

string

The decrypted text.

Exceptions

ArgumentNullException
CryptographicException

The certificate has no private key, or its private key cannot be accessed (for example missing permissions on a LocalMachine certificate).

NotSupportedException

The certificate has no RSA or EC private key.

Encrypt(X509Certificate2, string)

Encrypt a text and return an encrypted version formated in a 64string.

public static string Encrypt(this X509Certificate2 x509, string plainText)

Parameters

x509 X509Certificate2

The certificate used to encrypt

plainText string

The plain text to encrypt.

Returns

string

The encrypted plain text.

Exceptions

NotSupportedException

The certificate has no RSA or EC public key.