MayMeow.Cryptography 1.3.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package MayMeow.Cryptography --version 1.3.0
NuGet\Install-Package MayMeow.Cryptography -Version 1.3.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.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MayMeow.Cryptography --version 1.3.0
#r "nuget: MayMeow.Cryptography, 1.3.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.3.0

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

.NET Core

MayMeow.Cryptography

Wrapper arround .NET Cryptography library.

ko-fi

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)

  • Symmetric-key algorithm
  • Approved and used by NSA
  • Much faster than DES and 3DES for bulk data encryption.
  • Original name is Rijndael, named to AES after Advanced Encryption Standard contest.

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)

  • Achieving strong encryption through the use of two large prime numbers Wikipedia
  • Encrypts your data with public key
  • Decrypts your data with private key
  • Solves I want anybody to be able to encrypt a message, but I'm the only one who can decrypt it. I don't want to share decryption keys with anybody.

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. ⚠️ Do not share your private key with anyone!

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.

Sign and verify data using RSA (a.k.a. Digital signature)

  • Sign data using private key
  • Verify data using public key
  • Usually used in cases where it is important to detect forgery or tampering
  • Provides cryptographic way of Authentication, Integrity, Non-repudiation

For more information check this Wikipedia page.

Initialize RSA parameters (a.k.a. get your keys)

First you will need to get your public and private key. You can do this as in RSA encryption. Or you can use random generated one as follows:

RSA rsa = new RSA(RSA.KEY_SIZE);
RSAParameters key = rsa.GetRSAParameters(true);

method above will be available from version 1.3.0 for all RSA related tasks.

Now you can Sign your data like follows. (You are signing your data with your private key)

byte[] signedData = RSA.HashAndSignBytes(dataToSign, key);

// if you using method with keys provided from string use
byte[] signedData = RSA.HashAndSignBytes(dataToSign, RSA.SetKeyFromString(privKey));

And for verification use following lines (You are verifying your data with your Public key)

// will be TRUE if your data wasn't modified from time of your signature, otherwise it will be FALSE
bool isVerified = RSA.VerifySignedHash(dataToSign, key);

// if you using method with keys provided from string use
bool isVerified = RSA.VerifySignedHash(dataToSign, RSA.SetKeyFromString(pubKey));

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 750 10/17/2022
1.2.0 458 8/11/2022
1.1.0 459 10/16/2021
1.0.10 1,053 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