Kalix.ApiCrypto 11.0.0

dotnet add package Kalix.ApiCrypto --version 11.0.0
                    
NuGet\Install-Package Kalix.ApiCrypto -Version 11.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Kalix.ApiCrypto" Version="11.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Kalix.ApiCrypto" Version="11.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Kalix.ApiCrypto" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Kalix.ApiCrypto --version 11.0.0
                    
#r "nuget: Kalix.ApiCrypto, 11.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Kalix.ApiCrypto@11.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Kalix.ApiCrypto&version=11.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Kalix.ApiCrypto&version=11.0.0
                    
Install as a Cake Tool

Kalix.ApiCrypto

Useful collection of classes to help with security of api endpoints

Features:

  • Create Json Web Tokens signed with ECDSA (P521, P384, P256) certificate
  • Create secure AES keys using RSA certificates
  • Encrypt/Decrypt files using AES with best practise in mind

Examples of use

Global encryption (RSA/AES hybrid)

Certificate based encryption is really useful for separation of a codebase to the maintenance of a public/private key pair. It's downside is that the encryption is much slower due to the much larger key sizes of asymmetric (ie RSA) vs symmetric (ie AES) algorithms.

This example will set up a hybrid approach. You will have a strong RSA certifate, which you will use to encrypt an AES key blob. The blob can then be stored on azure blob storage or Amazon S3 as an example. When your server starts up it will use the RSA certificate to decrypt this blob. Once decrypted the AES symmetric key can be used to encrypt/decrypt all your records.

First step is to use a RSA certificate and create the encrypted AES shared key:

// Get the certificate from the store
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var cert = store.Certificates.Find(X509FindType.FindBySubjectName, "SubjectName", false)[0];

// Create a blob that can be saved in blob storage or S3 etc
var blobData = AESBlob.CreateBlob(AESKeySize.AES256, cert);

You will have to install the public/private key pair on all servers that need to encrypt/decrypt data. It will only be used to extract the AES key from the blob:

// Get the certificate from the store
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var cert = store.Certificates.Find(X509FindType.FindBySubjectName, "SubjectName", false)[0];

// Create the encryptor
var encryptor = AESBlob.CreateEncryptor(blobData, cert);

The encryptor has an Encypt and Decrypt method that is (read) stream based. This makes it easy to use for small or large files. It also follows best practice by creating a new IV per file and adding it to the start of the stream.

Access Tokens (inc. OAuth)

Kalix is broken up into two parts. One server serves up the content and deals with authentication and the other serves up sensitive patient data. Due to privacy requirements the patient records might need to reside in the country of origin. This means our systems cannot share a db etc without some serious latency.

In this scenario we can authenticate our users via a central authentication server. Once authenticated we can generate short lived access tokens that the user can use to access the alternative server that holds the patient records. The data is open but is signed using the private ECDSA key. ECDSA is similar to DSA but the key length is smaller which allows us to produce much smaller access tokens with the same level of security.

Since we are using Azure we will have the ECDSA key stored as a certificate. This allows us to seperate the managing of the super secret private key and our codebase.

The first step is to create the certificates we will need on each server.

// Get the certificate from the store
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var cert = store.Certificates.Find(X509FindType.FindBySubjectName, "SubjectName", false)[0];

// Export public key
var publicData = cert.Export(X509ContentType.Cert);
File.WriteAllBytes(Path.GetFullPath("public.cer"), publicData);

The full certificate should be installed on the machine that will create the access tokens. The cer should be installed on the machine that needs to verify them.

Now when you need to create an access token in your oauth flow you can use the JsonWebToken class:

// Get the certificate from the store
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var cert = store.Certificates.Find(X509FindType.FindBySubjectName, "SubjectName", false)[0];

// Create an access token that is signed (on authentication server)
var data = new { sub = "custId", aud = "scope1,scope2", exp = 1300819380 };
var token = JwtBuilder.Encode(data).SignUsingECDSA(cert).Build().JsonWebToken;

// On resource server we can verify and use this data
dynamic tokenData = JwtBuilder.Decode<object>(token).VerifyUsingECDSA(cert).Build().Claims;
var customerId = (string)tokenData.sub;
var exp = (int)tokenData.exp;

// Make sure to test expiry date, but apart from that we know customerId
// can be trusted and we can release information for that customer

Check out Claims on the JWT reference docs for claims you might use in your access token.

Licence

MIT - https://opensource.org/licenses/mit

Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Kalix.ApiCrypto:

Package Downloads
Kalix.Leo

Library to abstract indexing, querying, encryption and compression on a NoSQL store

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
11.0.0 165 11/12/2026
10.0.0 1,780 12/8/2025
9.0.0 963 11/21/2024
8.0.0 1,250 11/29/2023
7.0.1 567 6/29/2023
7.0.0 1,284 11/17/2022
6.0.1 734 9/3/2022
6.0.0 1,069 6/24/2022
5.0.0 2,893 1/28/2021
4.0.2 3,920 9/4/2017
3.0.0 10,201 12/28/2016
2.0.3 4,525 3/19/2016
2.0.2 2,231 11/4/2015
2.0.1 2,232 10/26/2015
2.0.0 4,464 9/21/2014
1.1.4 2,823 6/13/2014
1.1.3 1,738 5/23/2014
1.1.2 1,801 4/23/2014
Loading failed

Updated to .net 11