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

Using CryptoSys PKI with .NET Core on Windows


This page shows how to use CryptoSys PKI with .NET Core (dotnetcore) on Windows. It is similar to the instructions on how to use FirmaSAT with .NET core.

Prerequisites | Principles | A simple test | Instructions | Full .NET Tests | Using Visual Studio Code | Contact us

Prerequisites

  1. You must have installed CryptoSys PKI on your system, available here. That is, the core DLL file diCrPKI.dll must exist in your application's Library Search Path.
  2. Only Windows is supported. New2024-12-18: See Using CryptoSys PKI with .NET Core on Linux.
  3. You have installed .NET Core. We will be using the command-line interface (dotnet CLI). These instructions were written for .NET Core 3.1 (Updatedbut they work equally as well using .NET Core 9.0)

We use the word "core" in our documentation to refer to the "core" of the CryptoSys PKI application, the native Windows DLL file diCrPKI.dll. Don't get confused with "dotnet core".

Principles

The .NET interface library file diCrSysPKINet.dll provided with the CryptoSys PKI distribution works with both the standard .NET Framework and .NET Core. You just need to set a reference to it in your dotnetcore project file. All the example C# source code modules provided in the distribution and elsewhere on this site works on .NET Core without alteration (** except for one small edit).

A copy of the file diCrSysPKINet.dll is installed by default in the folder C:\Program Files (x86)\CryptoSysPKI\DotNet (or C:\Program Files\CryptoSysPKI\DotNet on a 32-bit platform). You can refer to this directly as a "hint" in your project file: a copy will be made in the \bin subdirectory when you first build the project.

A simple test

The equivalent of an "Hello World!" program for CryptoSys PKI is to print the version number of the native CryptoSys PKI DLL using the General.Version() method.

Here is the code module pki_simple.cs. Note the using CryptoSysPKI; line.

// pki_simple.cs

using System;
using CryptoSysPKI;

namespace pki_simple
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Version = {0}", General.Version());
        }
    }
}
Expected output (similar to):
Version = 200000
If this works OK, it shows you have done things correctly and you can move on to more complicated projects.

Instructions

  1. Create a new dotnet project. Open a command-line console and type
    > dotnet new console -n pki_simple -o pki_simple
    
    This should display:
    The template "Console Application" was created successfully.
    ...etc.
    Restore succeeded.
    
    This will create a new subdirectory called pki_simple with the following structure.
    \---pki_simple
        |   pki_simple.csproj
        |   Program.cs
        |
        \---obj
                project.assets.json
                project.nuget.cache
                ... etc.
    
  2. Change directory into this subdirectory:
    cd pki_simple
    
  3. Rename and edit the program code. Rename the file Program.cs to pki_simple.cs and edit it to match the above code. Or just copy the new file pki_simple.cs and delete Program.cs.
  4. Add a reference in the project file. Edit the .csproj file as follows. Add an ItemGroup element with a Reference to the .NET library DLL.
    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
      </PropertyGroup>
      <!-- BEGIN ADD THIS -->
      <ItemGroup>
        <Reference Include="diCrSysPKINet">
          <SpecificVersion>False</SpecificVersion>
          <HintPath>C:\Program Files (x86)\CryptoSysPKI\DotNet\diCrSysPKINet.dll</HintPath>
        </Reference>
      </ItemGroup>
      <!-- END ADD THIS -->
    </Project>
    
    Note we can use a hard-coded reference to diCrSysPKINet.dll in the HintPath because a copy will be made in our local bin directory when the project is built. If you used a different directory when installing CryptoSys PKI, you will need to change the HintPath to match.
  5. Build the project.
    > dotnet build
    
    If this works OK, it should show
    Build succeeded.
        0 Warning(s)
        0 Error(s)
    
  6. Run it.
    > dotnet run
    
    Expected output:
    Version = 200000
    
