Пример #1
0
        /// <summary>
        /// Encrypt multiple blocks of data in CFB (Cipher Feedback Mode) and Zero-Padding.
        /// </summary>
        /// <param name="input"> plaintext bytes </param>
        /// <param name="output"> Encrypted bytes whereas first 8 bytes are the initialization vector</param>
        public void EncryptData(byte[] input, out byte[] output)
        {
            PrepareData(ref input, out var inputBuffer, out output);
            byte[] encryptIn = new byte[bSize];

            var algorithm = new Lambda1(key, OperationMode.Encrypt);

            for (int i = 0, j = 1; j < output.Length / bSize; ++i, ++j)
            {
                CopyChunk(ref encryptIn, 0, ref output, i * bSize, bSize);
                algorithm.ProcessBlock(encryptIn, out var encryptOut);
                CopyChunk(ref output, j * bSize, ref encryptOut, 0, bSize);
                XorChunk(ref output, j * bSize, ref inputBuffer, j * bSize, bSize);
            }
        }
Пример #2
0
        /// <summary>
        /// Decrypt multiple blocks of data in CFB (Cipher Feedback Mode) and Zero-Padding.
        /// The padding is NOT stripped.
        /// </summary>
        /// <param name="input"> Encrypted buffer whereas first 8 bytes are the initialization vector </param>
        /// <param name="output"> Decrypted bytes </param>
        public void DecryptData(byte[] input, out byte[] output)
        {
            byte[] outputBuffer = new byte[input.Length - bSize];
            byte[] decryptIn    = new byte[bSize];

            var algorithm = new Lambda1(key, OperationMode.Encrypt);

            for (int i = 0, j = 1; j < input.Length / bSize; ++i, ++j)
            {
                CopyChunk(ref decryptIn, 0, ref input, i * bSize, bSize);
                algorithm.ProcessBlock(decryptIn, out var decryptOut);
                CopyChunk(ref outputBuffer, i * bSize, ref decryptOut, 0, bSize);
                XorChunk(ref outputBuffer, i * bSize, ref input, j * bSize, bSize);
            }
            output = outputBuffer;
        }