예제 #1
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))));
        }