Example #1
0
        public void IncorrectKeyForDecryption()
        {
            var keyGenerator = new AesManaged();

            keyGenerator.GenerateKey();
            var key1 = Convert.ToBase64String(keyGenerator.Key);

            keyGenerator.GenerateKey();
            var key2 = Convert.ToBase64String(keyGenerator.Key);

            Assert.AreNotEqual(key1, key2);

            var plain = _randomWords.Sentence(RandomGen.Next(20, 99));

            var crypto1 = new AesCryptString(key1);
            var cipher  = crypto1.Encrypt(plain);

            var plain2 = crypto1.Decrypt(cipher); // this should work.

            Assert.AreEqual(plain, plain2);
            try
            {
                crypto1.Decrypt(key2, cipher); // this should fail
                Assert.Fail("Expected this decrypt to fail because the key was not provided.");
            }
            catch (CryptographicException error)
            {
                Assert.IsTrue(Regex.IsMatch(error.Message, "can not decrypt this text", RegexOptions.IgnoreCase),
                              error.Message);
            }
        }
Example #2
0
        public void ForgotToInitializeKeyForDecryption()
        {
            var crypto1 = new AesCryptString();

            var plain = _randomWords.Sentence(RandomGen.Next(20, 99));

            var crypt = crypto1.Encrypt(plain);

            var plain2 = crypto1.Decrypt(crypt);

            Assert.AreEqual(plain, plain2);

            // Verify that unset decryption key provides a useful message
            var crypto2 = new AesCryptString();

            try
            {
                crypto2.Decrypt(crypt);
                Assert.Fail("Expected this decrypt to fail because we haven't set the key yet.");
            }
            catch (CryptographicException error)
            {
                Assert.IsTrue(Regex.IsMatch(error.Message, "decryption key not set", RegexOptions.IgnoreCase),
                              error.Message);
            }
        }
Example #3
0
        public void CryptSansKey()
        {
            var crypto = new AesCryptString("Q+pJALEKLqzBYWrctPUz64DXZK9zEQW5NuWOtSdPeCM=");

            var plain = _randomWords.Sentence(RandomGen.Next(20, 99));

            var crypt = crypto.Encrypt(plain);

            var plain2 = crypto.Decrypt(crypt);

            Assert.AreEqual(plain, plain2);
        }
Example #4
0
        public void CryptWithKey()
        {
            var crypto = new AesCryptString();

            var plain = _randomWords.Sentence(RandomGen.Next(20, 99));
            //"The rain in spain stays mainly on the plain. Where is the rain, On the plain, on the plain. Where is that rudy plain?  In Spain, In Spain! Oh bother ... Forty Nine and eight tenths.";
            const string key    = "Q+pJALEKLqzBYWrctPUz64DXZK9zEQW5NuWOtSdPeCM=";
            var          cipher = crypto.Encrypt(key, plain);

            WriteLine(cipher);

            WriteLine($"lengthDelta = {plain.Length - cipher.Length}");

            var plain2 = crypto.Decrypt(key, cipher);

            WriteLine(plain2);

            Assert.AreEqual(plain, plain2);
        }