Skip to main content

Encrypt and Decrypt Helpers

Library: ADS.EncryptDecrypt.Utils

Asymmetric Key Encryption Utilities​

The AsymmetricKeyEncryptionKeyVaultUtils class provides functionalities for managing RSA encryption and decryption operations using Azure Key Vault. It includes methods to encrypt and decrypt data, create RSA keys, and retrieve key identifiers for secure cryptographic operations.

Service : AsymmetricKeyEncryptionKeyVaultUtils

  1. Encrypt

Task<string> Encrypt(string plainText, string keyName);

Description:

Encrypts plain text using RSA encryption via Azure Key Vault.

Parameters:

• plainText (string) – The text to be encrypted.

• keyName (string) – The name of the RSA key stored in Key Vault.

Returns:

Task<string> – The Base64-encoded encrypted text.

  1. Decrypt

Task<string> Decrypt(string cipherText, string keyName);

Description:

Decrypts cipher text using RSA decryption via Azure Key Vault.

Parameters:

• cipherText (string) – The encrypted text (Base64-encoded).

• keyName (string) – The name of the RSA key stored in Key Vault.

Returns:

Task<string> – The decrypted plain text.

  1. CreateRSAKeyAsync

Task<string> CreateRSAKeyAsync(string keyName, int keySize = 2048);

Description:

Creates a new RSA key in Azure Key Vault.

Parameters:

• keyName (string) – The name of the RSA key.

• keySize (int, optional) – The size of the RSA key (Default: 2048).

Returns:

Task<string> – The Key Vault URI of the created RSA key.

  1. GetKeyIdAsync

Task<string> GetKeyIdAsync(string keyName);

Description:

Retrieves the Key Vault URI for a specified RSA key.

Parameters:

• keyName (string) – The name of the RSA key.

Returns:

Task<string> – The Key Vault URI of the RSA key.

  1. GetClientSecretCredentials

TokenCredential GetClientSecretCredentials(ClientCredentials clientCredentials = null);

Description:

Creates an authentication token for accessing Azure Key Vault.

Parameters:

• clientCredentials (ClientCredentials, optional) – The client credentials containing Tenant ID, Client ID, and Client Secret.

Returns:

TokenCredential – A token-based credential for secure authentication.

1. CreateSelfSignedCertificate​

Task<string> CreateSelfSignedCertificate(string certificateName, string subject, int keySize = 2048);

Description:

Creates a self-signed certificate in Azure Key Vault.

Parameters:

• certificateName (string) – The name of the certificate.

• subject (string) – The subject name for the certificate.

• keySize (int, optional) – The size of the RSA key (Default: 2048).

Returns:

Task<string> – The Key Vault URI of the created certificate.

2. RotateCertificate​

Task<string> RotateCertificate(string certificateName);

Description:

Rotates an existing self-signed certificate by disabling previous versions and generating a new one.

Parameters:

• certificateName (string) – The name of the certificate.

Returns:

Task<string> – The Key Vault URI of the rotated certificate.

3. GenerateRSAKeys​

AsymmetricKeyPair GenerateRSAKeys(int keySize = 2048);

Description:

Generates a new RSA key pair (public and private keys).

Parameters:

• keySize (int, optional) – The size of the RSA key (Default: 2048).

Returns:

AsymmetricKeyPair – An object containing the public and private keys.

4. ImportCertificate​

Task<string> ImportCertificate(string certificateName, byte[] certificateData);

Description:

Imports a certificate into Azure Key Vault.

Parameters:

• certificateName (string) – The name of the certificate.

• certificateData (byte[]) – The binary data of the certificate.

Returns:

Task<string> – The Key Vault URI of the imported certificate.

5. GetPublicKey​

string GetPublicKey(string certificateName);

Description:

Retrieves the public key from a certificate stored in Azure Key Vault.

Parameters:

• certificateName (string) – The name of the certificate.

Returns:

string – The public key.

6. GetPrivateKey​

string GetPrivateKey(string certificateName);

Description:

Retrieves the private key from a certificate stored in Azure Key Vault.

Parameters:

• certificateName (string) – The name of the certificate.

Returns:

string – The private key.

7. Encrypt​

string Encrypt(string plainText, string publicKey);

Description:

Encrypts plain text using RSA encryption with a public key.

Parameters:

• plainText (string) – The text to be encrypted.

• publicKey (string) – The public key used for encryption.

Returns:

string – The Base64-encoded encrypted text.

8. Decrypt​

string Decrypt(string cipherText, string privateKey);

Description:

Decrypts cipher text using RSA decryption with a private key.

Parameters:

• cipherText (string) – The encrypted text (Base64-encoded).

• privateKey (string) – The private key used for decryption.

Returns:

string – The decrypted plain text.

9. ExportPublicKey​

string ExportPublicKey(RSA rsa);

Description:

Exports the public key from an RSA object in PEM format.

Parameters:

• rsa (RSA) – The RSA object.

Returns:

string – The PEM-formatted public key.

10. ExportPrivateKey​

string ExportPrivateKey(RSA rsa);

Description:

Exports the private key from an RSA object in PEM format.

Parameters:

• rsa (RSA) – The RSA object.

Returns:

string – The PEM-formatted private key.

Symmetric Key Encryption Utilities​

The SymmetricKeyEncryptionUtils class provides functionalities for managing secrets and encryption operations using Azure Key Vault. It includes methods to create, retrieve, and rotate secrets, perform encryption and decryption using AES, and wrap/unwrap symmetric keys using Azure Key Vault.

Service : SymmetricKeyEncryptionUtils

1. CreateSecretAsync​

Task<string> CreateSecretAsync(string secretName, int keySize = 128);

Description:

Creates a secret (symmetric key) in Azure Key Vault.

Parameters:

• secretName (string) – The name of the secret.

• keySize (int, optional) – The size of the symmetric key (Default: 128).

Returns:

Task<string> – The Key Vault URI of the created secret.

2. GetSecretAsync​

Task<string> GetSecretAsync(string secretName);

Description:

Retrieves the secret (symmetric key) from Azure Key Vault.

Parameters:

• secretName (string) – The name of the secret.

Returns:

Task<string> – The secret value stored in Key Vault.

3. RotateSecretAsync​

Task<string> RotateSecretAsync(string secretName, int keySize = 128);

Description:

Rotates the secret key by disabling previous versions and generating a new one.

Parameters:

• secretName (string) – The name of the secret.

• keySize (int, optional) – The size of the symmetric key (Default: 128).

Returns:

Task<string> – The Key Vault URI of the new rotated secret.

4. Encrypt​

string Encrypt(string plainText, string symmetricKey);

Description:

Encrypts plain text using AES encryption.

Parameters:

• plainText (string) – The text to be encrypted.

• symmetricKey (string) – The symmetric key used for encryption.

Returns:

string – The Base64-encoded encrypted text.

5. Decrypt​

string Decrypt(string cipherText, string symmetricKey);

Description:

Decrypts cipher text using AES decryption.

Parameters:

• cipherText (string) – The encrypted text (Base64-encoded).

• symmetricKey (string) – The symmetric key used for decryption.

Returns:

string – The decrypted plain text.

6. WrapKeyAsync​

Task<string> WrapKeyAsync(string symmetricKey, string keyName);

Description:

Wraps a symmetric key using Azure Key Vault's key encryption key (KEK).

Parameters:

• symmetricKey (string) – The symmetric key to wrap.

• keyName (string) – The Key Vault key used for wrapping.

Returns:

Task<string> – The Base64-encoded wrapped key.

7. UnWrapKeyAsync​

Task<string> UnWrapKeyAsync(string wrapKey, string keyName);

Description:

Unwraps a symmetric key using Azure Key Vault's key encryption key (KEK).

Parameters:

• wrapKey (string) – The wrapped key (Base64-encoded).

• keyName (string) – The Key Vault key used for unwrapping.

Returns:

Task<string> – The unwrapped symmetric key.

8. GenerateRandomSymmetricKey​

string GenerateRandomSymmetricKey(int keySize = 128);

Description:

Generates a random symmetric key of a specified size.

Parameters:

• keySize (int, optional) – The size of the symmetric key (Default: 128).

Returns:

string – The Base64-encoded symmetric key.