コード例 #1
0
        public void WhenGeneratingKeysAreOutOfRangeThenShouldThrowException()
        {
            int keySize = int.MaxValue;

            Assert.ThrowsException <ArgumentOutOfRangeException>(() =>
            {
                AsymmetricCryptoUtil.GenerateKeys(keySize, out string publicKey, out string publicAndPrivateKey);
            });
        }
コード例 #2
0
        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));
        }
コード例 #3
0
        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));
        }
コード例 #4
0
        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));
        }
コード例 #5
0
        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));
        }
コード例 #6
0
        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));
        }
コード例 #7
0
        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));
        }
コード例 #8
0
        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));
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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);
        }
コード例 #11
0
        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);
        }
コード例 #12
0
        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);
        }