public void WhenGeneratingKeysAreOutOfRangeThenShouldThrowException()
        {
            int keySize = int.MaxValue;

            Assert.ThrowsException <ArgumentOutOfRangeException>(() =>
            {
                AsymmetricCryptoUtil.GenerateKeys(keySize, out string publicKey, out string publicAndPrivateKey);
            });
        }
        public void WhenDataToEncryptIsEmptyAndRSAParametersThenShouldThrowArgumentNullException()
        {
            int           keySize = 1024;
            RSAParameters publicKey;
            RSAParameters publicAndPrivateKey;

            AsymmetricCryptoUtil.GenerateKeys(keySize, out publicKey, out publicAndPrivateKey);

            Assert.ThrowsException <ArgumentNullException>(() => AsymmetricCryptoUtil.Encrypt(new byte[] { }, keySize, publicKey));
        }
        public void WhenByteArrayIsNullAndRSAParametersDrecryptThenShouldThrowArgumentNullException()
        {
            int           keySize = 1024;
            RSAParameters publicKey;
            RSAParameters publicAndPrivateKey;

            AsymmetricCryptoUtil.GenerateKeys(keySize, out publicKey, out publicAndPrivateKey);

            Assert.ThrowsException <ArgumentNullException>(() => AsymmetricCryptoUtil.Decrypt(null, keySize, publicAndPrivateKey));
        }
        public void WhenDataToEncryptIsNullThenShouldThrowArgumentNullException()
        {
            int    keySize = 1024;
            string publicKey;
            string publicAndPrivateKey;

            AsymmetricCryptoUtil.GenerateKeys(keySize, out publicKey, out publicAndPrivateKey);

            Assert.ThrowsException <ArgumentNullException>(() => AsymmetricCryptoUtil.Encrypt(null, keySize, publicKey));
        }
        public void WhenKeySizeIsGreaterThanMaxValueThenShouldThrowArgumentException()
        {
            byte[] data    = { 10, 47, 67, 10, 91, 15, 33, 25 };
            int    keySize = 4096;
            string publicKey;
            string publicAndPrivateKey;

            AsymmetricCryptoUtil.GenerateKeys(keySize, out publicKey, out publicAndPrivateKey);

            Assert.ThrowsException <ArgumentOutOfRangeException>(() => AsymmetricCryptoUtil.Encrypt(data, int.MaxValue, publicKey));
        }
        public void WhenByteArrayKeyIsOutOfRangeAndDecryptAndRSAParametersThenShouldThrowArgumentNullException()
        {
            byte[]        data    = { 10, 47, 67, 10, 91, 15, 33, 25 };
            int           keySize = 1024;
            RSAParameters publicKey;
            RSAParameters publicAndPrivateKey;

            AsymmetricCryptoUtil.GenerateKeys(keySize, out publicKey, out publicAndPrivateKey);

            Assert.ThrowsException <ArgumentOutOfRangeException>(() => AsymmetricCryptoUtil.Decrypt(data, int.MaxValue, publicAndPrivateKey));
        }
        public void WhenByteArrayIsEmptyAndRSAParametersDrecryptThenShouldThrowArgumentNullException()
        {
            byte[]        data    = { 10, 47, 67, 10, 91, 15, 33, 25 };
            int           keySize = 1024;
            RSAParameters publicKey;
            RSAParameters publicAndPrivateKey;

            AsymmetricCryptoUtil.GenerateKeys(keySize, out publicKey, out publicAndPrivateKey);

            Assert.ThrowsException <ArgumentNullException>(() => AsymmetricCryptoUtil.Decrypt(new byte[] { }, keySize, publicAndPrivateKey));
        }
        public void WhenDataSizeIsGreaterThanMaximumAllowedForKeyAndRSAParametersThenShouldThrowArgumentException()
        {
            int keySize = 384;

            byte[] data = new byte[AsymmetricCryptoUtil.GetMaxDataLength(384) * 2];

            Random random = new Random();

            random.NextBytes(data);

            AsymmetricCryptoUtil.GenerateKeys(keySize, out RSAParameters publicKey, out RSAParameters publicAndPrivateKey);

            Assert.ThrowsException <ArgumentOutOfRangeException>(() => AsymmetricCryptoUtil.Encrypt(data, keySize, publicKey));
        }
        public void WhenGeneratingKeysThenPublicAndPrivateAndRSAParametersKeyMustBeDifferent()
        {
            int           keySize = 4096;
            RSAParameters publicKey;
            RSAParameters publicAndPrivateKey;

            AsymmetricCryptoUtil.GenerateKeys(keySize, out publicKey, out publicAndPrivateKey);

            Assert.AreNotEqual(string.Empty, publicKey);
            Assert.IsNotNull(publicKey);

            Assert.AreNotEqual(string.Empty, publicAndPrivateKey);
            Assert.IsNotNull(publicAndPrivateKey);

            Assert.AreNotEqual(publicKey, publicAndPrivateKey);
        }
        public void WhenTextIsProvidedAndRSAParameterKeysThenShouldBeAbleToEncryptAndDecryptTest()
        {
            string        data    = "Test cryptographic RSA";
            int           keySize = 8192;
            RSAParameters publicKey;
            RSAParameters publicAndPrivateKey;

            AsymmetricCryptoUtil.GenerateKeys(keySize, out publicKey, out publicAndPrivateKey);

            var encryptedData = AsymmetricCryptoUtil.EncryptText(data, keySize, publicKey);

            var decryptedData = AsymmetricCryptoUtil.DecryptText(encryptedData, keySize, publicAndPrivateKey);

            Assert.AreNotEqual(data, encryptedData);
            Assert.AreEqual(data, decryptedData);
        }
        public void WhenByteArrayIsProvidedAndRSAParameterKeysThenShouldBeAbleToEncryptAndDecryptTest()
        {
            byte[]        data    = { 10, 47, 67, 10, 91, 15, 33, 25 };
            int           keySize = 1024;
            RSAParameters publicKey;
            RSAParameters publicAndPrivateKey;

            AsymmetricCryptoUtil.GenerateKeys(keySize, out publicKey, out publicAndPrivateKey);

            var encryptedData = AsymmetricCryptoUtil.Encrypt(data, keySize, publicKey);

            var decryptedData = AsymmetricCryptoUtil.Decrypt(encryptedData, keySize, publicAndPrivateKey);

            CollectionAssert.AreNotEqual(data, encryptedData);
            CollectionAssert.AreEqual(data, decryptedData);
        }
        public void WhenTextIsProvidedThenShouldBeAbleToEncryptAndDecryptTest()
        {
            string data    = "Essa é uma string de teste para testar a criptografia assimétrica utilizando RSA.";
            int    keySize = 4096;
            string publicKey;
            string publicAndPrivateKey;

            AsymmetricCryptoUtil.GenerateKeys(keySize, out publicKey, out publicAndPrivateKey);

            var encryptedData = AsymmetricCryptoUtil.EncryptText(data, keySize, publicKey);

            var decryptedData = AsymmetricCryptoUtil.DecryptText(encryptedData, keySize, publicAndPrivateKey);

            Assert.AreNotEqual(data, encryptedData);
            Assert.AreEqual(data, decryptedData);
        }