Exemple #1
0
        public void TestEncryptionAndDecryption()
        {
            var key       = AesEncryptionProvider.InitializeEncryptionKey();
            var encrypted = AesEncryptionProvider.Encrypt(key, Data);
            var decrypted = AesEncryptionProvider.Decrypt(key, encrypted);

            Assert.AreEqual(Data, decrypted);
        }
        public void Should_return_original_text_when_decrypting_with_same_key()
        {
            var inputText = "this is some text";
            var encText   = provider.Encrypt(inputText);

            var result = provider.Decrypt(encText);

            result.ShouldEqual(inputText);
        }
Exemple #3
0
        public IAsyncOperation <List <Account> > GetAvailableAccountsAsync()
        {
            return(Task.Run(async() =>
            {
                if (!_isInitialized)
                {
                    await InitializeStorageAsync();
                }
                if (IsRoamingStorageEncrypted && String.IsNullOrEmpty(LocalEncryptionTransferKey))
                {
                    throw new InvalidOperationException("The storage is encrypted but no key is provided.");
                }

                if (!IsRoamingStorageEncrypted)
                {
                    throw new SecurityException("NSA is watching you");
                }

                var accountList = new List <Account>();

                foreach (var file in await _roamingFolder.GetFilesAsync())
                {
                    var stream = await file.OpenAsync(FileAccessMode.Read);
                    var streamreader = new StreamReader(stream.AsStream());
                    var encryptedData = streamreader.ReadToEnd();

                    var data = AesEncryptionProvider.Decrypt(LocalEncryptionTransferKey, encryptedData);
                    JsonObject accountJsonObject;

                    if (!JsonObject.TryParse(data, out accountJsonObject))
                    {
                        continue;
                    }
                    if (accountJsonObject.ContainsKey("AccountName") &&
                        accountJsonObject.ContainsKey("AccountKeyBase32") &&
                        accountJsonObject.ContainsKey("AccountIcon"))
                    {
                        accountList.Add(new Account
                        {
                            AccountName = accountJsonObject.GetNamedString("AccountName"),
                            AccountKeyBase32 = accountJsonObject.GetNamedString("AccountKeyBase32"),
                            AccountIcon = accountJsonObject.GetNamedString("AccountIcon")
                        });
                    }
                }

                return accountList;
            }).AsAsyncOperation());
        }
Exemple #4
0
        public void AesEncryptionProvider_Encrypt_Decrypt_PlainText()
        {
            // arrange
            const string expected = "This is some plain text. This is some plain text. This is some plain text. This is some plain text. This is some plain text. This is some plain text. This is some plain text. This is some plain text. This is some plain text. This is some plain text. This is some plain text. This is some plain text. This is some plain text. This is some plain text. This is some plain text. This is some plain text. This is some plain text. This is some plain text.";
            var          aes      = new AesEncryptionProvider(new TestCryptoKeyProvider("9781908006028",
                                                                                        new Guid("F588C946-386F-4A49-A333-64A189D07DD4")));

            // act
            var encrypted = aes.Encrypt(Encoding.UTF8.GetBytes(expected));
            var decrypted = aes.Decrypt(encrypted);
            var actual    = Encoding.ASCII.GetString(decrypted);

            // assert
            Assert.AreEqual(actual, expected);
        }
Exemple #5
0
        public void EncryptionTests()
        {
            var key = AesEncryptionProvider.GenerateKey();

            Assert.IsNotNull(key);
            Assert.IsTrue(key.Length > 0);

            var          aes       = new AesEncryptionProvider(key);
            const string toEncrypt = "A man, a plan, a canal. Panama.";

            var encrypted = aes.Encrypt(toEncrypt);

            Assert.IsNotNull(encrypted);
            Assert.IsTrue(encrypted.Length > 0);
            Assert.AreNotEqual(toEncrypt, encrypted);

            var decrypted = aes.Decrypt(encrypted);

            Assert.IsNotNull(decrypted);
            Assert.IsTrue(decrypted.Length > 0);
            Assert.AreEqual(toEncrypt, decrypted);
        }