Exemplo n.º 1
0
        public void TestCreateFromKeyFile()
        {
            string    password      = "******";
            string    wrongpassword = password + "wrong";
            AesHelper aesHelper     = AesHelper.CreateFromPassword(password);

            byte[] keyfile = aesHelper.CreateKeyFile();

            // Test wrong password
            AesHelper aesHelper2;

            Assert.ThrowsException <AesHelper.PasswordIncorrectException>(
                () => AesHelper.CreateFromKeyFile(keyfile, wrongpassword));

            // Test correct password
            aesHelper2 = AesHelper.CreateFromKeyFile(keyfile, password);
            Assert.IsNotNull(aesHelper2);

            // NOTE: Do not test equivalent encryption, IV is random
            // so multiple encryptions will return different files

            // Test Equivalent decryption
            byte[] data = new byte[100];
            CoreTest.RandomData(data);
            byte[] decrypted = aesHelper.DecryptBytes(aesHelper.EncryptBytes(data));
            Assert.IsTrue(decrypted.SequenceEqual(data));
            Assert.IsTrue(decrypted
                          .SequenceEqual(aesHelper2.DecryptBytes(aesHelper2.EncryptBytes(data))));
            byte[] decrypted2 = aesHelper2.DecryptBytes(aesHelper.EncryptBytes(data));
            decrypted2.SequenceEqual(data);
            Assert.IsTrue(decrypted2
                          .SequenceEqual(aesHelper.DecryptBytes(aesHelper2.EncryptBytes(data))));
        }
Exemplo n.º 2
0
        public void TestAesHelperReconstruct()
        {
            string    password  = "******";
            AesHelper aesHelper = AesHelper.CreateFromPassword(password);

            byte[] keyfile = aesHelper.CreateKeyFile();

            // Test correct password
            AesHelper aesHelperDirect = new AesHelper(aesHelper.DataKeyKey, aesHelper.PasswordSalt, aesHelper.DataKeyKeyHash,
                                                      aesHelper.DataKeyKeyHashSalt, aesHelper.DataKeyAesProvider, aesHelper.DataKeyIV, aesHelper.DataKey);

            Assert.IsNotNull(aesHelperDirect);

            Assert.IsTrue(aesHelper.DataKeyKey.SequenceEqual(aesHelper.DataKeyAesProvider.Key));
            Assert.IsTrue(aesHelper.DataKeyIV.SequenceEqual(aesHelper.DataKeyAesProvider.IV));

            RijndaelManaged dataKeyAesProvider = AesHelper.CreateDataKeyAesProvider(aesHelper.DataKeyKey);

            dataKeyAesProvider.IV = aesHelper.DataKeyIV;
            byte[] decryptedAesDataKey = AesHelper.DecryptAesDataKey(aesHelper.EncryptAesDataKey(), dataKeyAesProvider);
            Assert.IsTrue(decryptedAesDataKey.SequenceEqual(aesHelper.DataKey));

            //AesHelper aesHelperFromFile = AesHelper.CreateFromKeyFile(keyfile, password);
            AesHelper aesHelperFromFile = AesHelper.CreateFromKeyFile(aesHelper.PasswordSalt, aesHelper.DataKeyKeyHashSalt,
                                                                      aesHelper.DataKeyIV, aesHelper.DataKeyKeyHash, aesHelper.EncryptAesDataKey(), password);

            Assert.IsTrue(aesHelperDirect.DataKeyKey.SequenceEqual(aesHelperFromFile.DataKeyKey));
            Assert.IsTrue(aesHelperDirect.PasswordSalt.SequenceEqual(aesHelperFromFile.PasswordSalt));
            Assert.IsTrue(aesHelperDirect.DataKeyKeyHash.SequenceEqual(aesHelperFromFile.DataKeyKeyHash));
            Assert.IsTrue(aesHelperDirect.DataKeyKeyHashSalt.SequenceEqual(aesHelperFromFile.DataKeyKeyHashSalt));
            Assert.IsTrue(aesHelperDirect.DataKeyIV.SequenceEqual(aesHelperFromFile.DataKeyIV));
            Assert.IsTrue(aesHelperDirect.DataKey.SequenceEqual(aesHelperFromFile.DataKey));

            // NOTE: Do not test equivalent encryption, IV is random
            // so multiple encryptions will return different files

            // Test Equivalent decryption
            byte[] data = new byte[100];
            CoreTest.RandomData(data);
            byte[] decrypted = aesHelper.DecryptBytes(aesHelper.EncryptBytes(data));
            Assert.IsTrue(decrypted.SequenceEqual(data));
            Assert.IsTrue(decrypted
                          .SequenceEqual(aesHelperDirect.DecryptBytes(aesHelperDirect.EncryptBytes(data))));
            byte[] decrypted2 = aesHelperDirect.DecryptBytes(aesHelper.EncryptBytes(data));
            decrypted2.SequenceEqual(data);
            Assert.IsTrue(decrypted2
                          .SequenceEqual(aesHelper.DecryptBytes(aesHelperDirect.EncryptBytes(data))));
        }