/**
        * initialise a DESede cipher.
        *
        * @param forEncryption whether or not we are for encryption.
        * @param parameters the parameters required to set up the cipher.
        * @exception ArgumentException if the parameters argument is
        * inappropriate.
        */
        public override void Init(
            bool				forEncryption,
            ICipherParameters	parameters)
        {
            if (!(parameters is KeyParameter))
                throw new ArgumentException("invalid parameter passed to DESede init - " + parameters.GetType().ToString());

            byte[] keyMaster = ((KeyParameter)parameters).GetKey();
            if (keyMaster.Length != 24 && keyMaster.Length != 16)
                throw new ArgumentException("key size must be 16 or 24 bytes.");

            this.forEncryption = forEncryption;

            byte[] key1 = new byte[8];
            Array.Copy(keyMaster, 0, key1, 0, key1.Length);
            workingKey1 = GenerateWorkingKey(forEncryption, key1);

            byte[] key2 = new byte[8];
            Array.Copy(keyMaster, 8, key2, 0, key2.Length);
            workingKey2 = GenerateWorkingKey(!forEncryption, key2);

            if (keyMaster.Length == 24)
            {
                byte[] key3 = new byte[8];
                Array.Copy(keyMaster, 16, key3, 0, key3.Length);
                workingKey3 = GenerateWorkingKey(forEncryption, key3);
            }
            else        // 16 byte key
            {
                workingKey3 = workingKey1;
            }
        }
Example #2
0
        public void Init(bool encrypting, ICipherParameters parameters)
        {
            if (!(parameters is KeyParameter))
              {
            throw new ArgumentException("Invalid parameter passed to "+
              "DesSsh1Engine init - " + parameters.GetType());
              }

              this.encrypting = encrypting;

              byte[] passphraseKey = (parameters as KeyParameter).GetKey();
              if (passphraseKey.Length !=16)
              {
            throw new ArgumentException("key size different than 16 bytes");
              }

              byte[] keyPart1 = new byte[8];
              byte[] keyPart2 = new byte[8];

              Array.Copy(passphraseKey, keyPart1, 8);
              Array.Copy(passphraseKey, 8, keyPart2, 0, 8);

              desEngine1 = new CbcBlockCipher(new DesEngine());
              desEngine2 = new CbcBlockCipher(new DesEngine());
              desEngine3 = new CbcBlockCipher(new DesEngine());

              desEngine1.Init(encrypting, new KeyParameter(keyPart1));
              desEngine2.Init(!encrypting, new KeyParameter(keyPart2));
              desEngine3.Init(encrypting, new KeyParameter(keyPart1));
        }
        /**
        * initialise a SKIPJACK cipher.
        *
        * @param forEncryption whether or not we are for encryption.
        * @param parameters the parameters required to set up the cipher.
        * @exception ArgumentException if the parameters argument is
        * inappropriate.
        */
        public virtual void Init(
            bool				forEncryption,
            ICipherParameters	parameters)
        {
            if (!(parameters is KeyParameter))
	            throw new ArgumentException("invalid parameter passed to SKIPJACK init - " + parameters.GetType().ToString());

			byte[] keyBytes = ((KeyParameter)parameters).GetKey();

            this.encrypting = forEncryption;
            this.key0 = new int[32];
            this.key1 = new int[32];
            this.key2 = new int[32];
            this.key3 = new int[32];

            //
            // expand the key to 128 bytes in 4 parts (saving us a modulo, multiply
            // and an addition).
            //
            for (int i = 0; i < 32; i ++)
            {
                key0[i] = keyBytes[(i * 4) % 10] & 0xff;
                key1[i] = keyBytes[(i * 4 + 1) % 10] & 0xff;
                key2[i] = keyBytes[(i * 4 + 2) % 10] & 0xff;
                key3[i] = keyBytes[(i * 4 + 3) % 10] & 0xff;
            }
        }
Example #4
0
		public void Init(
			ICipherParameters parameters)
		{
			Reset();
			buf = new byte[blockSize];
			if (parameters is ParametersWithSBox)
			{
				ParametersWithSBox param = (ParametersWithSBox)parameters;

				//
				// Set the S-Box
				//
				param.GetSBox().CopyTo(this.S, 0);

				//
				// set key if there is one
				//
				if (param.Parameters != null)
				{
					workingKey = generateWorkingKey(((KeyParameter)param.Parameters).GetKey());
				}
			}
			else if (parameters is KeyParameter)
			{
				workingKey = generateWorkingKey(((KeyParameter)parameters).GetKey());
			}
			else
			{
				throw new ArgumentException("invalid parameter passed to Gost28147 init - "
					+ parameters.GetType().Name);
			}
		}
        /**
        * Initialise a HC-256 cipher.
        *
        * @param forEncryption whether or not we are for encryption. Irrelevant, as
        *                      encryption and decryption are the same.
        * @param params        the parameters required to set up the cipher.
        * @throws ArgumentException if the params argument is
        *                                  inappropriate (ie. the key is not 256 bit long).
        */
        public void Init(
            bool				forEncryption,
            ICipherParameters	parameters)
        {
            ICipherParameters keyParam = parameters;

            if (parameters is ParametersWithIV)
            {
                iv = ((ParametersWithIV)parameters).GetIV();
                keyParam = ((ParametersWithIV)parameters).Parameters;
            }
            else
            {
                iv = new byte[0];
            }

            if (keyParam is KeyParameter)
            {
                key = ((KeyParameter)keyParam).GetKey();
                Init();
            }
            else
            {
                throw new ArgumentException(
                    "Invalid parameter passed to HC256 init - " + parameters.GetType().Name,
                    "parameters");
            }

            initialised = true;
        }
