MayMeow.Cryptography
1.2.0
Prefix Reserved
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
<PackageReference Include="MayMeow.Cryptography" Version="1.2.0" />
paket add MayMeow.Cryptography --version 1.2.0
#r "nuget: MayMeow.Cryptography, 1.2.0"
// 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
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 | Versions 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. |
-
net6.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Change target frameowrks to .net 6.0