Beispiel #1
0
        public void ShouldEncryptDataWithoutException()
        {
            MyRSA myRsa = new MyRSA(512);

            byte[] toEncrypt = { 1, 2, 3 };
            Assert.DoesNotThrow(() => myRsa.Encrypt(toEncrypt));
        }
Beispiel #2
0
    public void AdditionalTests(Byte[] data, RSAEncryptionPadding padding)
    {
        var customAsymProvider = new MyRSA();

        // Should raise on derived asymmetric classes
        customAsymProvider.Encrypt(data, padding);      // Noncompliant
        customAsymProvider.EncryptValue(data);          // Noncompliant
        customAsymProvider.Decrypt(data, padding);      // Noncompliant
        customAsymProvider.DecryptValue(data);          // Noncompliant

        // Should raise on the Try* methods added in NET Core 2.1
        // Note: this test is cheating - we can't currently referencing the
        // real 2.1 assemblies since the test project is targetting an older
        // NET Framework, so we're testing against a custom subclass
        // to which we've added the new method names.
        customAsymProvider.TryEncrypt();            // Noncompliant
        customAsymProvider.TryEncrypt(null);        // Noncompliant
        customAsymProvider.TryDecrypt();            // Noncompliant
        customAsymProvider.TryDecrypt(null);        // Noncompliant

        customAsymProvider.OtherMethod();

        // Should raise on derived symmetric classes
        var customSymProvider = new MySymmetricCrypto();

        customSymProvider.CreateEncryptor();        // Noncompliant
        customSymProvider.CreateDecryptor();        // Noncompliant
    }
Beispiel #3
0
        public void ShouldEncryptAndDecryptEmptyArray()
        {
            MyRSA myRsa = new MyRSA(512);

            byte[] toEncrypt = { };
            byte[] encrypted = myRsa.Encrypt(toEncrypt);
            Assert.AreEqual(0, encrypted.Length);
            byte[] decrypted = myRsa.Decrypt(encrypted);
            Assert.AreEqual(toEncrypt, decrypted);
        }
Beispiel #4
0
        public void ShouldEncryptAndDecryptMultipleZerosToOneZero()
        {
            MyRSA myRsa = new MyRSA(512);

            byte[] toEncrypt = { 0, 0, 0 };
            byte[] encrypted = myRsa.Encrypt(toEncrypt);
            Assert.AreEqual(64, encrypted.Length);
            byte[] decrypted = myRsa.Decrypt(encrypted);
            Assert.AreEqual(1, decrypted.Length);
            Assert.AreEqual(new byte[] { 0 }, decrypted);
        }
Beispiel #5
0
        public void ShouldEncryptAndDecrypt1()
        {
            MyRSA myRsa = new MyRSA(512);

            byte[] toEncrypt = { 1 };
            byte[] encrypted = myRsa.Encrypt(toEncrypt);
            Assert.AreEqual(64, encrypted.Length);
            Assert.AreEqual(1, encrypted[0]);
            byte[] decrypted = myRsa.Decrypt(encrypted);
            Assert.AreEqual(toEncrypt, decrypted);
        }
Beispiel #6
0
        public void ShouldEncryptAndDecrypt100SetsOfRandomData()
        {
            int    keyLength = 1024; // 1024 bit key for max data length of 64 bytes
            MyRSA  myRsa     = new MyRSA(keyLength);
            Random random    = new Random();

            for (int i = 0; i < 100; i++)
            {
                byte[] toEncrypt = new byte[random.Next(1, keyLength / (8 * 2))];
                random.NextBytes(toEncrypt);
                toEncrypt[toEncrypt.Length - 1] |= (byte)random.Next(1, 255);  // Prevent leading zeros
                byte[] encrypted = myRsa.Encrypt(toEncrypt);
                Assert.AreEqual(keyLength / 8, encrypted.Length);
                byte[] decrypted = myRsa.Decrypt(encrypted);
                Assert.AreEqual(toEncrypt, decrypted);
            }
        }