static byte[] EncryptStatic(byte[] src, byte[] iv = null)
        {
            var key = new byte[] {
                0xD3, 0x61, 0x57, 0x17, 0xE2, 0x16, 0x3F, 0x70, 0xAC, 0x69, 0x51, 0xB2, 0x7D, 0x7A, 0x0B, 0x86,
                0xD8, 0xE9, 0x3E, 0x16, 0xEA, 0xBF, 0x63, 0x2F, 0xDF, 0xBC, 0xC0, 0x0A, 0x1D, 0x3D, 0x62, 0xD6
            };


            if (iv == null)
            {
                iv = new byte[8];
                using (var rng = RandomNumberGenerator.Create())
                    rng.GetBytes(iv);
            }

            var cce = new ChaChaEngine();

            cce.Init(true, new ParametersWithIV(new KeyParameter(key), iv));

            var bsc    = new BufferedStreamCipher(cce);
            var output = new byte[src.Length + 8];

            Array.Copy(iv, output, 8);
            bsc.ProcessBytes(src, 0, src.Length, output, 8);
            return(output);
        }
Esempio n. 2
0
        /// <summary>
        /// Generate cipher.
        /// </summary>
        /// <param name="forEncryption"></param>
        /// <param name="parameters">Parameters.</param>
        /// <returns></returns>
        /// <exception cref="Exception"/>
        public IBufferedCipher GenerateCipher(bool forEncryption, ICipherParameters parameters)
        {
            IStreamCipher   engine = GenerateEngine();
            IBufferedCipher cipher = new BufferedStreamCipher(engine);

            cipher.Init(forEncryption, parameters);
            return(cipher);
        }
Esempio n. 3
0
            public ICipherBuilder <Parameters> DoCreateCipherBuilder(bool forEncryption, Parameters parameters)
            {
                IBufferedCipher cipher = new BufferedStreamCipher(engineProvider.CreateEngine(forEncryption ? EngineUsage.ENCRYPTION : EngineUsage.DECRYPTION));

                cipher.Init(forEncryption, new ParametersWithRounds(new Internal.Parameters.ParametersWithIV(null, parameters.GetIV()), parameters.Rounds));

                return(new CipherBuilderImpl <Parameters> (parameters, cipher));
            }
Esempio n. 4
0
            public ICipherBuilder <Parameters> doCreateCipherBuilder(bool forEncryption, Parameters parameters)
            {
                IBufferedCipher cipher = new BufferedStreamCipher(engineProvider.CreateEngine(forEncryption ? EngineUsage.ENCRYPTION : EngineUsage.DECRYPTION));

                cipher.Init(forEncryption, null);

                return(new CipherBuilderImpl <Parameters> (parameters, cipher));
            }
        static byte[] Encrypt(byte[] src, byte[] key, byte[] iv)
        {
            var cce = new ChaChaEngine();

            cce.Init(true, new ParametersWithIV(new KeyParameter(key), iv));

            var bsc = new BufferedStreamCipher(cce);

            return(bsc.ProcessBytes(src));
        }
        private Byte[] Decrypt(BufferedStreamCipher cipher, Byte[] data)
        {
            var size      = cipher.GetOutputSize(data.Length);
            var outBuffer = new Byte[size];
            //int off = cipher.DoFinal(data, outBuffer, 0);
            var off = cipher.ProcessBytes(data, 0, data.Length, outBuffer, 0);
            //cipher.DoFinal(data, 0, data.Length, outBuffer, 0);
            //cipher.DoFinal(outBuffer, off1);
            var result = new Byte[off];

            Array.Copy(outBuffer, 0, result, 0, result.Length);

            //String asString = Encoding.ASCII.GetString(result);
            //Console.WriteLine(asString);

            return(result);
        }
        static byte[] DecryptStatic(byte[] src, out byte[] iv)
        {
            var key = new byte[] {
                0xD3, 0x61, 0x57, 0x17, 0xE2, 0x16, 0x3F, 0x70, 0xAC, 0x69, 0x51, 0xB2, 0x7D, 0x7A, 0x0B, 0x86,
                0xD8, 0xE9, 0x3E, 0x16, 0xEA, 0xBF, 0x63, 0x2F, 0xDF, 0xBC, 0xC0, 0x0A, 0x1D, 0x3D, 0x62, 0xD6
            };

            iv = new byte[8];
            Array.Copy(src, iv, 8);

            var cce = new ChaChaEngine();

            cce.Init(false, new ParametersWithIV(new KeyParameter(key), iv));

            var bsc = new BufferedStreamCipher(cce);

            return(bsc.ProcessBytes(src, 8, src.Length - 8));
        }
Esempio n. 8
0
        public ICipher BuildCipher(Stream stream)
        {
            object cipher = EnvelopedDataHelper.CreateContentCipher(true, encKey, algorithmIdentifier);

            //
            // BufferedBlockCipher
            // IStreamCipher
            //

            if (cipher is IStreamCipher)
            {
                cipher = new BufferedStreamCipher((IStreamCipher)cipher);
            }

            if (stream == null)
            {
                stream = new MemoryStream();
            }

            return(new BufferedCipherWrapper((IBufferedCipher)cipher, stream));
        }