Example #6
0
		/**
        * initialise a DES cipher.
        *
        * @param forEncryption whether or not we are for encryption.
        * @param parameters the parameters required to set up the cipher.
        * @exception ArgumentException if the parameters argument is
        * inappropriate.
        */
        public virtual void Init(
            bool				forEncryption,
            ICipherParameters	parameters)
        {
            if (!(parameters is KeyParameter))
				throw new ArgumentException("invalid parameter passed to DES init - " + parameters.GetType().ToString());

			workingKey = GenerateWorkingKey(forEncryption, ((KeyParameter)parameters).GetKey());
        }
        private int           X0, X1, X2, X3;    // registers

        /**
        * initialise a Serpent cipher.
        *
        * @param forEncryption whether or not we are for encryption.
        * @param parameters the parameters required to set up the cipher.
        * @exception ArgumentException if the parameters argument is
        * inappropriate.
        */
        public virtual void Init(
            bool				forEncryption,
            ICipherParameters	parameters)
        {
            if (!(parameters is KeyParameter))
				throw new ArgumentException("invalid parameter passed to Serpent init - " + parameters.GetType().ToString());

			this.encrypting = forEncryption;
            this.wKey = MakeWorkingKey(((KeyParameter)parameters).GetKey());
        }
Example #8
0
        /**
        * initialise a RC5-32 cipher.
        *
        * @param forEncryption whether or not we are for encryption.
        * @param parameters the parameters required to set up the cipher.
        * @exception ArgumentException if the parameters argument is
        * inappropriate.
        */
        public void Init(
            bool				forEncryption,
            ICipherParameters	parameters)
        {
            if (!(parameters is KeyParameter))
                throw new ArgumentException("invalid parameter passed to RC6 init - " + parameters.GetType().ToString());

            this.forEncryption = forEncryption;

			KeyParameter p = (KeyParameter)parameters;
			SetKey(p.GetKey());
        }
		/**
		* initialise
		*
		* @param forEncryption whether or not we are for encryption.
		* @param params the parameters required to set up the cipher.
		* @exception ArgumentException if the params argument is
		* inappropriate.
		*/
		public virtual void Init(
			bool				forEncryption,
			ICipherParameters	parameters)
		{
			if (!(parameters is KeyParameter))
				throw new ArgumentException("Invalid parameters passed to Noekeon init - " + parameters.GetType().Name, "parameters");

			_forEncryption = forEncryption;
			_initialised = true;

			KeyParameter p = (KeyParameter) parameters;

			setKey(p.GetKey());
		}
Example #10
0
        /**
        * initialise a RC5-64 cipher.
        *
        * @param forEncryption whether or not we are for encryption.
        * @param parameters the parameters required to set up the cipher.
        * @exception ArgumentException if the parameters argument is
        * inappropriate.
        */
        public void Init(
            bool             forEncryption,
            ICipherParameters    parameters)
        {
            if (!(parameters is RC5Parameters))
            {
                throw new ArgumentException("invalid parameter passed to RC564 init - " + parameters.GetType().ToString());
            }

            RC5Parameters       p = (RC5Parameters)parameters;

            this.forEncryption = forEncryption;

            _noRounds     = p.Rounds;

            SetKey(p.GetKey());
        }
Example #11
0
		/**
		* initialise an ISAAC cipher.
		*
		* @param forEncryption whether or not we are for encryption.
		* @param params the parameters required to set up the cipher.
		* @exception ArgumentException if the params argument is
		* inappropriate.
		*/
		public void Init(
			bool				forEncryption, 
			ICipherParameters	parameters)
		{
			if (!(parameters is KeyParameter))
				throw new ArgumentException(
					"invalid parameter passed to ISAAC Init - " + parameters.GetType().Name,
					"parameters");

			/* 
			* ISAAC encryption and decryption is completely
			* symmetrical, so the 'forEncryption' is 
			* irrelevant.
			*/
			KeyParameter p = (KeyParameter) parameters;
			setKey(p.GetKey());
		}
Example #12
0
		/**
		* initialise
		*
		* @param forEncryption whether or not we are for encryption.
		* @param params the parameters required to set up the cipher.
		* @exception ArgumentException if the params argument is
		* inappropriate.
		*/
		public void Init(
			bool				forEncryption,
			ICipherParameters	parameters)
		{
			if (!(parameters is KeyParameter))
			{
				throw new ArgumentException("invalid parameter passed to TEA init - "
					+ parameters.GetType().FullName);
			}

			_forEncryption = forEncryption;
			_initialised = true;

			KeyParameter p = (KeyParameter) parameters;

			setKey(p.GetKey());
		}
Example #13
0
        /**
        * initialise a RC4 cipher.
        *
        * @param forEncryption whether or not we are for encryption.
        * @param parameters the parameters required to set up the cipher.
        * @exception ArgumentException if the parameters argument is
        * inappropriate.
        */
        public void Init(
            bool				forEncryption,
            ICipherParameters	parameters)
        {
            if (parameters is KeyParameter)
            {
                /*
                * RC4 encryption and decryption is completely
                * symmetrical, so the 'forEncryption' is
                * irrelevant.
                */
                workingKey = ((KeyParameter)parameters).GetKey();
                SetKey(workingKey);

                return;
            }

            throw new ArgumentException("invalid parameter passed to RC4 init - " + parameters.GetType().ToString());
        }
