CryptoNet 1.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package CryptoNet --version 1.2.0
NuGet\Install-Package CryptoNet -Version 1.2.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="CryptoNet" Version="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CryptoNet --version 1.2.0
#r "nuget: CryptoNet, 1.2.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.
// Install CryptoNet as a Cake Addin
#addin nuget:?package=CryptoNet&version=1.2.0

// Install CryptoNet as a Cake Tool
#tool nuget:?package=CryptoNet&version=1.2.0

Cryptonet

CryptoNet NuGet version Passing build workflow The Standard - COMPLIANT BCH compliance GitHub

Introdution

CryptoNet is a simple and a lightweight asymmetric encryption NuGet library. It is a 100% native C# implementation based on RSA factory class.

Installation

You can download CryptoNet via NuGet.

Versions

Nuget is latest version and are maintained.

v1.2.0 (Breaking changes)
  • Change from RSACryptoServiceProvider to RSA factory.
  • No longer support for symmertic encryption.
  • Console examples and Unit testing refactored.
  • Support for X509Certificate2.
v1.0.0:
  • Ability to encrypt and decrypt files like, images, word, excel etc.
  • Improvement documentation

Issues

Please report issues here.

Using / Documentation

Short intro

The are 2 ways to encrypt and decrypt content:

  1. Asymmetric RSA key pair (self generated keys).
  2. Asymmetric X509 Certificate (use own certificate).

Both ways:

  • Have a Private and Public key.
  • Use Public key to encrypt.
  • Use Private key to decrypt.
  • For RSA way, you need to generate RSA key pair first (Private/Public key).

You find the comlete and all examples here.

Here is some of the examples:

Example: Encrypt and Decrypt Content With SelfGenerated AsymmetricKey

ICryptoNet cryptoNet = new CryptoNet();

var privateKey = cryptoNet.ExportPrivateKey();
var publicKey = cryptoNet.ExportPublicKey();

ICryptoNet encryptClient = new CryptoNet(publicKey);
var encrypt = encryptClient.EncryptFromString(ConfidentialDummyData);
Console.WriteLine($"1- We will encrypt following text:\n{ConfidentialDummyData}\n");
Console.WriteLine($"2- To:\n{CryptoNetUtils.BytesToString(encrypt)}\n");

ICryptoNet decryptClient = new CryptoNet(privateKey);
var decrypt = decryptClient.DecryptToString(encrypt);
Console.WriteLine($"3- And we will decrypt it back to:\n{decrypt}\n");

Example: Generate and Export Asymmetric Key (Private/Public) Key (RasKeyPair)

ICryptoNet cryptoNet = new CryptoNet();

CryptoNetUtils.SaveKey(PrivateKeyFile, cryptoNet.ExportPrivateKey());
CryptoNetUtils.SaveKey(PublicKeyFile, cryptoNet.ExportPublicKey());

var privateKey = CryptoNetUtils.LoadFileToString(PrivateKeyFile);
Console.WriteLine($"The private key generated and saved to file {PrivateKeyFile}");
Console.WriteLine(privateKey);

var publicKey = CryptoNetUtils.LoadFileToString(PublicKeyFile);
Console.WriteLine($"\nThe public key generated and saved to file {PublicKeyFile}");
Console.WriteLine(publicKey);

Example: Encrypt with Public Key and later Decrypt with Private Key

var certificate = CryptoNetUtils.LoadFileToString(RsaKeyPair);
// Export public key
ICryptoNet cryptoNet = new CryptoNet(certificate, true);
var publicKey = cryptoNet.ExportPublicKey();
CryptoNetUtils.SaveKey(PublicKeyFile, publicKey);

// Import public key and encrypt
var importPublicKey = CryptoNetUtils.LoadFileToString(PublicKeyFile);
ICryptoNet cryptoNetEncryptWithPublicKey = new CryptoNet(importPublicKey, true);
var encryptWithPublicKey = cryptoNetEncryptWithPublicKey.EncryptFromString(ConfidentialDummyData);
Console.WriteLine("1- This time we use a certificate public key to encrypt");
Console.WriteLine(CryptoNetUtils.BytesToString(encryptWithPublicKey));

ICryptoNet cryptoNetDecryptWithPublicKey = new CryptoNet(certificate, true);
var decryptWithPrivateKey = cryptoNetDecryptWithPublicKey.DecryptToString(encryptWithPublicKey);
Console.WriteLine("6- And use the same certificate to decrypt");
Console.WriteLine(decryptWithPrivateKey);

Build and Testing

Build and run unit test from:

  1. Visual Studio 2019/2022.
  2. dotnet command line.
  3. Powershell, run build.ps1 from solution folder.
  4. Docker, run following command from solution folder:
docker build . --file .\Dockerfile --tag cryptonet-service:latest

Contributing

I need your help, so if you have good knowledge of C# and Cryptography just grab one of the issues and add a pull request. The same is valid, if you have idea for improvement or adding new feature.

How to contribute:

Here is a link to learn how to contribute if you are not a ware of how to do it.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.4.0 2,264 9/27/2023
2.3.0 581 8/15/2023
2.2.0 6,244 10/28/2022
2.2.0-preview001 117 7/12/2022
2.1.0 1,743 5/21/2022
1.6.0 432 2/3/2022
1.5.0 217 1/9/2022
1.2.0 209 1/1/2022
1.0.0 214 12/24/2021