public void Given_ValidKeySize_When_AESKeyGenerated_Then_GetKeySizeMustReturnActualKeySize( [Range(128, 256, 64)] int actualKeySize) { // Arrange var newKey = new AesKeyGenerator().GetNewKey(new AesKeyGenerationOptions() { NewKeyName = "n/a", KeySize = actualKeySize }); // Act var expectedKeySize = newKey.GetKeySize(); // Assert expectedKeySize.Should().Be(actualKeySize); }
public void Given_ValidKeyAndBinaryData_When_EncryptAndDecrypt_Then_DecryptionIsSameResult( [Values(192, 256)] int keySize) { // Arrange const string strToTestWith = "string should be encrypted and come out the same after descryption"; var byteConverter = new UnicodeEncoding(); var strAsBytes = byteConverter.GetBytes(strToTestWith); var algo = new AesAlgo(); // Act var key = new AesKeyGenerator().GetNewKey(new AesKeyGenerationOptions() { NewKeyName = "my new aes key", KeySize = keySize }); var result = algo.Decrypt(algo.Encrypt(strAsBytes, key), key); // Assert byteConverter.GetString(result).Should().Be(strToTestWith); }
public void Given_AesEntries_When_DecryptWithAudit_Then_FilesWrittenWithCorrectContent() { // Arrange // Generate random key const string keyName = "key to test audit"; var aesKeyGen = new AesKeyGenerator(); var aesKey = aesKeyGen.GetNewKey(new AesKeyGenerationOptions() { KeySize = GenerateAesKeyBuilder.DefaultAesKeySize, NewKeyName = keyName }); var keyLoaderMock = new Mock <IKeyLoader <AesKey, KeyFromFileDetails> >(MockBehavior.Strict); keyLoaderMock.Setup(l => l.Load(It.IsAny <KeyFromFileDetails>())) .Returns(aesKey); IUserStringConverter converter = new Utf16LittleEndianUserStringConverter(); const string categoryName = "my category"; const string entryName1 = "my entry"; const string entryName2 = "my entry2"; var datPoco = new EnvCryptDat() { Categories = new[] { new Category() { Name = categoryName, Entries = new[] { new Entry() { Name = entryName1, EncryptedValue = new[] { new byte[1] }, // not important for this test EncryptionAlgorithm = aesKey.Algorithm, KeyName = aesKey.Name, KeyHash = aesKey.GetHashCode() }, new Entry() { Name = entryName2, EncryptedValue = new[] { new byte[1] }, // not important for this test EncryptionAlgorithm = aesKey.Algorithm, KeyName = aesKey.Name, KeyHash = aesKey.GetHashCode() } } } } }; var datLoaderMock = new Mock <IDatLoader <DatFromFileLoaderOptions> >(); datLoaderMock.Setup(l => l.Load(It.IsAny <DatFromFileLoaderOptions>())) .Returns(datPoco); var encryptionAlgoMock = new Mock <ISegmentEncryptionAlgo <AesKey> >(); encryptionAlgoMock.Setup(a => a.Decrypt(It.IsAny <IList <byte[]> >(), aesKey)) .Returns(converter.Encode("not important for this test")); var processName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName); var utcNow = new DateTime(2015, 01, 01); var dateTimeMock = new Mock <IMyDateTime>(); dateTimeMock.Setup(t => t.UtcNow()).Returns(utcNow); var fileInfoFactoryMock = new Mock <IMyFileInfoFactory>(); var fileInfoMock = new Mock <IMyFileInfo>(); fileInfoMock.Setup(f => f.CreationTimeUtc) .Returns(utcNow.AddDays(-2)); fileInfoFactoryMock.Setup(f => f.GetNewInstance(It.Is <string>(s => s.Contains(processName)))) .Returns <string>(s => new MyFileInfo(s)); fileInfoFactoryMock.Setup(f => f.GetNewInstance(It.IsRegex(@".+deleteme"))) .Returns(fileInfoMock.Object); using (var tempDir = new TempDir()) { var loggerConfig = new ToFileAuditLoggerConfig() { LogDirectory = tempDir.TempDirectory, NumberOfDaysSinceCreationToKeep = 1 }; var auditLogger = new ToFileAuditLogger <AesKey, DecryptEntryWorkflowOptions>( loggerConfig, new MyDirectory(), new MyFile(), dateTimeMock.Object, new OldLogCleaner(loggerConfig, new MyDirectory(), new MyFile(), fileInfoFactoryMock.Object)); // Act var deleteFile1Path = Path.Combine(tempDir.TempDirectory, ".deleteme.log"); File.WriteAllText(deleteFile1Path, "somerandomtext"); var deleteFile2Path = Path.Combine(tempDir.TempDirectory, "asd.deleteme.log"); File.WriteAllText(deleteFile2Path, "somerandomtext2"); const string datFilePath = @"C:\my dat file"; new DecryptAesEntryWorkflowBuilder() .WithAesSegmentEncryptionAlgo(encryptionAlgoMock.Object) .WithKeyLoader(keyLoaderMock.Object) .WithDatLoader(datLoaderMock.Object) .WithAuditLogger(auditLogger) .Build().Run(new DecryptEntryWorkflowOptions() { CategoryEntryPair = new[] { new CategoryEntryPair(categoryName, entryName1), new CategoryEntryPair(categoryName, entryName2) }, DatFilePath = datFilePath, KeyFilePaths = new[] { "null" } }); // Assert File.Exists(deleteFile1Path).Should().BeFalse(); File.Exists(deleteFile2Path).Should().BeFalse(); var expectedFilePath = Path.Combine(tempDir.TempDirectory, string.Format(loggerConfig.FileNameFormat, utcNow.ToString(ToFileAuditLogger <AesKey, DecryptEntryWorkflowOptions> .DateTimeFormatInFileName), processName) + loggerConfig.LogFileExtension); File.Exists(expectedFilePath) .Should().BeTrue(); var lines = File.ReadAllLines(expectedFilePath); lines.Should().HaveCount(5); lines[1].Should().Be(datFilePath); lines[3].Should().Be(categoryName + "\t" + entryName1 + "\t" + keyName + "\t" + EnvCryptAlgoEnum.Aes); lines[4].Should().Be(categoryName + "\t" + entryName2 + "\t" + keyName + "\t" + EnvCryptAlgoEnum.Aes); } }