Example #14
0
		/// <summary>
		/// Optionally initialises the Skein digest with the provided parameters.
		/// </summary>
		/// See <see cref="NBitcoin.BouncyCastle.Crypto.Parameters.SkeinParameters"></see> for details on the parameterisation of the Skein hash function.
		/// <param name="parameters">the parameters to apply to this engine, or <code>null</code> to use no parameters.</param>
		public void Init(ICipherParameters parameters)
		{
			SkeinParameters skeinParameters;
			if (parameters is SkeinParameters)
			{
				skeinParameters = (SkeinParameters)parameters;
			}
			else if (parameters is KeyParameter)
			{
				skeinParameters = new SkeinParameters.Builder().SetKey(((KeyParameter)parameters).GetKey()).Build();
			}
			else
			{
				throw new ArgumentException("Invalid parameter passed to Skein MAC init - "
				                            + parameters.GetType().Name);
			}
			if (skeinParameters.GetKey() == null)
			{
				throw new ArgumentException("Skein MAC requires a key parameter.");
			}
			engine.Init(skeinParameters);
		}
		/// <summary>
		/// Initialise the engine.
		/// </summary>
		/// <param name="forEncryption">Initialise for encryption if true, for decryption if false.</param>
		/// <param name="parameters">an instance of <see cref="TweakableBlockCipherParameters"/> or <see cref="KeyParameter"/> (to
		///               use a 0 tweak)</param>
		public void Init(bool forEncryption, ICipherParameters parameters)
		{
			byte[] keyBytes;
			byte[] tweakBytes;

			if (parameters is TweakableBlockCipherParameters)
			{
				TweakableBlockCipherParameters tParams = (TweakableBlockCipherParameters)parameters;
				keyBytes = tParams.Key.GetKey();
				tweakBytes = tParams.Tweak;
			}
			else if (parameters is KeyParameter)
			{
				keyBytes = ((KeyParameter)parameters).GetKey();
				tweakBytes = null;
			}
			else
			{
				throw new ArgumentException("Invalid parameter passed to Threefish init - "
				                            + parameters.GetType().Name);
			}

			ulong[] keyWords = null;
			ulong[] tweakWords = null;

			if (keyBytes != null)
			{
				if (keyBytes.Length != this.blocksizeBytes)
				{
					throw new ArgumentException("Threefish key must be same size as block (" + blocksizeBytes
					                            + " bytes)");
				}
				keyWords = new ulong[blocksizeWords];
				for (int i = 0; i < keyWords.Length; i++)
				{
					keyWords[i] = BytesToWord(keyBytes, i * 8);
				}
			}
			if (tweakBytes != null)
			{
				if (tweakBytes.Length != TWEAK_SIZE_BYTES)
				{
					throw new ArgumentException("Threefish tweak must be " + TWEAK_SIZE_BYTES + " bytes");
				}
				tweakWords = new ulong[]{BytesToWord(tweakBytes, 0), BytesToWord(tweakBytes, 8)};
			}
			Init(forEncryption, keyWords, tweakWords);
		}
Example #16
0
        /**
        * initialise a RC5-32 cipher.
        *
        * @param forEncryption whether or not we are for encryption.
        * @param parameters the parameters required to set up the cipher.
        * @exception ArgumentException if the parameters argument is
        * inappropriate.
        */
        public void Init(
            bool				forEncryption,
            ICipherParameters	parameters)
        {
            if (parameters is RC5Parameters) {
                RC5Parameters p = (RC5Parameters)parameters;

                _noRounds = p.Rounds;

                SetKey (p.GetKey ());
            } else if (parameters is KeyParameter) {
                KeyParameter p = (KeyParameter)parameters;

                SetKey (p.GetKey ());
            } else {
                throw new ArgumentException ("invalid parameter passed to RC532 init - " + parameters.GetType ().ToString ());
            }

            this.forEncryption = forEncryption;
        }
Example #17
0
        public virtual void Init(bool forEncryption, ICipherParameters parameters)
        {
            if (!(parameters is KeyParameter))
            {
                throw new ArgumentException("invalid parameter passed to ISAAC Init - " + parameters.GetType().Name, "parameters");
            }
            KeyParameter keyParameter = (KeyParameter)parameters;

            this.setKey(keyParameter.GetKey());
        }
        /**
         * initialise an Gost28147 cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param parameters the parameters required to set up the cipher.
         * @exception ArgumentException if the parameters argument is inappropriate.
         */
        public virtual void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            if (parameters is ParametersWithSBox)
            {
                ParametersWithSBox param = (ParametersWithSBox)parameters;

                //
                // Set the S-Box
                //
                byte[] sBox = param.GetSBox();
                if (sBox.Length != Sbox_Default.Length)
                {
                    throw new ArgumentException("invalid S-box passed to GOST28147 init");
                }

                this.S = Arrays.Clone(sBox);

                //
                // set key if there is one
                //
                if (param.Parameters != null)
                {
                    workingKey = generateWorkingKey(forEncryption,
                                                    ((KeyParameter)param.Parameters).GetKey());
                }
            }
            else if (parameters is KeyParameter)
            {
                workingKey = generateWorkingKey(forEncryption,
                                                ((KeyParameter)parameters).GetKey());
            }
            else if (parameters != null)
            {
                throw new ArgumentException("invalid parameter passed to Gost28147 init - " + parameters.GetType().Name);
            }
        }
Example #19
0
        /**
         * initialise an AES cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param parameters the parameters required to set up the cipher.
         * @exception ArgumentException if the parameters argument is
         * inappropriate.
         */
        public void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            if (!(parameters is KeyParameter))
            {
                throw new ArgumentException("invalid parameter passed to AES init - " + parameters.GetType().ToString());
            }

            WorkingKey         = GenerateWorkingKey(((KeyParameter)parameters).GetKey(), forEncryption);
            this.forEncryption = forEncryption;
        }
Example #20
0
        /**
         * initialise a RC5-32 cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param parameters the parameters required to set up the cipher.
         * @exception ArgumentException if the parameters argument is
         * inappropriate.
         */

        public void Init(bool forEncryption, ICipherParameters parameters)
        {
            if (parameters is RC5Parameters)
            {
                var p = (RC5Parameters)parameters;

                _noRounds = p.Rounds;

                SetKey(p.GetKey());
            }
            else if (parameters is KeyParameter)
            {
                var p = (KeyParameter)parameters;

                SetKey(p.GetKey());
            }
            else
            {
                throw new ArgumentException("invalid parameter passed to RC532 init - " + parameters.GetType());
            }

            this.forEncryption = forEncryption;
        }