The actual EXE file is created by default in the subdirectory bin\Debug\netcoreapp3.1.
\---pki_simple
    |   pki_simple.cs
    |   pki_simple.csproj
    |
    +---bin
    |   \---Debug
    |       \---netcoreapp3.1
    |               diCrSysPKINet.dll
    |               pki_simple.deps.json
    |               pki_simple.dll
    |               pki_simple.exe
    |               pki_simple.pdb
    |               pki_simple.runtimeconfig.dev.json
    |               pki_simple.runtimeconfig.json
    |
which you can run directly
> .\bin\Debug\netcoreapp3.1\pki_simple.exe
Version = 200000

Full .NET Tests

The full .NET tests provided in the distribution work exactly the same as with the .NET Framework. The source code module TestPKIcsharp.cs is unchanged (** except for one small edit).

The setup in dotnetcore uses similar instructions as above, but we need to add a subdirectory for the test files, and add an instruction for the starting working directory to the project file.

The test files are included in the distribution in the file pkiDotNetTestFiles.zip.

  1. Create a new project
    dotnet new console -n TestPKIcsharp -o TestPKIcsharp
    
  2. Replace Project.cs with TestPKIcsharp.cs, then edit the file:

    One small edit

    Comment out line 293
    // test_Cnv_other();
    
    It seems the tests using Encoding.GetEncoding(1252) are not supported in dotnetcode.
  3. Add the test files in the subdirectory pkiDotNetTestFiles. So the file structure should now be as follows.
    \---TestPKIcsharp
        |   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.
    
  4. Edit the .csproj file as follows, with the same ItemGroup/Reference as above, plus a StartWorkingDirectory element so the program will start in the directory with the test files (the program assumes the test files are in its current working directory).
    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
      </PropertyGroup>
      <PropertyGroup>
        <StartWorkingDirectory>.\pkiDotNetTestFiles\</StartWorkingDirectory>
      </PropertyGroup>
      <ItemGroup>
        <Reference Include="diCrSysPKINet">
          <SpecificVersion>False</SpecificVersion>
          <HintPath>C:\Program Files (x86)\CryptoSysPKI\DotNet\diCrSysPKINet.dll</HintPath>
        </Reference>
      </ItemGroup>
    </Project>
    
  5. Build the project.
    dotnet build
    
  6. Run it
    dotnet run
    

Expected output

PKI Version is 200000
Local directory is 'C:\...\DotNetCore\pki\TestPKIcsharp\bin\Debug\netcoreapp3.1'.
Checking required test files are in local directory...
Creating test sub-directory 'pkitest.09385729'
CWD is C:\...\DotNetCore\pki\TestPKIcsharp\pkiDotNetTestFiles\pkitest.09385729
DOING ALL TESTS...

GENERAL FUNCTIONS:
Version=200000
LicenceType=D
ModuleName=C:\WINDOWS\SYSTEM32\diCrPKI.dll

...[cut]...

TESTING TRIPLE DES:
KY=010101010101010101010101010101010101010101010101
PT=8000000000000000
CT=95F8A5E5DD31D900
OK=95F8A5E5DD31D900
P'=8000000000000000
OK=8000000000000000
CT=95F8A5E5DD31D900

...[cut]...

ALL TESTS COMPLETED.

CWD reset to C:\...\DotNetCore\pki\TestPKIcsharp\pkiDotNetTestFiles
Removing test directory 'pkitest.09385729'

(Version and compile time may vary)

Using Visual Studio Code

You can use the free Visual Studio Code to analyse and even run your dotnetcode project.

Really neat trick to open VS Code

With the command line prompt open in your project directory, just type code . (that's the word "code" followed by a space and a dot).
code .
This will open up Visual Studio Code and show all your project files in the VSCode explorer window. You can even run your project from the IDE, but you may get a lot of unnecessary warnings, etc.

Contact us

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

[Go to top]

This page first published 18 November 2020. Last updated 18 December 2024.