Skip to content

Marfusios/MKEncryptor.Core

Repository files navigation

MKEncryptor.Core

Encryption portable library/wrapper for C#. It is used in commercial product MKEncryptor.

  • 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 - open source cryptography library
      • AES
      • AES Fast
      • Blowfish
      • Camellia
      • Des
      • Gost28147
      • Serpent
      • Twofish
  • Sample:

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):
class Program
{
    static void Main(string[] args)
    {
        var modelManager = new MKModelManager(new MKRawDataProviderStatic());
        var encryptor = new MKEncryptor();
        
        ...
    }
}

Show list of all provided ciphers:

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):

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:

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):

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
public interface IMKRawDataProvider
{
    Task<string> GetJsonString(string key);
    Task<bool> SaveJsonString(string key, string json);
}
  • Example of custom file raw data provider:
class MyFileDataProvider : IMKRawDataProvider
{
    public Task<string> 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<bool> SaveJsonString(string key, string json)
    {
        File.WriteAllText(key, json);
        return Task.FromResult(true);
    }
}
  • Use it while creating MKModelManager instance:
class Program
{
    static void Main(string[] args)
    {
        var modelManager = new MKModelManager(new MyFileDataProvider());
        var encryptor = new MKEncryptor();
        
        ...
    }
}

Check unit tests for more info

About

Encryption portable library/wrapper for C#

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages