static IBlockCipher Setup(IBlockCipher algo, string mode, bool encryption, byte[] key, byte[] iv) { if (mode == "ECB") { algo.Init(encryption, new KeyParameter(key)); return(algo); } IBlockCipher cipher = null; if (mode == "CBC") { cipher = new Org.BouncyCastle.Crypto.Modes.CbcBlockCipher(algo); } if (mode == "CFB") { cipher = new Org.BouncyCastle.Crypto.Modes.CfbBlockCipher(algo, iv.Length << 3); } if (mode == "OFB") { cipher = new Org.BouncyCastle.Crypto.Modes.OfbBlockCipher(algo, iv.Length); } if (cipher != null) { cipher.Init(encryption, new ParametersWithIV(new KeyParameter(key), iv)); return(cipher); } throw new ArgumentException(); }
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); }
static IBlockCipher Setup (IBlockCipher algo, string mode, bool encryption, byte[] key, byte[] iv) { if (mode == "ECB") { algo.Init (encryption, new KeyParameter (key)); return algo; } IBlockCipher cipher = null; if (mode == "CBC") cipher = new Org.BouncyCastle.Crypto.Modes.CbcBlockCipher (algo); if (mode == "CFB") cipher = new Org.BouncyCastle.Crypto.Modes.CfbBlockCipher (algo, iv.Length << 3); if (mode == "OFB") cipher = new Org.BouncyCastle.Crypto.Modes.OfbBlockCipher (algo, iv.Length); if (cipher != null) { cipher.Init (encryption, new ParametersWithIV (new KeyParameter (key), iv)); return cipher; } throw new ArgumentException (); }