Org.BouncyCastle.Crypto.Paddings.Pkcs7Padding.PadCount C# (CSharp) Method

PadCount() public method

public PadCount ( byte input ) : int
input byte
return int
        public int PadCount(
			byte[] input)
        {
            int count = (int) input[input.Length - 1];

			if (count < 1 || count > input.Length)
            {
                throw new InvalidCipherTextException("pad block corrupted");
            }

			for (int i = 1; i <= count; i++)
            {
                if (input[input.Length - i] != count)
                {
                    throw new InvalidCipherTextException("pad block corrupted");
                }
            }

            return count;
        }
    }

Usage Example

Ejemplo n.º 1
0
        public override void PerformTest()
        {
            SecureRandom    rand = new SecureRandom(new byte[20]);

            rand.SetSeed(DateTime.Now.Ticks);

            doTestPadding(new Pkcs7Padding(), rand,
                Hex.Decode("ffffff0505050505"),
                Hex.Decode("0000000004040404"));

            Pkcs7Padding padder = new Pkcs7Padding();
            try
            {
                padder.PadCount(new byte[8]);

                Fail("invalid padding not detected");
            }
            catch (InvalidCipherTextException e)
            {
                if (!"pad block corrupted".Equals(e.Message))
                {
                    Fail("wrong exception for corrupt padding: " + e);
                }
            }

            doTestPadding(new ISO10126d2Padding(), rand,
                null,
                null);

            doTestPadding(new X923Padding(), rand,
                null,
                null);

            doTestPadding(new TbcPadding(), rand,
                Hex.Decode("ffffff0000000000"),
                Hex.Decode("00000000ffffffff"));

            doTestPadding(new ZeroBytePadding(), rand,
                Hex.Decode("ffffff0000000000"),
                null);

            doTestPadding(new ISO7816d4Padding(), rand,
                Hex.Decode("ffffff8000000000"),
                Hex.Decode("0000000080000000"));
        }