Add some tests for the AesGcm class

This commit is contained in:
David Sparer
2016-07-11 14:17:50 -06:00
parent 8d638602b9
commit e92c1eaa9d
4 changed files with 71 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
using System.Security;
using mRemoteNG.Security;
using NUnit.Framework;
namespace mRemoteNGTests.Security
{
public class AesGcmTests
{
private SecureString _encryptionKey;
private string _plainText;
[SetUp]
public void Setup()
{
_encryptionKey = "mypassword111111".ConvertToSecureString();
_plainText = "MySecret!";
}
[TearDown]
public void TearDown()
{
}
[Test]
public void GetBlockSizeReturnsProperValueForAes()
{
Assert.That(AESGCM.BlockSizeInBytes, Is.EqualTo(16));
}
[Test]
public void EncryptionOutputsBase64String()
{
var cipherText = AESGCM.Encrypt(_plainText, _encryptionKey);
Assert.That(cipherText.IsBase64String, Is.True);
}
[Test]
public void DecryptedTextIsEqualToOriginalPlainText()
{
var cipherText = AESGCM.Encrypt(_plainText, _encryptionKey);
var decryptedCipherText = AESGCM.Decrypt(cipherText, _encryptionKey);
Assert.That(decryptedCipherText, Is.EqualTo(_plainText));
}
[Test]
public void EncryptingTheSameValueReturnsNewCipherTextEachTime()
{
var cipherText1 = AESGCM.Encrypt(_plainText, _encryptionKey);
var cipherText2 = AESGCM.Encrypt(_plainText, _encryptionKey);
Assert.That(cipherText1, Is.Not.EqualTo(cipherText2));
}
}
}

View File

@@ -105,6 +105,7 @@
<Compile Include="Config\Connections\SqlUpdateTimerTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Security\AesCryptographyProviderTests.cs" />
<Compile Include="Security\AesGcmTests.cs" />
<Compile Include="UI\Controls\CustomListViewTests.cs" />
<Compile Include="UI\Controls\TestForm.cs">
<SubType>Form</SubType>

View File

@@ -7,6 +7,7 @@
using System;
using System.IO;
using System.Security;
using System.Text;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
@@ -32,6 +33,8 @@ namespace mRemoteNG.Security
public static readonly int Iterations = 10000;
public static readonly int MinPasswordLength = 12;
public static int BlockSizeInBytes => 16;
/// <summary>
/// Helper that generates a random new key on each call.
@@ -44,6 +47,18 @@ namespace mRemoteNG.Security
return key;
}
public static string Encrypt(string plainText, SecureString encryptionKey)
{
var encryptedText = SimpleEncryptWithPassword(plainText, encryptionKey.ConvertToUnsecureString());
return encryptedText;
}
public static string Decrypt(string cipherText, SecureString decryptionKey)
{
var decryptedText = SimpleDecryptWithPassword(cipherText, decryptionKey.ConvertToUnsecureString());
return decryptedText;
}
/// <summary>
/// Simple Encryption And Authentication (AES-GCM) of a UTF8 string.
/// </summary>

View File

@@ -168,6 +168,7 @@
<Compile Include="Connection\Protocol\VNC\VNCEnum.cs" />
<Compile Include="Messages\MessageClassEnum.cs" />
<Compile Include="Security\AesCryptographyProvider.cs" />
<Compile Include="Security\AESGCM.cs" />
<Compile Include="Security\BouncyCastleCryptographyEngine.cs" />
<Compile Include="Security\Crypt.cs" />
<Compile Include="Security\Encryptor.cs" />