client-encryption-csharp
Table of Contents
Overview
Compatibility
.NET
ClientEncryption.Core targets .NET Standard 2.1
ClientEncryption.RestSharpV2 targets .NET Standard 2.1
ClientEncryption.RestSharp targets .NET Standard 2.1
.NET Standard versions supported by .NET implementations can be found in the following articles: .NET Standard, .NET Standard versions.
Strong Naming
Assemblies are strong-named as per Strong naming and .NET libraries.
The SN key is available here: Identity.snk.
References
Versioning and Deprecation Policy
Usage
Prerequisites
Before using this library, you will need to set up a project in the Mastercard Developers Portal.
As part of this set up, you'll receive:
- A public request encryption certificate (aka Client Encryption Keys)
- A private response decryption key (aka Mastercard Encryption Keys)
Adding the Libraries to Your Project
Package Manager
Install-Package Mastercard.Developer.ClientEncryption.{Core|RestSharp|RestSharpV2}
.NET CLI
dotnet add package Mastercard.Developer.ClientEncryption.{Core|RestSharp|RestSharpV2}
Loading the Encryption Certificate
A System.Security.Cryptography.X509Certificates.X509Certificate object can be created from a file by calling EncryptionUtils.LoadEncryptionCertificate:
var encryptionCertificate = EncryptionUtils.LoadEncryptionCertificate("<insert certificate file path>");
Supported certificate formats: PEM, DER.
Loading the Decryption Key
From a PKCS#12 Key Store
A System.Security.Cryptography.RSA object can be created from a PKCS#12 key store by calling EncryptionUtils.LoadDecryptionKey the following way:
var decryptionKey = EncryptionUtils.LoadDecryptionKey(
"<insert PKCS#12 key file path>",
"<insert key alias>",
"<insert key password>");
From an Unencrypted Key File
A System.Security.Cryptography.RSA object can be created from an unencrypted key file by calling EncryptionUtils.LoadDecryptionKey the following way:
var decryptionKey = EncryptionUtils.LoadDecryptionKey("<insert key file path>");
Supported RSA key formats:
- PKCS#1 PEM (starts with "-----BEGIN RSA PRIVATE KEY-----")
- PKCS#8 PEM (starts with "-----BEGIN PRIVATE KEY-----")
- Binary DER-encoded PKCS#8
Performing Payload Encryption and Decryption
Introduction
This library supports two types of encryption/decryption, both of which support field level and entire payload encryption: JWE encryption and what the library refers to as Field Level Encryption (Mastercard encryption), a scheme used by many services hosted on Mastercard Developers before the library added support for JWE.
JWE Encryption and Decryption
• Introduction
This library uses JWE compact serialization for the encryption of sensitive data.
The core methods responsible for payload encryption and decryption are EncryptPayload and DecryptPayload in the JweEncryption class.
var encryptedRequestPayload = JweEncryption.EncryptPayload(requestPayload, config);
var responsePayload = JweEncryption.DecryptPayload(encryptedResponsePayload, config);
• Configuring the JWE Encryption
Use the JweConfigBuilder to create JweConfig instances. Example:
var config = JweConfigBuilder.AJweEncryptionConfig()
.WithEncryptionCertificate(encryptionCertificate)
.WithDecryptionKey(decryptionKey)
.WithEncryptionPath("$.path.to.foo", "$.path.to.encryptedFoo")
.WithDecryptionPath("$.path.to.encryptedFoo", "$.path.to.foo")
.WithEncryptedValueFieldName("encryptedValue")
.Build();
• Performing JWE Encryption
Call JweEncryption.EncryptPayload with a JSON request payload and a JweConfig instance.
Example using the configuration above:
const string payload = "{" +
" \"path\": {" +
" \"to\": {" +
" \"foo\": {" +
" \"sensitiveField1\": \"sensitiveValue1\"," +
" \"sensitiveField2\": \"sensitiveValue2\"" +
" }" +
" }" +
" }" +
"}";
var encryptedPayload = JweEncryption.EncryptPayload(payload, config);
Console.WriteLine(JObject.Parse(encryptedPayload));
Output:
{
"path": {
"to": {
"encryptedFoo": {
"encryptedValue": "eyJraWQiOiI3NjFiMDAzYzFlYWRlM….Y+oPYKZEMTKyYcSIVEgtQw"
}
}
}
}
• Performing JWE Decryption
Call JweEncryption.DecryptPayload with a JSON response payload and a JweConfig instance.
Example using the configuration above:
const string encryptedPayload = "{" +
" \"path\": {" +
" \"to\": {" +
" \"encryptedFoo\": {" +
" \"encryptedValue\": \"eyJraWQiOiI3NjFiMDAzYzFlYWRlM….Y+oPYKZEMTKyYcSIVEgtQw\"" +
" }" +
" }" +
" }" +
"}";
var payload = JweEncryption.DecryptPayload(encryptedPayload, config);
Console.WriteLine(JObject.Parse(payload));
Output:
{
"path": {
"to": {
"foo": {
"sensitiveField1": "sensitiveValue1",
"sensitiveField2": "sensitiveValue2"
}
}
}
}
• Encrypting Entire Payloads
Entire payloads can be encrypted using the "$" operator as encryption path:
var config = JweConfigBuilder.AJweEncryptionConfig()
.WithEncryptionCertificate(encryptionCertificate)
.WithEncryptionPath("$", "$")
// …
.Build();
Example:
const string payload = "{" +
" \"sensitiveField1\": \"sensitiveValue1\"," +
" \"sensitiveField2\": \"sensitiveValue2\"" +
"}";
var encryptedPayload = JweEncryption.EncryptPayload(payload, config);
Console.WriteLine(JObject.Parse(encryptedPayload));
Output:
{
"encryptedValue": "eyJraWQiOiI3NjFiMDAzYzFlYWRlM….Y+oPYKZEMTKyYcSIVEgtQw"
}
• Decrypting Entire Payloads
Entire payloads can be decrypted using the "$" operator as decryption path:
var config = JweConfigBuilder.AJweEncryptionConfig()
.WithDecryptionKey(decryptionKey)
.WithDecryptionPath("$", "$")
// …
.Build();
Example:
const string encryptedPayload = "{" +
" \"encryptedValue\": \"eyJraWQiOiI3NjFiMDAzYzFlYWRlM….Y+oPYKZEMTKyYcSIVEgtQw\"" +
"}";
var payload = JweEncryption.DecryptPayload(encryptedPayload, config);
Console.WriteLine(JObject.Parse(payload));
Output:
{
"sensitiveField1": "sensitiveValue1",
"sensitiveField2": "sensitiveValue2"
}
Mastercard Encryption and Decryption
• Introduction
The core methods responsible for payload encryption and decryption are EncryptPayload and DecryptPayload in the FieldLevelEncryption class.
var encryptedRequestPayload = FieldLevelEncryption.EncryptPayload(requestPayload, config);
var responsePayload = FieldLevelEncryption.DecryptPayload(encryptedResponsePayload, config);
• Configuring the Mastercard Encryption
Use the FieldLevelEncryptionConfigBuilder to create FieldLevelEncryptionConfig instances. Example:
var config = FieldLevelEncryptionConfigBuilder.AFieldLevelEncryptionConfig()
.WithEncryptionCertificate(encryptionCertificate)
.WithDecryptionKey(decryptionKey)
.WithEncryptionPath("$.path.to.foo", "$.path.to.encryptedFoo")
.WithDecryptionPath("$.path.to.encryptedFoo", "$.path.to.foo")
.WithOaepPaddingDigestAlgorithm("SHA-256")
.WithEncryptedValueFieldName("encryptedValue")
.WithEncryptedKeyFieldName("encryptedKey")
.WithIvFieldName("iv")
.WithValueEncoding(FieldValueEncoding.Hex)
.Build();
See also:
• Performing Mastercard Encryption
Call FieldLevelEncryption.EncryptPayload with a JSON request payload and a FieldLevelEncryptionConfig instance.
Example using the configuration above:
const string payload = "{" +
" \"path\": {" +
" \"to\": {" +
" \"foo\": {" +
" \"sensitiveField1\": \"sensitiveValue1\"," +
" \"sensitiveField2\": \"sensitiveValue2\"" +
" }" +
" }" +
" }" +
"}";
var encryptedPayload = FieldLevelEncryption.EncryptPayload(payload, config);
Console.WriteLine(JObject.Parse(encryptedPayload));
Output:
{
"path": {
"to": {
"encryptedFoo": {
"iv": "7f1105fb0c684864a189fb3709ce3d28",
"encryptedKey": "67f467d1b653d98411a0c6d3c…ffd4c09dd42f713a51bff2b48f937c8",
"encryptedValue": "b73aabd267517fc09ed72455c2…dffb5fa04bf6e6ce9ade1ff514ed6141"
}
}
}
}
• Performing Mastercard Decryption
Call FieldLevelEncryption.DecryptPayload with a JSON response payload and a FieldLevelEncryptionConfig instance.
Example using the configuration above: