示例#1
0
        public void InvalidPaddingThrows(byte[] error, int blockSize)
        {
            var blkCipher = new NoOpTransform(blockSize);
            var dec       = new PaddingTransform(blkCipher, PaddingMode.PKCS7, true);

            Assert.True(dec.CanReuseTransform);
            Assert.True(dec.CanTransformMultipleBlocks);
            Assert.Throws <CryptographicException>(() => dec.TransformFinalBlock(error, 0, error.Length));
        }
示例#2
0
        public void Pkcs7PaddingVector(byte[] input, int blockSize, byte[] reference)
        {
            var blkCipher = new NoOpSingleBlockTransform(blockSize);
            var enc       = new PaddingTransform(blkCipher, PaddingMode.PKCS7, false);
            var dec       = new PaddingTransform(blkCipher, PaddingMode.PKCS7, true);
            var padded    = enc.TransformFinalBlock(input, 0, input.Length);

            Assert.Equal(reference.AsEnumerable(), padded);

            using (var unPadded = new MemoryStream()) {
                using (var cryptoStream = new CryptoStream(unPadded, dec, CryptoStreamMode.Write)) {
                    for (var i = 0; i < padded.Length; i += blockSize)
                    {
                        cryptoStream.Write(padded, i, blockSize);
                    }
                }

                Assert.Equal(input.AsEnumerable(), unPadded.ToArray());
            }

            Assert.True(enc.CanReuseTransform);
            Assert.False(enc.CanTransformMultipleBlocks);
        }