示例#1
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 (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;
        }
示例#2
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 - " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters));
            }

            this.forEncryption = forEncryption;
        }
示例#3
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)
        {
            //TODO: SUSTITUIDO. CAMBIAMOS EL IsInstanceOfType POR UN BLOQUE try - catch
            //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());

            RC5Parameters p = null;

            try
            {
                p = (RC5Parameters)parameters;
            }
            catch (Exception e)
            {
                throw new ArgumentException("invalid parameter passed to RC564 init - " + parameters.GetType().ToString() + " e: " + e);
            }

            this.forEncryption = forEncryption;

            _noRounds = p.Rounds;

            SetKey(p.GetKey());
        }
示例#4
0
        public virtual void Init(bool forEncryption, ICipherParameters parameters)
        {
            if (!typeof(RC5Parameters).IsInstanceOfType(parameters))
            {
                throw new ArgumentException("invalid parameter passed to RC564 init - " + parameters.GetType().ToString());
            }
            RC5Parameters rC5Parameters = (RC5Parameters)parameters;

            this.forEncryption = forEncryption;
            _noRounds          = rC5Parameters.Rounds;
            SetKey(rC5Parameters.GetKey());
        }
示例#5
0
        public virtual void Init(bool forEncryption, ICipherParameters parameters)
        {
            if (!typeof(RC5Parameters).IsInstanceOfType(parameters))
            {
                throw new ArgumentException("invalid parameter passed to RC564 init - " + Platform.GetTypeName(parameters));
            }
            RC5Parameters parameters2 = (RC5Parameters)parameters;

            this.forEncryption = forEncryption;
            this._noRounds     = parameters2.Rounds;
            this.SetKey(parameters2.GetKey());
        }
示例#6
0
        public virtual void Init(bool forEncryption, ICipherParameters parameters)
        {
            //IL_0022: Unknown result type (might be due to invalid IL or missing references)
            if (!typeof(RC5Parameters).IsInstanceOfType((object)parameters))
            {
                throw new ArgumentException("invalid parameter passed to RC564 init - " + Platform.GetTypeName(parameters));
            }
            RC5Parameters rC5Parameters = (RC5Parameters)parameters;

            this.forEncryption = forEncryption;
            _noRounds          = rC5Parameters.Rounds;
            SetKey(rC5Parameters.GetKey());
        }
示例#7
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 virtual void Init(
            bool forEncryption,
            ICipherParameters parameters)
        {
            if (!(typeof(RC5Parameters).IsInstanceOfType(parameters)))
            {
                throw new ArgumentException("invalid parameter passed to RC564 init - " + Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters));
            }

            RC5Parameters p = (RC5Parameters)parameters;

            this.forEncryption = forEncryption;

            _noRounds = p.Rounds;

            SetKey(p.GetKey());
        }