Example #21
0
        /**
        * initialise a RC2 cipher.
        *
        * @param forEncryption whether or not we are for encryption.
        * @param parameters the parameters required to set up the cipher.
        * @exception ArgumentException if the parameters argument is
        * inappropriate.
        */
        public void Init(
            bool				forEncryption,
            ICipherParameters	parameters)
        {
            this.encrypting = forEncryption;

			if (parameters is RC2Parameters)
            {
                RC2Parameters param = (RC2Parameters) parameters;

				workingKey = GenerateWorkingKey(param.GetKey(), param.EffectiveKeyBits);
            }
            else if (parameters is KeyParameter)
            {
				KeyParameter param = (KeyParameter) parameters;
				byte[] key = param.GetKey();

				workingKey = GenerateWorkingKey(key, key.Length * 8);
            }
            else
            {
                throw new ArgumentException("invalid parameter passed to RC2 init - " + parameters.GetType().Name);
            }
        }
        /**
        * initialise an Gost28147 cipher.
        *
        * @param forEncryption whether or not we are for encryption.
        * @param parameters the parameters required to set up the cipher.
        * @exception ArgumentException if the parameters argument is inappropriate.
        */
        public void Init(
			bool				forEncryption,
			ICipherParameters	parameters)
        {
            if (parameters is ParametersWithSBox)
            {
                ParametersWithSBox   param = (ParametersWithSBox)parameters;

                //
                // Set the S-Box
                //
                Array.Copy(param.GetSBox(), 0, this.S, 0, param.GetSBox().Length);

                //
                // set key if there is one
                //
                if (param.Parameters != null)
                {
                    workingKey = generateWorkingKey(forEncryption,
                            ((KeyParameter)param.Parameters).GetKey());
                }
            }
            else if (parameters is KeyParameter)
            {
                workingKey = generateWorkingKey(forEncryption,
                                    ((KeyParameter)parameters).GetKey());
            }
            else
            {
                throw new ArgumentException("invalid parameter passed to Gost28147 init - " + parameters.GetType().Name);
            }
        }
Example #23
0
        public virtual void Init(bool forEncryption, ICipherParameters parameters)
        {
            if (!(parameters is KeyParameter))
            {
                throw new ArgumentException("invalid parameter passed to TEA init - " + parameters.GetType().FullName);
            }
            this._forEncryption = forEncryption;
            this._initialised   = true;
            KeyParameter keyParameter = (KeyParameter)parameters;

            this.setKey(keyParameter.GetKey());
        }
Example #24
0
        /**
        * initialise an AES cipher.
        *
        * @param forEncryption whether or not we are for encryption.
        * @param parameters the parameters required to set up the cipher.
        * @exception ArgumentException if the parameters argument is
        * inappropriate.
        */
        public void Init(
            bool				forEncryption,
            ICipherParameters	parameters)
        {
            if (!(parameters is KeyParameter))
                throw new ArgumentException("invalid parameter passed to AES init - " + parameters.GetType().ToString());

            WorkingKey = GenerateWorkingKey(((KeyParameter)parameters).GetKey(), forEncryption);
            this.forEncryption = forEncryption;
        }
        private int X0, X1, X2, X3;              // registers

        /**
         * initialise a Serpent cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param parameters the parameters required to set up the cipher.
         * @exception ArgumentException if the parameters argument is
         * inappropriate.
         */
        public virtual void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            if (!(parameters is KeyParameter))
            {
                throw new ArgumentException("invalid parameter passed to Serpent init - " + parameters.GetType().ToString());
            }

            this.encrypting = forEncryption;
            this.wKey       = MakeWorkingKey(((KeyParameter)parameters).GetKey());
        }
        /**
         * initialise
         *
         * @param forEncryption whether or not we are for encryption.
         * @param params the parameters required to set up the cipher.
         * @exception ArgumentException if the params argument is
         * inappropriate.
         */
        public virtual void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            if (!(parameters is KeyParameter))
            {
                throw new ArgumentException("Invalid parameters passed to Noekeon init - " + parameters.GetType().Name, "parameters");
            }

            _forEncryption = forEncryption;
            _initialised   = true;

            KeyParameter p = (KeyParameter)parameters;

            setKey(p.GetKey());
        }
Example #27
0
        /**
         * initialise a RC5-32 cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param parameters the parameters required to set up the cipher.
         * @exception ArgumentException if the parameters argument is
         * inappropriate.
         */
        public virtual void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            if (typeof(RC5Parameters).IsInstanceOfType(parameters))
            {
                RC5Parameters p = (RC5Parameters)parameters;

                _noRounds = p.Rounds;

                SetKey(p.GetKey());
            }
            else if (typeof(KeyParameter).IsInstanceOfType(parameters))
            {
                KeyParameter p = (KeyParameter)parameters;

                SetKey(p.GetKey());
            }
            else
            {
                throw new ArgumentException("invalid parameter passed to RC532 init - " + parameters.GetType().ToString());
            }

            this.forEncryption = forEncryption;
        }
Example #28
0
        /**
         * initialise a RC5-32 cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param parameters the parameters required to set up the cipher.
         * @exception ArgumentException if the parameters argument is
         * inappropriate.
         */
        public void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            //TODO: SUSTITUIDO. CAMBIAMOS EL IsInstanceOfType POR UN BLOQUE try - catch
            //if (typeof(RC5Parameters).IsInstanceOfType(parameters))
            //{
            //    RC5Parameters p = (RC5Parameters)parameters;
            //    _noRounds = p.Rounds;
            //    SetKey(p.GetKey());
            //}
            //else if (typeof(KeyParameter).GetType().IsInstanceOfType(parameters))
            //{
            //    KeyParameter p = (KeyParameter)parameters;
            //    SetKey(p.GetKey());
            //}
            //else
            //{
            //    throw new ArgumentException("invalid parameter passed to RC532 init - " + parameters.GetType().ToString());
            //}
            //this.forEncryption = forEncryption;

            try{
                RC5Parameters p = (RC5Parameters)parameters;
                _noRounds = p.Rounds;
                SetKey(p.GetKey());
            }
            catch (Exception e) {
                try
                {
                    KeyParameter p = (KeyParameter)parameters;
                    SetKey(p.GetKey());
                }
                catch (Exception e2)
                {
                    throw new ArgumentException("invalid parameter passed to RC532 init - " + parameters.GetType().ToString() + " e:" + e + " : " + e2);
                }
            }

            this.forEncryption = forEncryption;
        }
        /**
        * initialise a Twofish cipher.
        *
        * @param forEncryption whether or not we are for encryption.
        * @param parameters the parameters required to set up the cipher.
        * @exception ArgumentException if the parameters argument is
        * inappropriate.
        */
        public void Init(
            bool              forEncryption,
            ICipherParameters parameters)
        {
            if (!(parameters is KeyParameter))
				throw new ArgumentException("invalid parameter passed to Twofish init - " + parameters.GetType().ToString());

			this.encrypting = forEncryption;
			this.workingKey = ((KeyParameter)parameters).GetKey();
			this.k64Cnt = (this.workingKey.Length / 8); // pre-padded ?
			SetKey(this.workingKey);
        }
