public static byte[] AESGCMEncrypt(byte[] data, byte[] key, byte[] iv)
        {
            var keyParamWithIV = new Org.BouncyCastle.Crypto.Parameters.ParametersWithIV(new Org.BouncyCastle.Crypto.Parameters.KeyParameter(key), iv, 0, 16);

            var cipher = Org.BouncyCastle.Security.CipherUtilities.GetCipher("AES/GCM/NoPadding");

            cipher.Init(true, keyParamWithIV);

            return(cipher.DoFinal(data));
        }
        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);
        }
        public static byte[] AESGCMEncrypt(byte[] data, byte[] key)
        {
            byte[] iv             = AESGenerateIV(16);
            var    keyParamWithIV = new Org.BouncyCastle.Crypto.Parameters.ParametersWithIV(new Org.BouncyCastle.Crypto.Parameters.KeyParameter(key), iv, 0, 16);

            var cipher = Org.BouncyCastle.Security.CipherUtilities.GetCipher("AES/GCM/NoPadding");

            cipher.Init(true, keyParamWithIV);

            var encryptedData = cipher.DoFinal(data);

            using (var stream = new System.IO.MemoryStream())
            {
                using (var writer = new System.IO.BinaryWriter(stream))
                {
                    writer.Write(iv);
                    writer.Write(encryptedData);
                }

                return(stream.ToArray());
            }
        }
        public static byte[] AESGCMDecrypt(byte[] data, byte[] key)
        {
            byte[] iv;
            byte[] encryptedData;

            using (var stream = new System.IO.MemoryStream(data))
            {
                using (var reader = new System.IO.BinaryReader(stream))
                {
                    iv            = reader.ReadBytes(16);
                    encryptedData = reader.ReadBytes(data.Length - 16);
                }
            }

            var keyParamWithIV = new Org.BouncyCastle.Crypto.Parameters.ParametersWithIV(new Org.BouncyCastle.Crypto.Parameters.KeyParameter(key), iv, 0, 16);

            var cipher = Org.BouncyCastle.Security.CipherUtilities.GetCipher("AES/GCM/NoPadding");

            cipher.Init(false, keyParamWithIV);

            return(cipher.DoFinal(encryptedData));
        }
Beispiel #5
0
        } // End Function Sha3

        // https://www.programcreek.com/java-api-examples/index.php?api=org.bouncycastle.crypto.engines.SerpentEngine
        // https://stackoverflow.com/questions/9806012/multiple-encryption-with-bouncycastle
        public static void Poly1305Tests()
        {
            // https://www.programcreek.com/java-api-examples/index.php?api=org.bouncycastle.crypto.engines.TwofishEngine
            var poly = new Org.BouncyCastle.Crypto.Macs.Poly1305(new Org.BouncyCastle.Crypto.Engines.SerpentEngine());

            poly.Init(null);


            var se   = new Org.BouncyCastle.Crypto.Engines.Salsa20Engine();
            var pwiv = new Org.BouncyCastle.Crypto.Parameters.ParametersWithIV(
                new Org.BouncyCastle.Crypto.Parameters.KeyParameter(new byte[16])
                , new byte[8]
                );


            // https://en.wikipedia.org/wiki/Salsa20
            var xse   = new Org.BouncyCastle.Crypto.Engines.XSalsa20Engine();
            var xsekp = new Org.BouncyCastle.Crypto.Parameters.ParametersWithIV(
                new Org.BouncyCastle.Crypto.Parameters.KeyParameter(new byte[32])
                , new byte[24]
                );


            var engine = new Org.BouncyCastle.Crypto.Engines.RC4Engine();

            byte[] key = System.Text.Encoding.UTF8.GetBytes("foobar");
            engine.Init(true, new Org.BouncyCastle.Crypto.Parameters.KeyParameter(key));


            Org.BouncyCastle.Crypto.Engines.TwofishEngine twofish = new Org.BouncyCastle.Crypto.Engines.TwofishEngine();
            twofish.Init(true, new Org.BouncyCastle.Crypto.Parameters.KeyParameter(key));
            new Org.BouncyCastle.Crypto.Engines.ThreefishEngine(1024);

            Org.BouncyCastle.Crypto.BufferedBlockCipher cipher =
                new Org.BouncyCastle.Crypto.BufferedBlockCipher(new Org.BouncyCastle.Crypto.Engines.TwofishEngine());
        }