示例#8
0
        /**
         * initialise a RC5-64 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)))
            {
                throw new ArgumentException("invalid parameter passed to RC564 init - " + parameters.GetType().ToString());
            }

            RC5Parameters p = (RC5Parameters)parameters;

            this.forEncryption = forEncryption;

            _noRounds = p.getRounds();

            setKey(p.getKey());
        }
示例#9
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());
        }
示例#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 == 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());
        }
示例#11
0
        private void DoTestExceptions()
        {
            IMac mac = MacUtilities.GetMac("HmacSHA1");

            byte [] b = { (byte)1, (byte)2, (byte)3, (byte)4, (byte)5 };
//			KeyParameter sks = new KeyParameter(b); //, "HmacSHA1");
//			RC5ParameterSpec algPS = new RC5ParameterSpec(100, 100, 100);
            RC5Parameters rc5Parameters = new RC5Parameters(b, 100);

            try
            {
//				mac.Init(sks, algPS);
                mac.Init(rc5Parameters);
            }
//			catch (InvalidAlgorithmParameterException e)
            catch (Exception)
            {
                // ignore okay
            }

            try
            {
                mac.Init(null); //, null);
            }
//			catch (InvalidKeyException)
//			{
//				// ignore okay
//			}
//			catch (InvalidAlgorithmParameterException e)
            catch (Exception)
            {
                // ignore okay
            }

//			try
//			{
//				mac.Init(null);
//			}
//			catch (InvalidKeyException)
//			{
//				// ignore okay
//			}
        }
 public virtual void Init(bool forEncryption, ICipherParameters parameters)
 {
     if (typeof(RC5Parameters).IsInstanceOfType(parameters))
     {
         RC5Parameters rC5Parameters = (RC5Parameters)parameters;
         _noRounds = rC5Parameters.Rounds;
         SetKey(rC5Parameters.GetKey());
     }
     else
     {
         if (!typeof(KeyParameter).IsInstanceOfType(parameters))
         {
             throw new ArgumentException("invalid parameter passed to RC532 init - " + Platform.GetTypeName(parameters));
         }
         KeyParameter keyParameter = (KeyParameter)parameters;
         SetKey(keyParameter.GetKey());
     }
     this.forEncryption = forEncryption;
 }
示例#13
0
        private void doTest(
            string algorithm,
            byte[]      input,
            byte[]      output)
        {
            KeyParameter       key = null;
            CipherKeyGenerator keyGen;
            SecureRandom       rand;
            IBufferedCipher    inCipher = null, outCipher = null;

            byte[]       iv = null;
            CipherStream cIn, cOut;
            MemoryStream bIn, bOut;

            rand = new FixedSecureRandom();

            string[] parts = algorithm.ToUpper(CultureInfo.InvariantCulture).Split('/');
            string   baseAlgorithm = parts[0];
            string   mode  = parts.Length > 1 ? parts[1] : null;

#if !INCLUDE_IDEA
            if (baseAlgorithm.Equals("IDEA"))
            {
                return;
            }
#endif

            try
            {
                keyGen = GeneratorUtilities.GetKeyGenerator(baseAlgorithm);

                // TODO Add Algorithm property to CipherKeyGenerator?
//				if (!keyGen.getAlgorithm().Equals(baseAlgorithm))
//				{
//					Fail("wrong key generator returned!");
//				}

                // TODO Add new Init method to CipherKeyGenerator?
//				keyGen.Init(rand);
                keyGen.Init(new KeyGenerationParameters(rand, keyGen.DefaultStrength));

                byte[] keyBytes = keyGen.GenerateKey();

                if (algorithm.StartsWith("RC5"))
                {
                    key = new RC5Parameters(keyBytes, rc5Rounds);
                }
                else
                {
                    key = ParameterUtilities.CreateKeyParameter(baseAlgorithm, keyBytes);
                }

                inCipher  = CipherUtilities.GetCipher(algorithm);
                outCipher = CipherUtilities.GetCipher(algorithm);

                if (!inCipher.AlgorithmName.ToUpper(CultureInfo.InvariantCulture).StartsWith(baseAlgorithm))
                {
                    Fail("wrong cipher returned!");
                }

                ICipherParameters parameters = key;

                int ivLength = GetIVLength(algorithm);

                if (ivLength > 0)
                {
                    if (baseAlgorithm == "RC2")
                    {
                        iv = rc2IV;
                    }
                    else if (baseAlgorithm == "RC5")
                    {
                        iv = rc5IV;
                    }
                    else if (baseAlgorithm == "RC5-64")
                    {
                        iv = rc564IV;
                    }
                    else
                    {
                        // NB: rand always generates same values each test run
                        iv = rand.GenerateSeed(ivLength);
                    }

                    parameters = new ParametersWithIV(key, iv);
                }

                // NB: 'rand' still needed e.g. for some paddings
                parameters = new ParametersWithRandom(parameters, rand);

                outCipher.Init(true, parameters);
            }
            catch (Exception e)
            {
                Fail("" + algorithm + " failed initialisation - " + e.ToString(), e);
            }

            //
            // grab the iv if there is one
            //
            try
            {
                // The Java version set this implicitly, but we set it explicity
                //byte[] iv = outCipher.getIV();

                if (iv != null)
                {
                    // TODO Examine short IV handling for these FIPS-compliant modes in Java build
                    if (mode.StartsWith("CFB") ||
                        mode.StartsWith("GOFB") ||
                        mode.StartsWith("OFB") ||
                        mode.StartsWith("OPENPGPCFB"))
                    {
                        // These modes automatically pad out the IV if it is short
                    }
                    else
                    {
                        try
                        {
                            byte[] nIv = new byte[iv.Length - 1];
                            inCipher.Init(false, new ParametersWithIV(key, nIv));
                            Fail("failed to pick up short IV");
                        }
                        //catch (InvalidAlgorithmParameterException e)
                        catch (ArgumentException)
                        {
                            // ignore - this is what we want...
                        }
                    }

                    //IvParameterSpec spec = new IvParameterSpec(iv);
                    inCipher.Init(false, new ParametersWithIV(key, iv));
                }
                else
                {
                    inCipher.Init(false, key);
                }
            }
            catch (Exception e)
            {
                Fail("" + algorithm + " failed initialisation - " + e.ToString());
            }

            //
            // encryption pass
            //
            bOut = new MemoryStream();
            cOut = new CipherStream(bOut, null, outCipher);

            try
            {
                for (int i = 0; i != input.Length / 2; i++)
                {
                    cOut.WriteByte(input[i]);
                }
                cOut.Write(input, input.Length / 2, input.Length - input.Length / 2);
                cOut.Close();
            }
            catch (IOException e)
            {
                Fail("" + algorithm + " failed encryption - " + e.ToString());
            }

            byte[] bytes = bOut.ToArray();

            if (!AreEqual(bytes, output))
            {
                Fail("" + algorithm + " failed encryption - expected "
                     + Hex.ToHexString(output) + " got "
                     + Hex.ToHexString(bytes));
            }

            //
            // decryption pass
            //
            bIn = new MemoryStream(bytes, false);
            cIn = new CipherStream(bIn, inCipher, null);

            try
            {
                BinaryReader dIn = new BinaryReader(cIn);

                bytes = new byte[input.Length];

                for (int i = 0; i != input.Length / 2; i++)
                {
                    bytes[i] = dIn.ReadByte();
                }

                int    remaining = bytes.Length - input.Length / 2;
                byte[] extra     = dIn.ReadBytes(remaining);
                if (extra.Length < remaining)
                {
                    throw new EndOfStreamException();
                }
                extra.CopyTo(bytes, input.Length / 2);
            }
            catch (Exception e)
            {
                Fail("" + algorithm + " failed decryption - " + e.ToString());
            }

            if (!AreEqual(bytes, input))
            {
                Fail("" + algorithm + " failed decryption - expected "
                     + Hex.ToHexString(input) + " got "
                     + Hex.ToHexString(bytes));
            }
        }