Esempio n. 1
0
        public void SymmetricEncryption_Test_DifferentText_Pass()
        {
            using (var encryption = new SymmetricEncryption())
            {
                var          encryptionKey        = encryption.GenerateRandomNumber(32);
                var          initializationVector = encryption.GenerateRandomNumber(16);
                const string original             = "Actual text to encrypt";
                const string expected             = "Text to compare";

                var    encrypted = encryption.Encrypt(Encoding.UTF8.GetBytes(original), encryptionKey, initializationVector);
                var    decrypted = encryption.Decrypt(encrypted, encryptionKey, initializationVector);
                string actual    = Encoding.UTF8.GetString(decrypted);

                Assert.AreNotEqual(expected, actual);
                Assert.AreEqual(original, actual);
            }
        }
Esempio n. 2
0
        public void SymmetricEncryption_Test__DifferentIV_Pass()
        {
            using (var encryption = new SymmetricEncryption())
            {
                var encryptionKey        = encryption.GenerateRandomNumber(32);
                var initializationVector = encryption.GenerateRandomNumber(16);
                var otherIV = encryption.GenerateRandomNumber(16);

                const string expected = "Text to encrypt";

                var    encrypted = encryption.Encrypt(Encoding.UTF8.GetBytes(expected), encryptionKey, initializationVector);
                var    decrypted = encryption.Decrypt(encrypted, encryptionKey, otherIV);
                string actual    = Encoding.UTF8.GetString(decrypted);


                var ex = Assert.Catch <System.Security.Cryptography.CryptographicException>(() => encryption.Decrypt(encrypted, encryptionKey, otherIV));
                Assert.AreEqual(ex.Message, "System.Security.Cryptography.CryptographicException : Padding is invalid and cannot be removed.");
            }
        }
        public void EncryptedData_WithDifferentKey_IsNotSuccessfullyDecrypted()
        {
            // Arrange
            var message = "my super secret message";
            var key1    = SymmetricEncryption.GenerateKey(32);
            var key2    = SymmetricEncryption.GenerateKey(32);
            var iv      = SymmetricEncryption.GenerateRandomNumber(16);

            // Act
            var encryptedData = SymmetricEncryption.Encrypt(message, key1, iv);
            var decryptedData = SymmetricEncryption.DecryptValue(encryptedData.EncryptedData, key2, iv);

            // Assert
            Assert.NotEqual(message, decryptedData);
        }
        public void EncryptedData_WithIv_IsSuccessfullyDecrypted()
        {
            // Arrange
            var message = "my super secret message";
            var key     = SymmetricEncryption.GenerateKey(32);
            var iv      = SymmetricEncryption.GenerateRandomNumber(16);

            // Act
            var encryptedData = SymmetricEncryption.Encrypt(message, key, iv);
            var decryptedData = SymmetricEncryption.DecryptValue(encryptedData.EncryptedData, key, iv);

            // Assert
            Assert.Equal(message, decryptedData);
            Assert.Equal("AES", encryptedData.Method);
            Assert.NotEqual(encryptedData.EncryptedData, System.Text.Encoding.UTF8.GetBytes(message));
        }