Exemplo n.º 1
0
        public void EncryptAndDecryptPaddingZeros()
        {
            Action <byte[]> check = plainText =>
            {
                byte[] cipherText, newPlainText;

                using (var algorithm = new SimpleSymmetricAlgorithm {
                    Mode = CipherMode.ECB, Padding = PaddingMode.Zeros
                })
                {
                    algorithm.GenerateKey();
                    algorithm.GenerateIV();

                    InternalEncryptAndDecrypt(algorithm, plainText, out cipherText, out newPlainText);
                }

                for (int i = 0; i < plainText.Length; i++)
                {
                    Assert.Equal(plainText[i], newPlainText[i]);
                }

                for (int i = plainText.Length; i < newPlainText.Length; i++)
                {
                    Assert.Equal(0, newPlainText[i]);
                }
            };

            foreach (var plainText in BlockSizeMultiplePlainTexts.Union(BlockSizeNonMultiplePlainTexts))
            {
                check(plainText);
            }
        }
Exemplo n.º 2
0
        public void EncryptAndDecryptPaddingNone()
        {
            Action <byte[]> check = plainText =>
            {
                byte[] cipherText, newPlainText;

                using (var algorithm = new SimpleSymmetricAlgorithm {
                    Mode = CipherMode.ECB, Padding = PaddingMode.None
                })
                {
                    algorithm.GenerateKey();
                    algorithm.GenerateIV();

                    InternalEncryptAndDecrypt(algorithm, plainText, out cipherText, out newPlainText);
                }

                Assert.Equal(plainText, newPlainText);
            };

            foreach (var plainText in BlockSizeMultiplePlainTexts)
            {
                check(plainText);
            }

            foreach (var plainText in BlockSizeNonMultiplePlainTexts)
            {
                Assert.Throws <CryptographicException>(() => check(plainText));
            }
        }
Exemplo n.º 3
0
        public void EncryptAndDecryptPaddingPKCS7()
        {
            Action <byte[]> check = plainText =>
            {
                byte[] cipherText, newPlainText, newPlainTextNoDepad;

                using (var algorithm = new SimpleSymmetricAlgorithm {
                    Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7
                })
                {
                    algorithm.GenerateKey();
                    algorithm.GenerateIV();

                    InternalEncryptAndDecrypt(algorithm, plainText, out cipherText, out newPlainText);

                    algorithm.Padding = PaddingMode.None;

                    newPlainTextNoDepad = InternalTransform(algorithm.CreateDecryptor, cipherText);
                }

                int padCount = newPlainTextNoDepad.Length - newPlainText.Length;

                byte[] padding = new byte[padCount];
                BlockCopy(newPlainTextNoDepad, newPlainText.Length, padding, 0, padCount);

                Assert.Equal(plainText, newPlainText);

                for (int i = 0; i < plainText.Length; i++)
                {
                    Assert.Equal(plainText[i], newPlainTextNoDepad[i]);
                }

                if (padCount > 0)
                {
                    Assert.Equal(padCount, padding[padCount - 1]);

                    for (int i = 0; i < padCount - 1; i++)
                    {
                        Assert.Equal(padCount, padding[i]);
                    }
                }
            };

            foreach (var plainText in BlockSizeMultiplePlainTexts.Union(BlockSizeNonMultiplePlainTexts))
            {
                check(plainText);
            }
        }