Example #1
0
        public void TestEncryptDecrypt(int keySize, Func <int, int, int> getLengthFunc, int blocks, bool preserveLength = true)
        {
            RSACryptographyProvider provider = RSACryptographyProvider.Create(keySize);

            Assert.IsTrue(provider.CanEncrypt);
            Trace.WriteLine($"Input Block Size:{provider.InputBlockSize}; OutputBlockSize:{provider.OutputBlockSize}");

            // Generate random input.
            int length = getLengthFunc(provider.InputBlockSize, provider.OutputBlockSize);

            byte[] input = new byte[length];
            if (length > 0)
            {
                Tester.RandomGenerator.NextBytes(input);
            }

            byte[] encryptedBytes = provider.Encrypt(input, preserveLength);
            Assert.IsNotNull(encryptedBytes);

            // Check we got the correct number of blocks.
            Assert.AreEqual(0, encryptedBytes.LongLength % provider.OutputBlockSize);
            Assert.AreEqual(blocks, encryptedBytes.LongLength / provider.OutputBlockSize);

            Assert.IsTrue(provider.CanDecrypt);
            byte[] output = provider.Decrypt(encryptedBytes, preserveLength);
            Assert.IsNotNull(output);
            Assert.AreEqual(length, output.LongLength);
            CollectionAssert.AreEqual(input, output);
        }
Example #2
0
        public void Test_Null_1024()
        {
            RSACryptographyProvider provider = RSACryptographyProvider.Create(1024);

            Assert.IsTrue(provider.CanEncrypt);
            Assert.IsNull(provider.Encrypt((byte[])null));
            Assert.IsTrue(provider.CanDecrypt);
            Assert.IsNull(provider.Decrypt((byte[])null));
        }