Пример #1
0
        /**
         * initialise an IDEA 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,
            CipherParameters parameters)
        {
            if (typeof(KeyParameter).IsInstanceOfType(parameters))
            {
                workingKey = generateWorkingKey(forEncryption,
                                                ((KeyParameter)parameters).getKey());
                return;
            }

            throw new ArgumentException("invalid parameter passed to IDEA init - " + parameters.GetType().ToString());
        }
Пример #2
0
        /**
         * initialise a RC4 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,
            CipherParameters parameters
            )
        {
            if (typeof(KeyParameter).IsInstanceOfType(parameters))
            {
                /*
                 * 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());
        }
Пример #3
0
        /**
         * initialise a Twofish 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 encrypting,
            CipherParameters parameters)
        {
            if (typeof(KeyParameter).IsInstanceOfType(parameters))
            {
                this.encrypting = encrypting;
                this.workingKey = ((KeyParameter)parameters).getKey();
                this.k64Cnt     = (this.workingKey.Length / 8);             // pre-padded ?
                setKey(this.workingKey);

                return;
            }

            throw new ArgumentException("invalid parameter passed to Twofish init - " + parameters.GetType().ToString());
        }
Пример #4
0
        /**
         * initialise a RC2 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 encrypting,
            CipherParameters parameters)
        {
            this.encrypting = encrypting;

            if (typeof(RC2Parameters).IsInstanceOfType(parameters))
            {
                RC2Parameters param = (RC2Parameters)parameters;

                workingKey = generateWorkingKey(param.getKey(),
                                                param.getEffectiveKeyBits());
            }
            else if (typeof(KeyParameter).IsInstanceOfType(parameters))
            {
                byte[] key = ((KeyParameter)parameters).getKey();

                workingKey = generateWorkingKey(key, key.Length * 8);
            }
            else
            {
                throw new ArgumentException("invalid parameter passed to RC2 init - " + parameters.GetType().ToString());
            }
        }
Пример #5
0
        /**
         * initialise a SKIPJACK 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 encrypting,
            CipherParameters parameters)
        {
            if (!(typeof(KeyParameter).IsInstanceOfType(parameters)))
            {
                throw new ArgumentException("invalid parameter passed to SKIPJACK init - " + parameters.GetType().ToString());
            }

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

            this.encrypting = encrypting;
            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;
            }
        }
Пример #6
0
        /**
         * initialise a RC5-32 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,
            CipherParameters parameters)
        {
            if (typeof(RC5Parameters).IsInstanceOfType(parameters))
            {
                RC5Parameters p = (RC5Parameters)parameters;

                _noRounds = p.getRounds();

                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;
        }
Пример #7
0
        private int X0, X1, X2, X3;                             // registers

        /**
         * initialise a Serpent 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 encrypting,
            CipherParameters parameters)
        {
            if (typeof(KeyParameter).IsInstanceOfType(parameters))
            {
                this.encrypting = encrypting;
                this.wKey       = makeWorkingKey(((KeyParameter)parameters).getKey());
                return;
            }

            throw new ArgumentException("invalid parameter passed to Serpent init - " + parameters.GetType().ToString());
        }
Пример #8
0
        /**
         * initialise a DESede cipher.
         *
         * @param forEncryption whether or not we are for encryption.
         * @param params the parameters required to set up the cipher.
         * @exception IllegalArgumentException if the params argument is
         * inappropriate.
         */
        public override void init(
            bool encrypting,
            CipherParameters parameters)
        {
            if (!(typeof(KeyParameter).IsInstanceOfType(parameters)))
            {
                throw new ArgumentException("invalid parameter passed to DESede init - " + parameters.GetType().ToString());
            }

            byte[] keyMaster = ((KeyParameter)parameters).getKey();
            byte[] key1      = new byte[8], key2 = new byte[8], key3 = new byte[8];

            this.forEncryption = encrypting;

            if (keyMaster.Length == 24)
            {
                Array.Copy(keyMaster, 0, key1, 0, key1.Length);
                Array.Copy(keyMaster, 8, key2, 0, key2.Length);
                Array.Copy(keyMaster, 16, key3, 0, key3.Length);

                workingKey1 = generateWorkingKey(encrypting, key1);
                workingKey2 = generateWorkingKey(!encrypting, key2);
                workingKey3 = generateWorkingKey(encrypting, key3);
            }
            else                    // 16 byte key
            {
                Array.Copy(keyMaster, 0, key1, 0, key1.Length);
                Array.Copy(keyMaster, 8, key2, 0, key2.Length);

                workingKey1 = generateWorkingKey(encrypting, key1);
                workingKey2 = generateWorkingKey(!encrypting, key2);
                workingKey3 = workingKey1;
            }
        }
Пример #9
0
        /**
         * initialise a RC5-32 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,
            CipherParameters parameters)
        {
            if (!(typeof(KeyParameter).IsInstanceOfType(parameters)))
            {
                throw new ArgumentException("invalid parameter passed to RC6 init - " + parameters.GetType().ToString());
            }

            KeyParameter p = (KeyParameter)parameters;

            this.forEncryption = forEncryption;
            setKey(p.getKey());
        }