示例#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
        private ICryptoTransform CreateXfrm(byte[] rgbKey, byte[] rgbIV, bool decrypt)
        {
            ICryptoTransform xfrm = new SM4Transform(rgbKey, decrypt);

            switch (Mode)
            {
            case CipherMode.ECB:
                break;

            case CipherMode.CBC:
                xfrm = new CbcTransform(xfrm, rgbIV, decrypt);
                break;

            default:
                throw new NotSupportedException("Only CBC/ECB is supported");
            }

            switch (PaddingValue)
            {
            case PaddingMode.None:
                break;

            case PaddingMode.PKCS7:
#pragma warning disable 618
            case PaddingTransform.Iso10126:
            case PaddingTransform.AnsiX923:
#pragma warning restore 618
                xfrm = new PaddingTransform(xfrm, PaddingValue, decrypt);
                break;

            case PaddingMode.Zeros:
                xfrm = new ZerosPaddingTransform(xfrm, decrypt);
                break;

            default:
                throw new NotSupportedException("Only PKCS#7 padding is supported");
            }

            return(xfrm);
        }
示例#3
0
        public void TestCbc(byte[] k, byte[] iv, byte[] d)
        {
            var aes = Aes.Create();

            aes.Mode    = CipherMode.CBC;
            aes.Padding = PaddingMode.PKCS7;
            var reference = TransformData(aes.CreateEncryptor(k, iv), d);

            var ecb = Aes.Create();

            ecb.Padding = PaddingMode.None;
            ecb.Mode    = CipherMode.ECB;
            var encChain = new PaddingTransform(new CbcTransform(ecb.CreateEncryptor(k, iv), iv, false), PaddingMode.PKCS7, false);
            var actual   = TransformData(encChain, d);

            Assert.Equal(reference, actual);

            var decChain  = new PaddingTransform(new CbcTransform(ecb.CreateDecryptor(k, iv), iv, true), PaddingMode.PKCS7, true);
            var decrypted = TransformData(decChain, actual);

            Assert.Equal(d, decrypted);
        }
示例#4
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);
        }