https://github.com/marfusios/mkencryptor.core
Encryption portable library/wrapper for C#
https://github.com/marfusios/mkencryptor.core
Last synced: 8 months ago
JSON representation
Encryption portable library/wrapper for C#
- Host: GitHub
- URL: https://github.com/marfusios/mkencryptor.core
- Owner: Marfusios
- Created: 2016-05-03T11:57:22.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-05-03T12:02:33.000Z (about 10 years ago)
- Last Synced: 2025-02-23T02:33:22.446Z (over 1 year ago)
- Language: C#
- Size: 3.43 MB
- Stars: 0
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# MKEncryptor.Core
Encryption portable library/wrapper for C#. It is used in commercial product [MKEncryptor](https://www.microsoft.com/en-us/store/apps/mk-encryptor/9nblggh682ch).
* How encryption works
1. Initial vector (IV) and salt (128b) is randomly generated (by SecureRandom)
2. Password is generated by Pkcs5S2ParametersGenerator via 13333 iteration count (it uses users password and salt)
3. Password generating produces derived mac parameters (HMAC)
4. Check if HMAC is valid
5. Data are encrypted by selected provider and cipher
* Currently supported providers and ciphers:
* [Bouncy Castle](https://www.bouncycastle.org/) - open source cryptography library
* AES
* AES Fast
* Blowfish
* Camellia
* Des
* Gost28147
* Serpent
* Twofish
* Sample:
```C#
class Program
{
static void Main(string[] args)
{
var encryptor = new MKEncryptor();
var camelliaCipher = encryptor.FindCipherBy("camellia", "bouncy_castle");
var password = "my password";
var plainText = "my secret text";
var encrypted = encryptor.Encrypt(plainText, password, cipher, MKKeySize.Key256);
var decrypted = encryptor.Decrypt(encrypted, password, cipher, MKKeySize.Key256);
// decrypted == plainText
}
}
```
* Minimal targets
* .NET Framework 4.5
* .NET Core 1.0
* Windows 8
* Windows Phone 8.1
___
## How to use:
**Initialization:**
* Add reference to MKEncryptor_Interfaces and MKEncryptor_Core
* In your class (or console app):
```C#
class Program
{
static void Main(string[] args)
{
var modelManager = new MKModelManager(new MKRawDataProviderStatic());
var encryptor = new MKEncryptor();
...
}
}
```
**Show list of all provided ciphers:**
```C#
class Program
{
static void Main(string[] args)
{
var modelManager = new MKModelManager(new MKRawDataProviderStatic());
var encryptor = new MKEncryptor();
foreach (var cipher in encryptor.ProvidedCiphers)
{
Console.WriteLine("{0} Supported keys: {1}", cipher.DisplayName, MKEnumerableHelper.ArrayToString(cipher.SupportedKeySizes));
}
}
}
```
**Add text or register file, directory (with choosing ciphers):**
```C#
class Program
{
static void Main(string[] args)
{
var modelManager = new MKModelManager(new MKRawDataProviderStatic());
var camelliaCipher = encryptor.FindCipherBy("camellia", "bouncy_castle");
var otherCipher = encryptor.ProvidedCiphers.First();
var text = new MKEncryptionText(...);
text.AddUsedCipherAtLastPosition(MKUsedCipher.CreateFrom(camelliaCipher, MKKeySize.Key256));
text.AddUsedCipherAtLastPosition(MKUsedCipher.CreateFrom(otherCipher, MKKeySize.Key256));
var file = new MKEncryptionFile(...);
file.AddUsedCipherAtLastPosition(MKUsedCipher.CreateFrom(camelliaCipher, MKKeySize.Key256));
file.AddUsedCipherAtLastPosition(MKUsedCipher.CreateFrom(otherCipher, MKKeySize.Key256));
var directory = new MKEncryptionDir(...);
directory.AddUsedCipherAtLastPosition(MKUsedCipher.CreateFrom(camelliaCipher, MKKeySize.Key256));
directory.AddUsedCipherAtLastPosition(MKUsedCipher.CreateFrom(otherCipher, MKKeySize.Key256));
modelManager.Texts.Add(text);
modelManager.Files.Add(file);
modelManager.Dirs.Add(directory);
modelManager.SaveChanges()
}
}
```
**Simple encryption and decryption:**
```C#
class Program
{
static void Main(string[] args)
{
var encryptor = new MKEncryptor();
var camelliaCipher = encryptor.FindCipherBy("camellia", "bouncy_castle");
var password = "my password";
var plainText = "my secret text";
var encrypted = encryptor.Encrypt(plainText, password, cipher, MKKeySize.Key256);
var decrypted = encryptor.Decrypt(encrypted, password, cipher, MKKeySize.Key256);
// decrypted == plainText
}
}
```
**Advanced encryption and decryption (files, directories, texts):**
```C#
class Program
{
static void Main(string[] args)
{
var modelManager = new MKModelManager(new MKRawDataProviderStatic());
var encryptor = new MKEncryptor();
var selectedIndex = 0;
var file = modelManager.Files[selectedIndex];
var password = "my secret password";
if (file.State == MKEncryptionState.Decrypted)
{
encryptor.Encrypt(password, file);
}
else
{
encryptor.Decrypt(password, file);
}
}
}
```
___
**Custom raw data provider:**
* Implement interface IMKRawDataProvider
```C#
public interface IMKRawDataProvider
{
Task GetJsonString(string key);
Task SaveJsonString(string key, string json);
}
```
* Example of custom file raw data provider:
```C#
class MyFileDataProvider : IMKRawDataProvider
{
public Task GetJsonString(string key)
{
return Task.Factory.StartNew(() => getJsonStringInternal(key));
}
private static string getJsonStringInternal(string key)
{
try
{
return File.ReadAllText(key);
}
catch (Exception)
{
return string.Empty;
}
}
public Task SaveJsonString(string key, string json)
{
File.WriteAllText(key, json);
return Task.FromResult(true);
}
}
```
* Use it while creating MKModelManager instance:
```C#
class Program
{
static void Main(string[] args)
{
var modelManager = new MKModelManager(new MyFileDataProvider());
var encryptor = new MKEncryptor();
...
}
}
```
### Check unit tests for more info