Example #1
0
        public void GenerateALotOfNewKeys_WriteToFile()
        {
            // Arrange
            // Change the path as needed.
            using (FileStream file = File.OpenWrite(@"s:\RandomKeys.txt"))
            {
                using (StreamWriter writer = new StreamWriter(file))
                {
                    // Act
                    for (int i = 1; i < 2000; i++)
                    {
                        byte[] newKey = AESThenHMACEncryptionHelper.NewKey();

                        string key = Convert.ToBase64String(newKey);
                        writer.WriteLine(key);

                        Trace.WriteLine(key);

                        // Assert
                        Assert.IsNotNull(key);
                    }
                    file.Flush();
                }
            }
        }
Example #2
0
        [TestCase("Password", "18cKBWuDPW5jVXn580mAxEAouNkRbMXq392WfQ/QFNk=", "RbWZAwKpPx6zFLLuyubeR61i+QpLULw2kvHIGYrUYR4=")] // Used below. ensure it's normally valid
        public void EncryptThenDecrypt_DecryptsSuccessfully(string toEncrypt, string encryptionKey, string authenticationKey)
        {
            // Arrange
            var encryptionKeyBytes     = Convert.FromBase64String(encryptionKey);
            var authenticationKeyBytes = Convert.FromBase64String(authenticationKey);

            // Act
            string encrypted = AESThenHMACEncryptionHelper.SimpleEncrypt(toEncrypt, encryptionKeyBytes, authenticationKeyBytes);
            string decrypted = AESThenHMACEncryptionHelper.SimpleDecrypt(encrypted, encryptionKeyBytes, authenticationKeyBytes);

            // Assert
            Assert.AreEqual(toEncrypt, decrypted);
        }
Example #3
0
        public void GenerateNewKeys()
        {
            // Arrange

            // Act
            byte[] newKey = AESThenHMACEncryptionHelper.NewKey();

            string key = Convert.ToBase64String(newKey);

            Trace.WriteLine(key);

            // Assert
            Assert.IsNotNull(key);
        }
Example #4
0
        [TestCase("PmUtd9koxdSmhtJyy3pQqQEe8743cs7zDRDyGRt7i6k=", "PE6K9jw/KQMgHxpzUJpKuzlrIQxUZjzvCnVe2wMEheo=")] // Authentication key invalid
        public void EncryptThenDecrypt_WhereEncryptionKeyIsInvalid_DoesNotDecrypt(string encryptionKey, string authenticationKey)
        {
            // Arrange
            string toEncrypt              = "Password";
            var    encryptionKeyBytes     = Convert.FromBase64String(encryptionKey);
            var    authenticationKeyBytes = Convert.FromBase64String(authenticationKey);
            string encrypted              = AESThenHMACEncryptionHelper.SimpleEncrypt(toEncrypt, encryptionKeyBytes, authenticationKeyBytes);

            var deencryptionKey    = Convert.FromBase64String("8oI/pQNdEx6aWNATrKVsUH0gU3wW3j05furiqLFax5k=");
            var deauthentiationKey = Convert.FromBase64String("PE6K9jw/KQMgHxpzUJpKuzlrIQxUZjzvCnVe2wMEheo=");

            // Act
            // Assert
            Assert.Throws <CryptographicException>(() => AESThenHMACEncryptionHelper.SimpleDecrypt(encrypted, deencryptionKey, deauthentiationKey));
        }