CryptoSys Home > PKI > Using CryptoSys PKI with .NET Core on Linux

Using CryptoSys PKI with .NET Core on Linux


You can use CryptoSys PKI Pro with .NET Core (dotnetcore) on an x64 Linux system. To use on Windows see Using CryptoSys PKI with .NET Core on Windows.

Introduction

Using CryptoSys PKI with .NET Core on Linux is different from using on Windows in two ways.

  1. You need the Linux core dynamic library for CryptoSys PKI instead of the Windows diCrPKI.dll
  2. You include the source code CryptoSysPKI.cs in your project instead of making a reference to the .NET DLL diCrSysPKINet.dll

Install

  1. Install the CryptoSys PKI Linux version on your system. Download the latest package from CryptoSys PKI Linux Version and follow the instructions.
    TL;DR
  2. Check dotnet core SDK is installed.
    dotnet --info
    dotnet --list-sdks
    
  3. If SDK is not installed, install it. See the Microsoft Documentation.

Set up a test dotnet project

If this simple test project works, then everything else will work...

1. From the command line create a new dotnet project
dotnet new console -n pki_simple -o pki_simple
2. Copy the file CryptoSysPKI.cs into this directory. Expected directory structure
\---pki_simple
    |   CryptoSysPKI.cs
    |   pki_simple.csproj
    |   Program.cs
    |
    \---obj
        project.assets.json
        ... etc.
3. Change directory into project
cd pki_simple
4. If you are using dotnet9.0 or later, edit the file pki_simple.csproj and change the line <Nullable>enable</Nullable> to <Nullable>disable</Nullable>

This prevents messages of the form warning CS8625: Cannot convert null literal to non-nullable reference type. This annoying warning is not relevant for our code.

5. Replace existing Program.cs with the following:
using System.Runtime.InteropServices;
using System.Text;
using CryptoSysPKI;

public class CryptoSysPKIWrapper
{
    [STAThread]
    static void Main(string[] args)
    {
		Console.WriteLine("CryptoSysPKI core Version is " + General.Version());
		Console.WriteLine("CompileTime= " + General.CompileTime());
    }
}
6. Compile and run
dotnet build
dotnet run
The output should be similar to
CryptoSysPKI core Version is 230001
CompileTime= Dec 15 2024 20:25:50

Full .NET Tests

As with the Windows version, we can do all the TestPKIcsharp tests on the Linux platform using dotnetcore.
dotnet new console -n TestPKIcsharp -o TestPKIcsharp
Copy CryptoSysPKI.cs and TestPKIcsharp.cs into the test folder and unzip the test files into a sub-directory pkiDotNetTestFiles. (All these files are in Downloads below.)
\---TestPKIcsharp
    |   CryptoSysPKI.cs
    |   TestPKIcsharp.cs
    |   TestPKIcsharp.csproj
    |
    +---pkiDotNetTestFiles
    |       alice-lamps.crt
    |       alice-lamps.p12
    |       AlicePrivRSASign.p8e
    |       AlicePubRSA.pub
    |       AliceRSAPSS.p8
    |       AliceRSAPSS.p8e
    |       ... etc.
    \---obj
            project.assets.json
            project.nuget.cache
            ... etc.
Edit the project file
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>disable</Nullable>
  </PropertyGroup>
  <PropertyGroup>
    <StartWorkingDirectory>./pkiDotNetTestFiles/</StartWorkingDirectory>
  </PropertyGroup>

</Project>
Build and run.
dotnet build
dotnet run

What's the significance of all these complicated tests?

The tests show that all the features of CryptoSys PKI will work on a Linux system using dotnetcore. If these work, then any of the examples provided should work. Furthermore, you should be able to pick out the code you need to do whatever task you require.

Notes

  1. You can check the libraries have been installed correctly by listing the files in the /usr/lib directory. You should see the following links
    /usr/lib/diCrPKI.dll.so -> /usr/lib/libcryptosyspki.so.23
    /usr/lib/libcryptosyspki.so.23 -> /usr/lib/libcryptosyspki.so.23.0.1
    
    The "dll" means nothing in Linux but making that link from diCrPKI.dll.so means we don't have to change the [DllImport] statements in the source code.

Downloads

Download the test files used above: pki-dotnetcore-tests.zip.

Manifest
CryptoSysPKI.cs
pkiDotNetTestFiles.zip
Program.cs
TestPKIcsharp.cs
The file TestPKIcsharp.cs has been edited since its last release v23.0.0 in September 2024 to fix up a few Windows-centric issues (hard-coded backslash file separator, Windows 1252 encoding not supported in dotnet).

Contact us

To contact us or comment on this page, please send us a message.

[Go to top]

This page first published 18 December 2024. Last updated 19 December 2024