Modified the ICryptographyProvider interface to require getters for the cipher engine and mode using their enum types

This commit is contained in:
David Sparer
2016-10-19 17:17:28 -06:00
parent 2df182c4c9
commit 550119a53a
4 changed files with 55 additions and 4 deletions

View File

@@ -81,5 +81,37 @@ namespace mRemoteNGTests.Security
ActualValueDelegate<string> decryptMethod = () => _cryptographyProvider.Decrypt(cipherText, "wrongKey".ConvertToSecureString());
Assert.That(decryptMethod, Throws.TypeOf<EncryptionException>());
}
[TestCaseSource(typeof(TestCaseSources), nameof(TestCaseSources.AllEngineAndModeCombos))]
public void GetCipherEngine(BlockCipherEngines engine, BlockCipherModes mode)
{
var cryptoProvider = new CryptographyProviderFactory().CreateAeadCryptographyProvider(engine, mode);
Assert.That(cryptoProvider.CipherEngine, Is.EqualTo(engine));
}
[TestCaseSource(typeof(TestCaseSources), nameof(TestCaseSources.AllEngineAndModeCombos))]
public void GetCipherMode(BlockCipherEngines engine, BlockCipherModes mode)
{
var cryptoProvider = new CryptographyProviderFactory().CreateAeadCryptographyProvider(engine, mode);
Assert.That(cryptoProvider.CipherMode, Is.EqualTo(mode));
}
private class TestCaseSources
{
public static IEnumerable AllEngineAndModeCombos
{
get
{
foreach (var engine in Enum.GetValues(typeof(BlockCipherEngines)))
{
foreach (var mode in Enum.GetValues(typeof(BlockCipherModes)))
{
yield return new TestCaseData(engine, mode);
}
}
}
}
}
}
}

View File

@@ -6,7 +6,9 @@ namespace mRemoteNG.Security
{
int BlockSizeInBytes { get; }
string CipherEngine { get; }
BlockCipherEngines CipherEngine { get; }
BlockCipherModes CipherMode { get; }
string Encrypt(string plainText, SecureString encryptionKey);

View File

@@ -38,7 +38,23 @@ namespace mRemoteNG.Security.SymmetricEncryption
public int BlockSizeInBytes => _aeadBlockCipher.GetBlockSize();
public string CipherEngine => _aeadBlockCipher.AlgorithmName;
public BlockCipherEngines CipherEngine
{
get
{
var cipherEngine = _aeadBlockCipher.AlgorithmName.Split('/')[0];
return (BlockCipherEngines)Enum.Parse(typeof(BlockCipherEngines), cipherEngine);
}
}
public BlockCipherModes CipherMode
{
get
{
var cipherMode = _aeadBlockCipher.AlgorithmName.Split('/')[1];
return (BlockCipherModes)Enum.Parse(typeof(BlockCipherModes), cipherMode);
}
}
public AeadCryptographyProvider()
{

View File

@@ -13,11 +13,12 @@ namespace mRemoteNG.Security.SymmetricEncryption
{
public int BlockSizeInBytes { get; }
public string CipherEngine { get; }
public BlockCipherEngines CipherEngine { get; }
public BlockCipherModes CipherMode { get; }
public LegacyRijndaelCryptographyProvider()
{
CipherEngine = "Rijndael";
BlockSizeInBytes = 16;
}