using System;
using System.Collections;
using Nequeo.Cryptography.Key.Crypto.Parameters;
using Nequeo.Cryptography.Key.Math;
using Nequeo.Cryptography.Key.Utilities;
namespace Nequeo.Cryptography.Key.Crypto.Engines
{
	/**
	* NaccacheStern Engine. For details on this cipher, please see
	* http://www.gemplus.com/smart/rd/publications/pdf/NS98pkcs.pdf
	*/
	public class NaccacheSternEngine
		: IAsymmetricBlockCipher
	{
		private bool forEncryption;
		private NaccacheSternKeyParameters key;
		private IList[] lookup = null;
		private bool debug = false;
		public string AlgorithmName
		{
			get { return "NaccacheStern"; }
		}
		/**
		* Initializes this algorithm. Must be called before all other Functions.
		*
		* @see Nequeo.Cryptography.Key.crypto.AsymmetricBlockCipher#init(bool,
		*      Nequeo.Cryptography.Key.crypto.CipherParameters)
		*/
		public void Init(
			bool				forEncryption,
			ICipherParameters	parameters)
		{
			this.forEncryption = forEncryption;
			if (parameters is ParametersWithRandom)
			{
				parameters = ((ParametersWithRandom) parameters).Parameters;
			}
			key = (NaccacheSternKeyParameters)parameters;
			// construct lookup table for faster decryption if necessary
			if (!this.forEncryption)
			{
				if (debug)
				{
					Console.WriteLine("Constructing lookup Array");
				}
				NaccacheSternPrivateKeyParameters priv = (NaccacheSternPrivateKeyParameters)key;
				IList primes = priv.SmallPrimesList;
				lookup = new IList[primes.Count];
				for (int i = 0; i < primes.Count; i++)
				{
					BigInteger actualPrime = (BigInteger) primes[i];
					int actualPrimeValue = actualPrime.IntValue;
					lookup[i] = Platform.CreateArrayList(actualPrimeValue);
					lookup[i].Add(BigInteger.One);
					if (debug)
					{
						Console.WriteLine("Constructing lookup ArrayList for " + actualPrimeValue);
					}
					BigInteger accJ = BigInteger.Zero;
					for (int j = 1; j < actualPrimeValue; j++)
					{
//						BigInteger bigJ = BigInteger.ValueOf(j);
//						accJ = priv.PhiN.Multiply(bigJ);
						accJ = accJ.Add(priv.PhiN);
						BigInteger comp = accJ.Divide(actualPrime);
						lookup[i].Add(priv.G.ModPow(comp, priv.Modulus));
					}
				}
			}
		}
		public bool Debug
		{
			set { this.debug = value; }
		}
		/**
		* Returns the input block size of this algorithm.
		*
		* @see Nequeo.Cryptography.Key.crypto.AsymmetricBlockCipher#GetInputBlockSize()
		*/
		public int GetInputBlockSize()
		{
			if (forEncryption)
			{
				// We can only encrypt values up to lowerSigmaBound
				return (key.LowerSigmaBound + 7) / 8 - 1;
			}
			else
			{
				// We pad to modulus-size bytes for easier decryption.
//				return key.Modulus.ToByteArray().Length;
				return key.Modulus.BitLength / 8 + 1;
			}
		}
		/**
		* Returns the output block size of this algorithm.
		*
		* @see Nequeo.Cryptography.Key.crypto.AsymmetricBlockCipher#GetOutputBlockSize()
		*/
		public int GetOutputBlockSize()
		{
			if (forEncryption)
			{
				// encrypted Data is always padded up to modulus size
//				return key.Modulus.ToByteArray().Length;
				return key.Modulus.BitLength / 8 + 1;
			}
			else
			{
				// decrypted Data has upper limit lowerSigmaBound
				return (key.LowerSigmaBound + 7) / 8 - 1;
			}
		}
		/**
		* Process a single Block using the Naccache-Stern algorithm.
		*
		* @see Nequeo.Cryptography.Key.crypto.AsymmetricBlockCipher#ProcessBlock(byte[],
		*      int, int)
		*/
		public byte[] ProcessBlock(
			byte[]	inBytes,
			int		inOff,
			int		length)
		{
			if (key == null)
				throw new InvalidOperationException("NaccacheStern engine not initialised");
			if (length > (GetInputBlockSize() + 1))
				throw new DataLengthException("input too large for Naccache-Stern cipher.\n");
			if (!forEncryption)
			{
				// At decryption make sure that we receive padded data blocks
				if (length < GetInputBlockSize())
				{
					throw new InvalidCipherTextException("BlockLength does not match modulus for Naccache-Stern cipher.\n");
				}
			}
			// transform input into BigInteger
			BigInteger input = new BigInteger(1, inBytes, inOff, length);
			if (debug)
			{
				Console.WriteLine("input as BigInteger: " + input);
			}
			byte[] output;
			if (forEncryption)
			{
				output = Encrypt(input);
			}
			else
			{
				IList plain = Platform.CreateArrayList();
				NaccacheSternPrivateKeyParameters priv = (NaccacheSternPrivateKeyParameters)key;
				IList primes = priv.SmallPrimesList;
				// Get Chinese Remainders of CipherText
				for (int i = 0; i < primes.Count; i++)
				{
					BigInteger exp = input.ModPow(priv.PhiN.Divide((BigInteger)primes[i]), priv.Modulus);
					IList al = lookup[i];
					if (lookup[i].Count != ((BigInteger)primes[i]).IntValue)
					{
						if (debug)
						{
							Console.WriteLine("Prime is " + primes[i] + ", lookup table has size " + al.Count);
						}
						throw new InvalidCipherTextException("Error in lookup Array for "
										+ ((BigInteger)primes[i]).IntValue
										+ ": Size mismatch. Expected ArrayList with length "
										+ ((BigInteger)primes[i]).IntValue + " but found ArrayList of length "
										+ lookup[i].Count);
					}
					int lookedup = al.IndexOf(exp);
					if (lookedup == -1)
					{
						if (debug)
						{
							Console.WriteLine("Actual prime is " + primes[i]);
							Console.WriteLine("Decrypted value is " + exp);
							Console.WriteLine("LookupList for " + primes[i] + " with size " + lookup[i].Count
											+ " is: ");
							for (int j = 0; j < lookup[i].Count; j++)
							{
								Console.WriteLine(lookup[i][j]);
							}
						}
						throw new InvalidCipherTextException("Lookup failed");
					}
					plain.Add(BigInteger.ValueOf(lookedup));
				}
				BigInteger test = chineseRemainder(plain, primes);
				// Should not be used as an oracle, so reencrypt output to see
				// if it corresponds to input
				// this breaks probabilisic encryption, so disable it. Anyway, we do
				// use the first n primes for key generation, so it is pretty easy
				// to guess them. But as stated in the paper, this is not a security
				// breach. So we can just work with the correct sigma.
				// if (debug) {
				//      Console.WriteLine("Decryption is " + test);
				// }
				// if ((key.G.ModPow(test, key.Modulus)).Equals(input)) {
				//      output = test.ToByteArray();
				// } else {
				//      if(debug){
				//          Console.WriteLine("Engine seems to be used as an oracle,
				//          returning null");
				//      }
				//      output = null;
				// }
				output = test.ToByteArray();
			}
			return output;
		}
		/**
		* Encrypts a BigInteger aka Plaintext with the public key.
		*
		* @param plain
		*            The BigInteger to encrypt
		* @return The byte[] representation of the encrypted BigInteger (i.e.
		*         crypted.toByteArray())
		*/
		public byte[] Encrypt(
			BigInteger plain)
		{
			// Always return modulus size values 0-padded at the beginning
			// 0-padding at the beginning is correctly parsed by BigInteger :)
//			byte[] output = key.Modulus.ToByteArray();
//			Array.Clear(output, 0, output.Length);
			byte[] output = new byte[key.Modulus.BitLength / 8 + 1];
			byte[] tmp = key.G.ModPow(plain, key.Modulus).ToByteArray();
			Array.Copy(tmp, 0, output, output.Length - tmp.Length, tmp.Length);
			if (debug)
			{
				Console.WriteLine("Encrypted value is:  " + new BigInteger(output));
			}
			return output;
		}
		/**
		* Adds the contents of two encrypted blocks mod sigma
		*
		* @param block1
		*            the first encrypted block
		* @param block2
		*            the second encrypted block
		* @return encrypt((block1 + block2) mod sigma)
		* @throws InvalidCipherTextException
		*/
		public byte[] AddCryptedBlocks(
			byte[] block1,
			byte[] block2)
		{
			// check for correct blocksize
			if (forEncryption)
			{
				if ((block1.Length > GetOutputBlockSize())
						|| (block2.Length > GetOutputBlockSize()))
				{
					throw new InvalidCipherTextException(
							"BlockLength too large for simple addition.\n");
				}
			}
			else
			{
				if ((block1.Length > GetInputBlockSize())


Answer: 						|| (block2.Length > GetInputBlockSize()))