Example #30
0
        public void Init(ICipherParameters parameters)
        {
            SkeinParameters skeinParameters;

            if (parameters is SkeinParameters)
            {
                skeinParameters = (SkeinParameters)parameters;
            }
            else
            {
                if (!(parameters is KeyParameter))
                {
                    throw new ArgumentException("Invalid parameter passed to Skein MAC init - " + parameters.GetType().Name);
                }
                skeinParameters = new SkeinParameters.Builder().SetKey(((KeyParameter)parameters).GetKey()).Build();
            }
            if (skeinParameters.GetKey() == null)
            {
                throw new ArgumentException("Skein MAC requires a key parameter.");
            }
            this.engine.Init(skeinParameters);
        }
Example #31
0
        /**
        * initialise a CAST cipher.
        *
        * @param forEncryption whether or not we are for encryption.
        * @param parameters the parameters required to set up the cipher.
        * @exception ArgumentException if the parameters argument is
        * inappropriate.
        */
        public void Init(
            bool				forEncryption,
            ICipherParameters	parameters)
        {
            if (!(parameters is KeyParameter))
				throw new ArgumentException("Invalid parameter passed to "+ AlgorithmName +" init - " + parameters.GetType().ToString());

			_encrypting = forEncryption;
			_workingKey = ((KeyParameter)parameters).GetKey();
			SetKey(_workingKey);
        }
Example #32
0
        public virtual void Init(bool forEncryption, ICipherParameters parameters)
        {
            ICipherParameters cipherParameters = parameters;

            if (parameters is ParametersWithIV)
            {
                this.iv          = ((ParametersWithIV)parameters).GetIV();
                cipherParameters = ((ParametersWithIV)parameters).Parameters;
            }
            else
            {
                this.iv = new byte[0];
            }
            if (cipherParameters is KeyParameter)
            {
                this.key = ((KeyParameter)cipherParameters).GetKey();
                this.Init();
                this.initialised = true;
                return;
            }
            throw new ArgumentException("Invalid parameter passed to HC128 init - " + parameters.GetType().Name, "parameters");
        }
Example #33
0
        /**
         * initialise a DESede cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param parameters the parameters required to set up the cipher.
         * @exception ArgumentException if the parameters argument is
         * inappropriate.
         */
        public override void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            if (parameters == null)
            {
                return;  // already initialised
            }
            if (!(parameters is KeyParameter))
            {
                throw new ArgumentException("invalid parameter passed to DESede init - " + parameters.GetType().ToString());
            }

            byte[] keyMaster = ((KeyParameter)parameters).GetKey();
            if (keyMaster.Length != 24 && keyMaster.Length != 16)
            {
                throw new ArgumentException("key size must be 16 or 24 bytes.");
            }

            this.forEncryption = forEncryption;

            byte[] key1 = new byte[8];
            Array.Copy(keyMaster, 0, key1, 0, key1.Length);
            workingKey1 = GenerateWorkingKey(forEncryption, key1);

            byte[] key2 = new byte[8];
            Array.Copy(keyMaster, 8, key2, 0, key2.Length);
            workingKey2 = GenerateWorkingKey(!forEncryption, key2);

            if (keyMaster.Length == 24)
            {
                byte[] key3 = new byte[8];
                Array.Copy(keyMaster, 16, key3, 0, key3.Length);
                workingKey3 = GenerateWorkingKey(forEncryption, key3);
            }
            else        // 16 byte key
            {
                workingKey3 = workingKey1;
            }

            // note: blockCount can only go to zero on cipher initialization.
            blockCount = 0;
        }
Example #34
0
 public void Init(bool forEncryption, ICipherParameters parameters)
 {
     if (!(parameters is KeyParameter))
     {
         throw new ArgumentException("invalid parameter passed to Twofish init - " + parameters.GetType().ToString());
     }
     this.encrypting = forEncryption;
     this.workingKey = ((KeyParameter)parameters).GetKey();
     this.k64Cnt     = this.workingKey.Length / 8;
     this.SetKey(this.workingKey);
 }
Example #35
0
        /**
         * initialise a RC5-64 cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param parameters the parameters required to set up the cipher.
         * @exception ArgumentException if the parameters argument is
         * inappropriate.
         */
        public void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            if (!(typeof(RC5Parameters).IsInstanceOfType(parameters)))
            {
                throw new ArgumentException("invalid parameter passed to RC564 init - " + parameters.GetType().ToString());
            }

            RC5Parameters p = (RC5Parameters)parameters;

            this.forEncryption = forEncryption;

            _noRounds = p.Rounds;

            SetKey(p.GetKey());
        }
Example #36
0
        /**
         * initialise a RC5-32 cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param parameters the parameters required to set up the cipher.
         * @exception ArgumentException if the parameters argument is
         * inappropriate.
         */
        public void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            if (!(parameters is KeyParameter))
            {
                throw new ArgumentException("invalid parameter passed to RC6 init - " + parameters.GetType().ToString());
            }

            this.forEncryption = forEncryption;

            KeyParameter p = (KeyParameter)parameters;

            SetKey(p.GetKey());
        }
Example #37
0
        /**
         * initialise a RC2 cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param parameters the parameters required to set up the cipher.
         * @exception ArgumentException if the parameters argument is
         * inappropriate.
         */
        public virtual void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            this.encrypting = forEncryption;

            if (parameters is RC2Parameters)
            {
                RC2Parameters param = (RC2Parameters)parameters;

                workingKey = GenerateWorkingKey(param.GetKey(), param.EffectiveKeyBits);
            }
            else if (parameters is KeyParameter)
            {
                KeyParameter param = (KeyParameter)parameters;
                byte[]       key   = param.GetKey();

                workingKey = GenerateWorkingKey(key, key.Length * 8);
            }
            else
            {
                throw new ArgumentException("invalid parameter passed to RC2 init - " + parameters.GetType().Name);
            }
        }
Example #38
0
        /**
         * initialise an Gost28147 cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param parameters the parameters required to set up the cipher.
         * @exception ArgumentException if the parameters argument is inappropriate.
         */
        public void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            if (parameters is ParametersWithSBox)
            {
                ParametersWithSBox param = (ParametersWithSBox)parameters;

                //
                // Set the S-Box
                //
                Array.Copy(param.GetSBox(), 0, this.S, 0, param.GetSBox().Length);

                //
                // set key if there is one
                //
                if (param.Parameters != null)
                {
                    workingKey = generateWorkingKey(forEncryption,
                                                    ((KeyParameter)param.Parameters).GetKey());
                }
            }
            else if (parameters is KeyParameter)
            {
                workingKey = generateWorkingKey(forEncryption,
                                                ((KeyParameter)parameters).GetKey());
            }
            else
            {
                throw new ArgumentException("invalid parameter passed to Gost28147 init - " + parameters.GetType().Name);
            }
        }
Example #39
0
 public override void Init(bool forEncryption, ICipherParameters parameters)
 {
     if (!(parameters is KeyParameter))
     {
         throw new ArgumentException("invalid parameter passed to DESede init - " + parameters.GetType().ToString());
     }
     byte[] key = ((KeyParameter)parameters).GetKey();
     if (key.Length != 24 && key.Length != 16)
     {
         throw new ArgumentException("key size must be 16 or 24 bytes.");
     }
     this.forEncryption = forEncryption;
     byte[] array = new byte[8];
     Array.Copy(key, 0, array, 0, array.Length);
     workingKey1 = DesEngine.GenerateWorkingKey(forEncryption, array);
     byte[] array2 = new byte[8];
     Array.Copy(key, 8, array2, 0, array2.Length);
     workingKey2 = DesEngine.GenerateWorkingKey(!forEncryption, array2);
     if (key.Length == 24)
     {
         byte[] array3 = new byte[8];
         Array.Copy(key, 16, array3, 0, array3.Length);
         workingKey3 = DesEngine.GenerateWorkingKey(forEncryption, array3);
     }
     else
     {
         workingKey3 = workingKey1;
     }
 }
        /**
         * initialise a Rijndael cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param parameters the parameters required to set up the cipher.
         * @exception ArgumentException if the parameters argument is
         * inappropriate.
         */

        public void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            var parameter = parameters as KeyParameter;

            if (parameter != null)
            {
                workingKey         = GenerateWorkingKey(parameter.GetKey());
                this.forEncryption = forEncryption;
                return;
            }

            throw new ArgumentException("invalid parameter passed to Rijndael init - " + parameters.GetType());
        }
        /**
        * initialise an Gost28147 cipher.
        *
        * @param forEncryption whether or not we are for encryption.
        * @param parameters the parameters required to set up the cipher.
        * @exception ArgumentException if the parameters argument is inappropriate.
        */
        public void Init(
			bool				forEncryption,
			ICipherParameters	parameters)
        {
            if (parameters is ParametersWithSBox)
            {
                ParametersWithSBox   param = (ParametersWithSBox)parameters;

                //
                // Set the S-Box
                //
                byte[] sBox = param.GetSBox();
                if (sBox.Length != Sbox_Default.Length)
                    throw new ArgumentException("invalid S-box passed to GOST28147 init");

                this.S = Arrays.Clone(sBox);

                //
                // set key if there is one
                //
                if (param.Parameters != null)
                {
                    workingKey = generateWorkingKey(forEncryption,
                            ((KeyParameter)param.Parameters).GetKey());
                }
            }
            else if (parameters is KeyParameter)
            {
                workingKey = generateWorkingKey(forEncryption,
                                    ((KeyParameter)parameters).GetKey());
            }
            else if (parameters != null)
            {
                throw new ArgumentException("invalid parameter passed to Gost28147 init - " + parameters.GetType().Name);
            }
        }
Example #42
0
        /**
         * initialise a DESede cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param parameters the parameters required to set up the cipher.
         * @exception ArgumentException if the parameters argument is
         * inappropriate.
         */
        public override void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            if (!(parameters is KeyParameter))
            {
                throw new ArgumentException("invalid parameter passed to DESede init - " + parameters.GetType().ToString());
            }

            byte[] keyMaster = ((KeyParameter)parameters).GetKey();

            this.forEncryption = forEncryption;

            byte[] key1 = new byte[8];
            Array.Copy(keyMaster, 0, key1, 0, key1.Length);
            workingKey1 = GenerateWorkingKey(forEncryption, key1);

            byte[] key2 = new byte[8];
            Array.Copy(keyMaster, 8, key2, 0, key2.Length);
            workingKey2 = GenerateWorkingKey(!forEncryption, key2);

            if (keyMaster.Length == 24)
            {
                byte[] key3 = new byte[8];
                Array.Copy(keyMaster, 16, key3, 0, key3.Length);
                workingKey3 = GenerateWorkingKey(forEncryption, key3);
            }
            else                    // 16 byte key
            {
                workingKey3 = workingKey1;
            }
        }
Example #43
0
        /**
        * initialise a RC5-32 cipher.
        *
        * @param forEncryption whether or not we are for encryption.
        * @param parameters the parameters required to set up the cipher.
        * @exception ArgumentException if the parameters argument is
        * inappropriate.
        */
        public void Init(
            bool				forEncryption,
            ICipherParameters	parameters)
        {
            //TODO: SUSTITUIDO. CAMBIAMOS EL IsInstanceOfType POR UN BLOQUE try - catch
            //if (typeof(RC5Parameters).IsInstanceOfType(parameters))
            //{
            //    RC5Parameters p = (RC5Parameters)parameters;
            //    _noRounds = p.Rounds;
            //    SetKey(p.GetKey());
            //}
            //else if (typeof(KeyParameter).GetType().IsInstanceOfType(parameters))
            //{
            //    KeyParameter p = (KeyParameter)parameters;
            //    SetKey(p.GetKey());
            //}
            //else
            //{
            //    throw new ArgumentException("invalid parameter passed to RC532 init - " + parameters.GetType().ToString());
            //}
            //this.forEncryption = forEncryption;

            try{
                RC5Parameters p = (RC5Parameters)parameters;
                _noRounds = p.Rounds;
                SetKey(p.GetKey());
            }
            catch (Exception e){
                try
                {
                    KeyParameter p = (KeyParameter)parameters;
                    SetKey(p.GetKey());
                }
                catch (Exception e2)
                {
                    throw new ArgumentException("invalid parameter passed to RC532 init - " + parameters.GetType().ToString()+" e:"+e+" : "+e2);
                }
            }

            this.forEncryption = forEncryption;
        }
        /**
        * initialise a Blowfish cipher.
        *
        * @param forEncryption whether or not we are for encryption.
        * @param parameters the parameters required to set up the cipher.
        * @exception ArgumentException if the parameters argument is
        * inappropriate.
        */
        public void Init(
            bool               forEncryption,
            ICipherParameters  parameters)
        {
            if (!(parameters is KeyParameter))
				throw new ArgumentException("invalid parameter passed to Blowfish init - " + parameters.GetType().ToString());

			this.encrypting = forEncryption;
			this.workingKey = ((KeyParameter)parameters).GetKey();
			SetKey(this.workingKey);
        }
Example #45
0
        /**
         * initialise a RC5-64 cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param parameters the parameters required to set up the cipher.
         * @exception ArgumentException if the parameters argument is
         * inappropriate.
         */
        public void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            if (parameters == null || !typeof(RC5Parameters).GetTypeInfo().IsAssignableFrom(parameters.GetType().GetTypeInfo()))
            {
                throw new ArgumentException("invalid parameter passed to RC564 init - " + parameters.GetType().ToString());
            }

            RC5Parameters p = (RC5Parameters)parameters;

            this.forEncryption = forEncryption;

            _noRounds = p.Rounds;

            SetKey(p.GetKey());
        }
 public virtual void Init(bool forEncryption, ICipherParameters parameters)
 {
     if (parameters is ParametersWithSBox)
     {
         ParametersWithSBox parametersWithSBox = (ParametersWithSBox)parameters;
         byte[]             sBox = parametersWithSBox.GetSBox();
         if (sBox.Length != Gost28147Engine.Sbox_Default.Length)
         {
             throw new ArgumentException("invalid S-box passed to GOST28147 init");
         }
         this.S = Arrays.Clone(sBox);
         if (parametersWithSBox.Parameters != null)
         {
             this.workingKey = this.generateWorkingKey(forEncryption, ((KeyParameter)parametersWithSBox.Parameters).GetKey());
             return;
         }
     }
     else
     {
         if (parameters is KeyParameter)
         {
             this.workingKey = this.generateWorkingKey(forEncryption, ((KeyParameter)parameters).GetKey());
             return;
         }
         if (parameters != null)
         {
             throw new ArgumentException("invalid parameter passed to Gost28147 init - " + parameters.GetType().Name);
         }
     }
 }
Example #47
0
        /**
        * initialise a Rijndael cipher.
        *
        * @param forEncryption whether or not we are for encryption.
        * @param parameters the parameters required to set up the cipher.
        * @exception ArgumentException if the parameters argument is
        * inappropriate.
        */
        public void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            var parameter = parameters as KeyParameter;
            if (parameter != null)
            {
                workingKey = GenerateWorkingKey(parameter.GetKey());
                this.forEncryption = forEncryption;
                return;
            }

            throw new ArgumentException("invalid parameter passed to Rijndael init - " + parameters.GetType().ToString());
        }
Example #48
0
 public virtual void Init(bool forEncryption, ICipherParameters parameters)
 {
     if (!(parameters is KeyParameter))
     {
         throw new ArgumentException("invalid parameter passed to DES init - " + parameters.GetType().ToString());
     }
     workingKey = GenerateWorkingKey(forEncryption, ((KeyParameter)parameters).GetKey());
 }
Example #49
0
 /**
 * initialise a Rijndael cipher.
 *
 * @param forEncryption whether or not we are for encryption.
 * @param parameters the parameters required to set up the cipher.
 * @exception ArgumentException if the parameters argument is
 * inappropriate.
 */
 //TODO: COMENTADO. NO EXISTE EL TIPO "IsInstanceOfType". VAMOS A SUSTITUIRLO POR UN TRY CATCH
 //public void Init(
 //    bool           forEncryption,
 //    ICipherParameters  parameters)
 //{
 //    if (typeof(KeyParameter).IsInstanceOfType(parameters))
 //    {
 //        workingKey = GenerateWorkingKey(((KeyParameter)parameters).GetKey());
 //        this.forEncryption = forEncryption;
 //        return;
 //    }
 //    throw new ArgumentException("invalid parameter passed to Rijndael init - " + parameters.GetType().ToString());
 //}
 public void Init(
     bool forEncryption,
     ICipherParameters parameters)
 {
     try
     {
         workingKey = GenerateWorkingKey(((KeyParameter)parameters).GetKey());
         this.forEncryption = forEncryption;
         return;
     }
     catch (Exception e)
     {
         throw new ArgumentException("invalid parameter passed to Rijndael init - " + parameters.GetType().ToString()+" e:"+e);
     }
 }
Example #50
0
        /**
         * initialise an AES cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param parameters the parameters required to set up the cipher.
         * @exception ArgumentException if the parameters argument is
         * inappropriate.
         */
        public virtual void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            KeyParameter keyParameter = parameters as KeyParameter;

            if (keyParameter == null)
            {
                throw new ArgumentException("invalid parameter passed to AES init - " + parameters.GetType().Name);
            }

            WorkingKey = GenerateWorkingKey(keyParameter.GetKey(), forEncryption);

            this.forEncryption = forEncryption;
        }
