Example #1
0
        public void SymmetricEncryptor_HeavyUsage()
        {
            CryptoRandomizer random         = new CryptoRandomizer();
            const int        Iterations     = 100;
            const int        MaxMemoryBlock = 100000;


            for (int i = 0; i < Iterations; i++)
            {
                int    blockSize = random.Next(MaxMemoryBlock);
                byte[] buffer    = new ByteGenerator().GenerateBytes(blockSize);
                string key       = CryptoString.GenerateRandomText(1000);

                byte[] decryptedBuffer;
                byte[] encryptedBuffer;

                using (SymmetricEncryptor encryptor = new SymmetricEncryptor())
                {
                    //Encrypt
                    encryptedBuffer = encryptor.EncryptBytes(buffer, key);

                    // Decrypt
                    decryptedBuffer = encryptor.DecryptBytes(encryptedBuffer, key);
                }                 // IDispose - Closes and clears the keys in memory

                // Assert - Check to make sure the bytes are all the same
                Assert.IsTrue(buffer.SequenceEqual(decryptedBuffer));
            }
        }
        public void CryptoString_TestUtilityMethods()
        {
            string password = "******";
            int    length   = 100;

            string random = CryptoString.GenerateRandomText(length);

            Assert.IsTrue(random.Length > (length / 2));

            string byteString = CryptoString.BytesToString(new ByteGenerator().GenerateBytes(length));

            Assert.IsTrue(byteString.Length > 0);


            // String conversions
            SecureString secureString   = CryptoString.StringToSecureString(password);
            string       unsecureString = CryptoString.SecureStringToString(secureString);

            Assert.IsTrue(password.Equals(unsecureString));
        }
        public void CryptoString_BasicUsuage()
        {
            string       password     = "******";
            SecureString secureString = CryptoString.StringToSecureString(password);             // Only used for test
            string       decryptedString;

            CryptoString crypto = new CryptoString(secureString);

            // Make sure the ToString DOES NOT return the string
            decryptedString = crypto.ToString();
            Assert.IsFalse(decryptedString.Equals(password));

            // Use this only if SecureString is not accepted
            string useString = CryptoString.SecureStringToString(crypto.GetSecureString());

            ////////////////////////////////////////////
            // You can now use the useString variable
            ////////////////////////////////////////////
            // When you done fill the variable full of random Text
            // NOTE: Not totally secure, but an added level of obsfucation
            useString = CryptoString.GenerateRandomText(10000);
        }