MayMeow.Cryptography 1.2.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package MayMeow.Cryptography --version 1.2.0
NuGet\Install-Package MayMeow.Cryptography -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="MayMeow.Cryptography" Version="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MayMeow.Cryptography --version 1.2.0
#r "nuget: MayMeow.Cryptography, 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 MayMeow.Cryptography as a Cake Addin
#addin nuget:?package=MayMeow.Cryptography&version=1.2.0

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

.NET Core

MayMeow.Cryptography

Wrapper arround .NET Cryptography library.

Installation

This library can be installed to your project with NuGet package manager

Install-Package MayMeow.Cryptography -Version 1.1.0

or with dotnet cli

dotnet add package MayMeow.Cryptography --version 1.1.0

For more installation methods refer NuGet page of this project.

Usage

Using this library

Use in you project

using MayMeow.Cryptography;

AES encryption (symmetric one)

Initialize aes and generate new key and IV. AES is an symmetric encryption which using same key to encrypt and decrypt.

AES aes = new AES();

string AesKey = aes.GetAesKey();
string AesIV = aes.GetIV();

To encrypt your text use

string AesEncrypted = AES.Encrypt(message, AesKey, AesIV);

and simillarly to decrypt use

string AesDecrypted = AES.Decrypt(AesEncrypted, AesKey);

Example above using generated and unprotected key for your encryption.

RSA Encryption (asymmetric one)

First initialize RSA and create your public and private key

RSA rsa = new RSA(RSA.KEY_SIZE);

string pubKey = TextConversion.Base64Encode(rsa.GetPublicKey());
string privKey = TextConversion.Base64Encode(rsa.GetPrivateKey());

Now encryption is easy as

string message = "Hello world";
string encryptedText = RSA.Encrypt(message, RSA.SetKeyFromString(pubKey));
string plainText = RSA.Decrypt(encryptedText, RSA.SetKeyFromString(privKey));

AES GCM encryption with protected key (combination of asymmetric and symmetric one)

This is more advande example where key for encryption is protected with RSA. RSA is asymetric encryption where public key is used for encryption your data and for decryption is used private key which is in most time also protected by password. Private key has only its owner.

Initialize RSA keys
RSA rsa = new RSA(RSA.KEY_SIZE);

string pubKey = TextConversion.Base64Encode(rsa.GetPublicKey());
string privKey = TextConversion.Base64Encode(rsa.GetPrivateKey());

Initialize key and aad for GCM encryption

// Create AES Keys
byte[] key = new byte[16];
RandomNumberGenerator.Fill(key);

byte[] aad = new byte[32];
RandomNumberGenerator.Fill(aad);

Now secure your key

byte[] encryptedAeskey = RSA.EncryptBytes(key, RSA.SetKeyFromString(pubKey));

before using it you have to decrypt it

byte[] decryptedAesKey = RSA.DecryptBytes(encryptedAeskey, RSA.SetKeyFromString(privKey));

Key above was secured with asymmetric cryptography. Never share your private key with anyone.

Now encryption is simmilar as in our first example

byte[] encryptedData = GCM.Encrypt(dataToEncrypt, key, aad);
byte[] decryptedData = GCM.Decrypt(encryptedData, decryptedAesKey, aad);

If you want to encrypt string you have to do it as follows

byte[] encryptedStringData = GCM.Encrypt(Encoding.UTF8.GetBytes(stringToEncrypt), key, aad);

For decryption is it same as above.

Key derivation with PBKDF2

This function is used to derive you key (for example for unlocking private key) from your password. You can read more about it on Wikipedia

to derive key use following snippet

// string password = "my$up3r$3cr3tP4$$w0rd1";
// string salt = "8VySCxa42j9McSqGjZxCVQnH4x4rSZszEL9YQT3VkZ75xbBD";
var derivedKey = PBKDF2.keyDerivate(password, salt, 1024, 10);

License MIT

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.3.0 774 10/17/2022
1.2.0 458 8/11/2022
1.1.0 459 10/16/2021
1.0.10 1,077 11/3/2020
1.0.9 685 2/6/2020
1.0.8 488 2/6/2020
1.0.7 471 2/6/2020
1.0.6 497 2/6/2020
1.0.4 481 11/15/2019
1.0.3 452 11/15/2019
1.0.2 454 11/15/2019
1.0.1 447 11/15/2019
1.0.0 499 11/15/2019

Change target frameowrks to .net 6.0