public byte[] ofbDecoding(byte[] key, byte[] message, byte[] iv)
        {
            Crypto.Engines.AesEngine    engine                       = new Crypto.Engines.AesEngine();
            Crypto.Modes.OfbBlockCipher blockCipher                  = new Crypto.Modes.OfbBlockCipher(engine, 128);
            Crypto.Paddings.PaddedBufferedBlockCipher cipher         = new Crypto.Paddings.PaddedBufferedBlockCipher(blockCipher);
            Crypto.Parameters.KeyParameter            keyParam       = new Crypto.Parameters.KeyParameter(key);
            Crypto.Parameters.ParametersWithIV        keyParamWithIV = new Crypto.Parameters.ParametersWithIV(keyParam, iv);

            cipher.Init(false, keyParamWithIV);
            byte[] comparisonBytes = new byte[cipher.GetOutputSize(message.Length)];
            int    length          = cipher.ProcessBytes(message, comparisonBytes, 0);

            cipher.DoFinal(comparisonBytes, length);

            return(comparisonBytes);
        }
Exemplo n.º 2
0
        private void DoCbc(byte[] key, byte[] iv, byte[] pt, byte[] expected)
        {
            Org.BouncyCastle.Crypto.Paddings.PaddedBufferedBlockCipher c =
                new Org.BouncyCastle.Crypto.Paddings.PaddedBufferedBlockCipher(
                    new Org.BouncyCastle.Crypto.Modes.CbcBlockCipher(
                        new Org.BouncyCastle.Crypto.Engines.SerpentEngine())
                    , new Org.BouncyCastle.Crypto.Paddings.Pkcs7Padding());

            byte[] ct = new byte[expected.Length];

            c.Init(true, new Org.BouncyCastle.Crypto.Parameters.ParametersWithIV(
                       new Org.BouncyCastle.Crypto.Parameters.KeyParameter(key), iv));

            int l = c.ProcessBytes(pt, 0, pt.Length, ct, 0);

            c.DoFinal(ct, l);

            if (!Org.BouncyCastle.Utilities.Arrays.AreEqual(expected, ct))
            {
                System.Console.WriteLine("CBC test failed");
            }
        }