Exemplo n.º 1
0
 private void onPaddingSelectChanged(object sender, EventArgs e)
 {
     if (paddingSelect.SelectedIndex == 0)
     {
         encryptPadding = BlockPadding.PKCS7Padding;
     }
     else if (paddingSelect.SelectedIndex == 1)
     {
         encryptPadding = BlockPadding.ZeroBytePadding;
     }
     else if (paddingSelect.SelectedIndex == 2)
     {
         encryptPadding = BlockPadding.NoPadding;
     }
 }
        public void PKCS7PaddingLengthValidBlockPass()
        {
            for (byte i = 1; i < 16; i++)
            {
                const int blockSize = 16;
                Assert.AreEqual(i, BlockPadding.GetPKCS7PaddingLength(blockSize, CreateBlock(blockSize, i)));
            }

            for (byte i = 1; i < 16; i++)
            {
                const int blockSize = 16;
                Assert.AreEqual(i, BlockPadding.GetPKCS7PaddingLength(blockSize, CreateBlock(blockSize * 2, i)));
            }

            for (byte i = 1; i < byte.MaxValue; i++)
            {
                const int blockSize = 255;
                Assert.AreEqual(i, BlockPadding.GetPKCS7PaddingLength(blockSize, CreateBlock(blockSize, i)));
            }
        }
        public void PKCS7PaddingInvalidBlockFail()
        {
            const int blockSize16 = 16;

            for (byte i = 1; i < blockSize16; i++)
            {
                byte[] b = CreateBlock(blockSize16, i);
                b[blockSize16 - i] = (byte)(i + 17);

                Assert.AreEqual <int>(-1, BlockPadding.GetPKCS7PaddingLength(blockSize16, b));
            }

            const int blockSize255 = byte.MaxValue;

            for (byte i = 1; i < blockSize255; i++)
            {
                byte[] b = CreateBlock(blockSize255, i);
                b[blockSize255 - i] = (byte)(i - 1);

                Assert.AreEqual <int>(-1, BlockPadding.GetPKCS7PaddingLength(blockSize255, b));
            }
        }
        public void PKCS7PKCS7PaddingLengthBadArgumentFail()
        {
            ArgumentException ex;

            ex = Assert.ThrowsException <ArgumentOutOfRangeException>(() => BlockPadding.GetPKCS7PaddingLength(4, null));
            Assert.AreEqual <string>("buffer", ex.ParamName);

            ex = Assert.ThrowsException <ArgumentOutOfRangeException>(() => BlockPadding.GetPKCS7PaddingLength(4, default));
            Assert.AreEqual <string>("buffer", ex.ParamName);

            ex = Assert.ThrowsException <ArgumentOutOfRangeException>(() => BlockPadding.GetPKCS7PaddingLength(4, Span <byte> .Empty));
            Assert.AreEqual <string>("buffer", ex.ParamName);


            ex = Assert.ThrowsException <ArgumentOutOfRangeException>(() => BlockPadding.GetPKCS7PaddingLength(-1, new byte[16]));
            Assert.AreEqual <string>("blockSize", ex.ParamName);

            ex = Assert.ThrowsException <ArgumentOutOfRangeException>(() => BlockPadding.GetPKCS7PaddingLength(0, new byte[16]));
            Assert.AreEqual <string>("blockSize", ex.ParamName);

            ex = Assert.ThrowsException <ArgumentOutOfRangeException>(() => BlockPadding.GetPKCS7PaddingLength(byte.MaxValue + 1, new byte[16]));
            Assert.AreEqual <string>("blockSize", ex.ParamName);


            ex = Assert.ThrowsException <ArgumentOutOfRangeException>(() => BlockPadding.GetPKCS7PaddingLength(15, new byte[16]));
            Assert.AreEqual <string>("buffer", ex.ParamName);
            Assert.AreEqual <string>("Length is not a multiple of block size. (Parameter 'buffer')", ex.Message);

            ex = Assert.ThrowsException <ArgumentOutOfRangeException>(() => BlockPadding.GetPKCS7PaddingLength(17, new byte[32]));
            Assert.AreEqual <string>("buffer", ex.ParamName);
            Assert.AreEqual <string>("Length is not a multiple of block size. (Parameter 'buffer')", ex.Message);

            ex = Assert.ThrowsException <ArgumentOutOfRangeException>(() => BlockPadding.GetPKCS7PaddingLength(16, new byte[15]));
            Assert.AreEqual <string>("buffer", ex.ParamName);
            Assert.AreEqual <string>("Length is not a multiple of block size. (Parameter 'buffer')", ex.Message);
        }
Exemplo n.º 5
0
        //private
        private static byte[] encryptDecrypt(byte[] input, byte[] key, byte[] iv, BlockMode mode, BlockPadding padding, bool encrypt)
        {
            DesEdeEngine        engine = new DesEdeEngine();
            IBlockCipher        cipherMode;
            BufferedBlockCipher cipher;
            KeyParameter        keyP      = new KeyParameter(key);
            ParametersWithIV    keyParams = new ParametersWithIV(keyP, iv);

            if (mode == BlockMode.CBC)
            {
                cipherMode = new CbcBlockCipher(engine);
            }
            else if (mode == BlockMode.CFB)
            {
                cipherMode = new CfbBlockCipher(engine, iv.Length);
            }
            else if (mode == BlockMode.OFB)
            {
                cipherMode = new OfbBlockCipher(engine, iv.Length);
            }
            else
            {
                throw new Exception("mode must be a valid BlockMode.");
            }

            if (padding == BlockPadding.PKCS7Padding)
            {
                cipher = new PaddedBufferedBlockCipher(cipherMode, new Pkcs7Padding());
            }
            else if (padding == BlockPadding.ZeroBytePadding)
            {
                cipher = new PaddedBufferedBlockCipher(cipherMode, new ZeroBytePadding());
            }
            else if (padding == BlockPadding.NoPadding)
            {
                cipher = new BufferedBlockCipher(cipherMode);
            }
            else
            {
                throw new Exception("padding must be a valid BlockPadding.");
            }

            cipher.Init(encrypt, keyParams);
            byte[] output = new byte[cipher.GetOutputSize(input.Length)];
            int    length = cipher.ProcessBytes(input, output, 0);

            cipher.DoFinal(output, length);

            return(output);
        }
Exemplo n.º 6
0
 public static byte[] decrypt(byte[] input, byte[] key, byte[] iv, BlockMode mode, BlockPadding padding)
 {
     return(encryptDecrypt(input, key, iv, mode, padding, false));
 }