CryptoNet 1.6.0
See the version list below for details.
Requires NuGet 1.0.0 or higher.
dotnet add package CryptoNet --version 1.6.0
NuGet\Install-Package CryptoNet -Version 1.6.0
<PackageReference Include="CryptoNet" Version="1.6.0" />
paket add CryptoNet --version 1.6.0
#r "nuget: CryptoNet, 1.6.0"
// Install CryptoNet as a Cake Addin #addin nuget:?package=CryptoNet&version=1.6.0 // Install CryptoNet as a Cake Tool #tool nuget:?package=CryptoNet&version=1.6.0
Introdution
🚀 CryptoNet is simple, fast and a lightweight asymmetric and symmetric (note1) encryption NuGet library supporting .NET Standard 2.0 and C# 8.0 for cross platforms Windows, Linux, iOS. It is a 100% native C# implementation based on RSA factory class. It does not depending on other library.
note1: symmetric encryption is only supported in Windows OS.
Installation
You can download CryptoNet via NuGet.
Versions
is latest version and are maintained.
- Adapt RSA public interface for customization
- Adapt customization example for PEM
- Adapt ADotNet IoC for autogenerating GitHub workflows (first stage)
- Reintroducing symmertic encryption only for Windows OS.
- Adding Source Link, Deterministic and Compiler Flags to NuGet package.
- Readme enhancement.
- Change from RSACryptoServiceProvider to RSA factory that support cross platforms (Windows, Linux, iOS).
- No longer support for symmertic encryption from version 1.0.0.
- Console examples and Unit testing refactored.
- Support for X509Certificate2.
- Ability to encrypt and decrypt files like, images, word, excel etc.
- Improvement documentation
Issues
Please report issues here.
How to use
Short intro
The library can be used in 2 ways:
- Symmetric way (Only supported in Windows)
- Asymmetric way
Symmetric way
You use the same key (any secret key) for encryption and decryption.
Asymmetric way
With asymmetric way, the library can use its own self-generated RSA key pairs (Private/Public key) to encrypt and decrypt content.
You can store the private key on one or more machines. The public key can easily distribute to all clients.
Note: Please be aware of not to distribute private key publicly and keep it in a safe place. If private key mistakenly gets exposed, you need to re-issue new keys. The content that is already encrypted with private key, can not be decrypted back with the new generated private key. So before updating private key or deleting the old key ensure all your content are decrypted, other wise you lose the content.
It is also possible to use asymmetric keys of X509 Certificate instead of generating your own keys.
The main concept with asymmetric encryption, is that you have a Private and Public key. You use Public key to encrypt the content with and use Private key to decrypt the content back again.
You find the comlete and all examples of both ways here.
Here is some of the examples:
Examples
Example: Encrypt and Decrypt Content With Symmetric Key (Only windows)
var symmetricKey = "AnySecretKey";
ICryptoNet encryptClient = new CryptoNet(symmetricKey, true);
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(symmetricKey, true);
var decrypt = decryptClient.DecryptToString(encrypt);
Console.WriteLine($"3- And we will decrypt it back to:\n{decrypt}\n");
Example: Encrypt and Decrypt Content With Self-Generated Asymmetric Key
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("2- And use the same certificate to decrypt");
Console.WriteLine(decryptWithPrivateKey);
Example: Use X509 certificate to Encrypt with Public Key and later Decrypt with Private Key
// Find and replace CN=Maytham with your own certificate
X509Certificate2? certificate = CryptoNetUtils.GetCertificateFromStore("CN=Maytham");
ICryptoNet cryptoNetWithPublicKey = new CryptoNet(certificate, KeyHelper.KeyType.PublicKey);
var encryptWithPublicKey = cryptoNetWithPublicKey.EncryptFromString(ConfidentialDummyData);
Console.WriteLine($"1- We get public key from Certificate to encrypt following text:\n{ConfidentialDummyData}\n");
Console.WriteLine($"2- To:\n{CryptoNetUtils.BytesToString(encryptWithPublicKey)}\n");
ICryptoNet cryptoNetWithPrivateKey = new CryptoNet(certificate, KeyHelper.KeyType.PrivateKey);
var decryptWithPrivateKey = cryptoNetWithPrivateKey.DecryptToString(encryptWithPublicKey);
Console.WriteLine($"3- And we get private key from Certificate to decrypt it back to:\n{decryptWithPrivateKey}");
Build and Testing
You have different options to build and run unit test from:
- Visual Studio 2019/2022.
- dotnet command line.
- Powershell, run build.ps1 from solution folder.
- 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, adding new feature or even documentation improvement and enhancemnet, you are more than welcome to contribute.
How to contribute:
Here is a link to learn how to contribute if you are not a ware of how to do it.
Product | Versions 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. |
-
.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.