Example #51
0
 public virtual void Init(bool forEncryption, ICipherParameters parameters)
 {
     if (!(parameters is KeyParameter))
     {
         throw new ArgumentException("invalid parameter passed to SKIPJACK init - " + parameters.GetType().ToString());
     }
     byte[] key = ((KeyParameter)parameters).GetKey();
     this.encrypting = forEncryption;
     this.key0       = new int[32];
     this.key1       = new int[32];
     this.key2       = new int[32];
     this.key3       = new int[32];
     for (int i = 0; i < 32; i++)
     {
         this.key0[i] = (int)(key[i * 4 % 10] & 255);
         this.key1[i] = (int)(key[(i * 4 + 1) % 10] & 255);
         this.key2[i] = (int)(key[(i * 4 + 2) % 10] & 255);
         this.key3[i] = (int)(key[(i * 4 + 3) % 10] & 255);
     }
 }
Example #52
0
 public virtual void Init(bool forEncryption, ICipherParameters parameters)
 {
     if (typeof(KeyParameter).IsInstanceOfType(parameters))
     {
         workingKey         = GenerateWorkingKey(((KeyParameter)parameters).GetKey());
         this.forEncryption = forEncryption;
         return;
     }
     throw new ArgumentException("invalid parameter passed to Rijndael init - " + parameters.GetType().ToString());
 }
Example #53
0
		/**
		* initialise a Rijndael cipher.
		*
		* @param forEncryption whether or not we are for encryption.
		* @param parameters the parameters required to set up the cipher.
		* @exception ArgumentException if the parameters argument is
		* inappropriate.
		*/
		public void Init(
			bool           forEncryption,
			ICipherParameters  parameters)
		{
			if (typeof(KeyParameter).IsInstanceOfType(parameters))
			{
				workingKey = GenerateWorkingKey(((KeyParameter)parameters).GetKey());
				this.forEncryption = forEncryption;
				return;
			}

			throw new ArgumentException("invalid parameter passed to Rijndael init - " + parameters.GetType().ToString());
		}
Example #54
0
        /**
         * initialise a CAST cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param parameters the parameters required to set up the cipher.
         * @exception ArgumentException if the parameters argument is
         * inappropriate.
         */
        public void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            if (!(parameters is KeyParameter))
            {
                throw new ArgumentException("Invalid parameter passed to " + AlgorithmName + " init - " + parameters.GetType().ToString());
            }

            _encrypting = forEncryption;
            _workingKey = ((KeyParameter)parameters).GetKey();
            SetKey(_workingKey);
        }
Example #55
0
        /**
        * initialise an AES cipher.
        *
        * @param forEncryption whether or not we are for encryption.
        * @param parameters the parameters required to set up the cipher.
        * @exception ArgumentException if the parameters argument is
        * inappropriate.
        */
        public void Init(
            bool				forEncryption,
            ICipherParameters	parameters)
        {
            KeyParameter keyParameter = parameters as KeyParameter;

            if (keyParameter == null)
                throw new ArgumentException("invalid parameter passed to AES init - " + parameters.GetType().Name);

            WorkingKey = GenerateWorkingKey(keyParameter.GetKey(), forEncryption);

            this.forEncryption = forEncryption;
        }
Example #56
0
        /**
         * initialise a SKIPJACK cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param parameters the parameters required to set up the cipher.
         * @exception ArgumentException if the parameters argument is
         * inappropriate.
         */
        public void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            if (!(parameters is KeyParameter))
            {
                throw new ArgumentException("invalid parameter passed to SKIPJACK init - " + parameters.GetType().ToString());
            }

            byte[] keyBytes = ((KeyParameter)parameters).GetKey();

            this.encrypting = forEncryption;
            this.key0       = new int[32];
            this.key1       = new int[32];
            this.key2       = new int[32];
            this.key3       = new int[32];

            //
            // expand the key to 128 bytes in 4 parts (saving us a modulo, multiply
            // and an addition).
            //
            for (int i = 0; i < 32; i++)
            {
                key0[i] = keyBytes[(i * 4) % 10] & 0xff;
                key1[i] = keyBytes[(i * 4 + 1) % 10] & 0xff;
                key2[i] = keyBytes[(i * 4 + 2) % 10] & 0xff;
                key3[i] = keyBytes[(i * 4 + 3) % 10] & 0xff;
            }
        }
Example #57
0
        /**
         * initialise a RC5-32 cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param parameters the parameters required to set up the cipher.
         * @exception ArgumentException if the parameters argument is
         * inappropriate.
         */
        public void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            if (parameters != null && typeof(RC5Parameters).GetTypeInfo().IsAssignableFrom(parameters.GetType().GetTypeInfo()))
            {
                RC5Parameters p = (RC5Parameters)parameters;

                _noRounds = p.Rounds;

                SetKey(p.GetKey());
            }
            else if (parameters != null && typeof(KeyParameter).GetTypeInfo().IsAssignableFrom(parameters.GetType().GetTypeInfo()))
            {
                KeyParameter p = (KeyParameter)parameters;

                SetKey(p.GetKey());
            }
            else
            {
                throw new ArgumentException("invalid parameter passed to RC532 init - " + parameters.GetType().ToString());
            }

            this.forEncryption = forEncryption;
        }
        /**
         * initialise a Rijndael cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param parameters the parameters required to set up the cipher.
         * @exception ArgumentException if the parameters argument is
         * inappropriate.
         */
        public void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            if (parameters != null && typeof(KeyParameter).GetTypeInfo().IsAssignableFrom(parameters.GetType().GetTypeInfo()))
            {
                workingKey         = GenerateWorkingKey(((KeyParameter)parameters).GetKey());
                this.forEncryption = forEncryption;
                return;
            }

            throw new ArgumentException("invalid parameter passed to Rijndael init - " + parameters.GetType().ToString());
        }