Skip to content

Commit 15018c4

Browse files
author
May
committed
Update README.md
1 parent d332235 commit 15018c4

File tree

1 file changed

+136
-3
lines changed

1 file changed

+136
-3
lines changed

README.md

Lines changed: 136 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,142 @@
1+
![.NET Core](https://github.com/MayMeow/MayMeow.Cryptography/workflows/.NET%20Core/badge.svg)
2+
13
# MayMeow.Cryptography
24

3-
Library to encrypt /decrypt data
5+
Wrapper arround .NET Cryptography library.
46

5-
![.NET Core](https://github.com/MayMeow/MayMeow.Cryptography/workflows/.NET%20Core/badge.svg)
7+
## Installation
8+
9+
This library can be installed to your project with NuGet package manager
10+
11+
```powershell
12+
Install-Package MayMeow.Cryptography -Version 1.1.0
13+
```
14+
15+
or with dotnet cli
16+
17+
```powershell
18+
dotnet add package MayMeow.Cryptography --version 1.1.0
19+
```
20+
21+
For more installation methods refer [NuGet page](https://www.nuget.org/packages/MayMeow.Cryptography) of this project.
22+
23+
## Usage
24+
25+
### Using this library
26+
27+
Use in you project
28+
29+
```csharp
30+
using MayMeow.Cryptography;
31+
```
32+
33+
### AES encryption (symmetric one)
34+
35+
Initialize aes and generate new key and IV. AES is an symmetric encryption which using same key to encrypt and decrypt.
36+
37+
```csharp
38+
AES aes = new AES();
39+
40+
string AesKey = aes.GetAesKey();
41+
string AesIV = aes.GetIV();
42+
```
43+
44+
To encrypt your text use
45+
46+
```csharp
47+
string AesEncrypted = AES.Encrypt(message, AesKey, AesIV);
48+
```
49+
50+
and simillarly to decrypt use
51+
52+
```csharp
53+
string AesDecrypted = AES.Decrypt(AesEncrypted, AesKey);
54+
```
55+
56+
Example above using generated and unprotected key for your encryption.
57+
58+
### RSA Encryption (asymmetric one)
59+
60+
First initialize RSA and create your public and private key
61+
62+
```csharp
63+
RSA rsa = new RSA(RSA.KEY_SIZE);
64+
65+
string pubKey = TextConversion.Base64Encode(rsa.GetPublicKey());
66+
string privKey = TextConversion.Base64Encode(rsa.GetPrivateKey());
67+
```
68+
69+
Now encryption is easy as
70+
71+
```csharp
72+
string message = "Hello world";
73+
string encryptedText = RSA.Encrypt(message, RSA.SetKeyFromString(pubKey));
74+
string plainText = RSA.Decrypt(encryptedText, RSA.SetKeyFromString(privKey));
75+
```
76+
77+
### AES GCM encryption with protected key (combination of asymmetric and symmetric one)
78+
79+
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.
80+
81+
#### Initialize RSA keys
82+
83+
```csharp
84+
RSA rsa = new RSA(RSA.KEY_SIZE);
85+
86+
string pubKey = TextConversion.Base64Encode(rsa.GetPublicKey());
87+
string privKey = TextConversion.Base64Encode(rsa.GetPrivateKey());
88+
```
89+
90+
Initialize key and aad for GCM encryption
91+
92+
```csharp
93+
// Create AES Keys
94+
byte[] key = new byte[16];
95+
RandomNumberGenerator.Fill(key);
96+
97+
byte[] aad = new byte[32];
98+
RandomNumberGenerator.Fill(aad);
99+
```
100+
101+
Now secure your key
102+
103+
```csharp
104+
byte[] encryptedAeskey = RSA.EncryptBytes(key, RSA.SetKeyFromString(pubKey));
105+
```
106+
107+
before using it you have to decrypt it
108+
109+
```csharp
110+
byte[] decryptedAesKey = RSA.DecryptBytes(encryptedAeskey, RSA.SetKeyFromString(privKey));
111+
```
112+
113+
Key above was secured with asymmetric cryptography. **Never share your private key with anyone.**
114+
115+
Now encryption is simmilar as in our first example
116+
117+
```csharp
118+
byte[] encryptedData = GCM.Encrypt(dataToEncrypt, key, aad);
119+
byte[] decryptedData = GCM.Decrypt(encryptedData, decryptedAesKey, aad);
120+
```
121+
122+
If you want to encrypt string you have to do it as follows
123+
124+
```csharp
125+
byte[] encryptedStringData = GCM.Encrypt(Encoding.UTF8.GetBytes(stringToEncrypt), key, aad);
126+
```
127+
128+
For decryption is it same as above.
129+
130+
## Key derivation with PBKDF2
131+
132+
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](https://en.wikipedia.org/wiki/PBKDF2)
133+
134+
to derive key use following snippet
6135

7-
[![Build Status](http://meow/MeowSoft/MayMeow.Cryptography/_apis/build/status/MayMeow.Cryptography?branchName=master)](http://meow/MeowSoft/MayMeow.Cryptography/_build/latest?definitionId=1&branchName=master)
136+
```csharp
137+
// string password = "my$up3r$3cr3tP4$$w0rd1";
138+
// string salt = "8VySCxa42j9McSqGjZxCVQnH4x4rSZszEL9YQT3VkZ75xbBD";
139+
var derivedKey = PBKDF2.keyDerivate(password, salt, 1024, 10);
140+
```
8141

9142
License MIT

0 commit comments

Comments
 (0)