예제 #1
0
        /// <summary>
        /// Encrypts a single block.
        /// </summary>
        /// <param name="input">Block to encrypt.</param>
        /// <returns>Encrypted block</returns>
        public byte[] Encrypt(byte[] input)
        {
            State state = new State(input);

            if (Debugging.IsEnabled)
            {
                Debugging.Trace("Encrypting these plaintext bytes:");
                ByteUtilities.WriteBytes(input);
                Debugging.Trace("");
                Debugging.Trace("Initial state:");
                Debugging.Trace(state.ToString());
                Debugging.Trace("");
            }

            _InitialRound.Apply(state, 0);

            int totalRounds = _Settings.Rounds;
            for (int round = 1; round < totalRounds; round++)
            {
                _IntermediateRound.Apply(state, round);
            }

            _FinalRound.Apply(state, totalRounds);

            byte[] encryptedBytes = state.ToByteArray();
            
            if(Debugging.IsEnabled)
            {
                Debugging.Trace("Encryption resulted in this ciphertext:");
                ByteUtilities.WriteBytes(encryptedBytes);
            }

            return encryptedBytes;
        }
예제 #2
0
        /// <summary>
        /// Decrypts a single block.
        /// </summary>
        /// <param name="input">Block to decrypt.</param>
        /// <returns>The decrypted block.</returns>
        public byte[] Decrypt(byte[] input)
        {
            // Decryption does everything in reverse
            State state = new State(input);
            int totalRounds = _Settings.Rounds;

            _FinalRound.Inverse(state, totalRounds);

            for (int round = (totalRounds - 1); round > 0; round--)
            {
                _IntermediateRound.Inverse(state, round);
            }

            _InitialRound.Inverse(state, 0);

            return state.ToByteArray();
        }