public void GetFileContentTest()
        {
            // Arrange
            byte[] derivedKey = new byte[16] {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 255
            };
            byte[] initialCounter = new byte[] { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };

            SettingsAES_CTR settingsAES_CTR = new SettingsAES_CTR(initialCounter);

            SymmetricKeyAlgorithm skaAES_CTR = new SymmetricKeyAlgorithm(SymmetricEncryptionAlgorithm.AES_CTR, 128, settingsAES_CTR);

            string filename = "nice.pdf";

            byte[] fileContent = new byte[] { 1, 2, 3, 1, 2, byte.MaxValue, 0, 0, 0, 0, 0, 0 };

            FileEntry fe = new FileEntry(filename, fileContent);

            FileEntrySecret fes = new FileEntrySecret(fe, "does not matter", skaAES_CTR, derivedKey);

            // Act
            byte[] rtFileContent = fes.GetFileContent(derivedKey);

            // Assert
            CollectionAssert.AreEqual(fileContent, rtFileContent);
        }
        public void SetFileContentTest()
        {
            // Arrange
            byte[] derivedKey = new byte[16] {
                111, 222, 31, 4, 5, 6, 7, 8, 98, 10, 11, 12, 131, 104, 15, 16
            };

            SymmetricKeyAlgorithm ska = SymmetricKeyAlgorithm.GenerateNew(SymmetricEncryptionAlgorithm.AES_CTR);

            string filename = "backup.zip";

            byte[] fileContent = Encoding.UTF8.GetBytes("peace, happiness, freedom and something...");

            FileEntry fileEntry = new FileEntry(filename, fileContent);

            FileEntrySecret fes = new FileEntrySecret(fileEntry, "does not matter", ska, derivedKey);

            byte[] fileContent1 = Encoding.UTF8.GetBytes("this is a wrong backup for this situation");

            // Act
            bool shouldBeTrue = fes.SetFileContent(fileContent1, derivedKey);

            byte[] fileContent2  = fes.GetFileContent(derivedKey);
            bool   shouldBeFalse = fes.SetFileContent(fileContent1, new byte[] { 1, 2, 3 });

            // Assert
            Assert.IsTrue(shouldBeTrue);
            Assert.IsFalse(shouldBeFalse);
            CollectionAssert.AreEqual(fileContent1, fileContent2);
        }
示例#3
0
    private void EditFileInCollection(int zeroBasedIndexNumber, bool isNowSecure, bool wasSecurityModified, string keyIdentifier)
    {
        if (wasSecurityModified)
        {
            // If file jumps from secure <-> unsecure
            if (isNowSecure)
            {
                this.csc.AddFileEntrySecret(this.derivedPasswords[keyIdentifier], this.csc.files[zeroBasedIndexNumber], keyIdentifier);
                this.csc.files.RemoveAt(zeroBasedIndexNumber);
            }
            else
            {
                FileEntrySecret fes             = this.csc.fileSecrets[zeroBasedIndexNumber];
                byte[]          derivedPassword = derivedPasswords[fes.GetKeyIdentifier()];
                FileEntry       tempFileEntry   = new FileEntry(fes.GetFilename(derivedPassword), fes.GetFileContent(derivedPassword));
                this.csc.fileSecrets.RemoveAt(zeroBasedIndexNumber);
                this.csc.files.Add(tempFileEntry);
            }
        }
        else
        {
            if (isNowSecure)
            {
                // TODO: support key change
            }
        }

        // Editing a file modifies the structure
        this.isModified = true;
        this.UpdateMainTitle(this.filePath != null ? this.filePath : untitledTempName);

        this.GenerateFileSimplifiedsFromCommonSecrets();
    }