/*
         * Constructor, that takes the arguments appropriate for
         * processing the command line directives.
         */
        public DesExample(
            string infile,
            string outfile,
            string keyfile,
            bool encrypt)
        {
            /*
             * First, determine that infile & keyfile exist as appropriate.
             *
             * This will also create the BufferedInputStream as required
             * for reading the input file.  All input files are treated
             * as if they are binary, even if they contain text, it's the
             * bytes that are encrypted.
             */
            this.encrypt = encrypt;
            try
            {
                inStr = File.OpenRead(infile);
            }
            catch (FileNotFoundException)
            {
                //Console.Error.WriteLine("Input file not found [" + infile + "]");
                Environment.Exit(1);
            }

            try
            {
                outStr = File.Create(outfile);
            }
            catch (IOException)
            {
                //Console.Error.WriteLine("Output file not created [" + outfile + "]");
                Environment.Exit(1);
            }

            if (encrypt)
            {
                try
                {
                    /*
                     * The process of creating a new key requires a
                     * number of steps.
                     *
                     * First, create the parameters for the key generator
                     * which are a secure random number generator, and
                     * the length of the key (in bits).
                     */
                    SecureRandom sr = new SecureRandom();

                    KeyGenerationParameters kgp = new KeyGenerationParameters(
                        sr,
                        DesEdeParameters.DesEdeKeyLength * 8);

                    /*
                     * Second, initialise the key generator with the parameters
                     */

                    DesEdeKeyGenerator kg = new DesEdeKeyGenerator();
                    kg.Init(kgp);

                    /*
                     * Third, and finally, generate the key
                     */
                    key = kg.GenerateKey();

                    /*
                     * We can now output the key to the file, but first
                     * hex Encode the key so that we can have a look
                     * at it with a text editor if we so desire
                     */
                    Stream keystream = File.Create(keyfile);
                    byte[] keyhex    = Hex.Encode(key);
                    keystream.Write(keyhex, 0, keyhex.Length);
                    keystream.Flush();
                    keystream.Close();
                }
                catch (IOException)
                {
                    //Console.Error.WriteLine("Could not decryption create key file [" + keyfile + "]");
                    Environment.Exit(1);
                }
            }
            else
            {
                try
                {
                    // TODO This block is a bit dodgy

                    // read the key, and Decode from hex encoding
                    Stream keystream = File.OpenRead(keyfile);
                    //     int len = keystream.available();
                    int    len    = (int)keystream.Length;
                    byte[] keyhex = new byte[len];
                    keystream.Read(keyhex, 0, len);
                    key = Hex.Decode(keyhex);
                }
                catch (IOException)
                {
                    //Console.Error.WriteLine("Decryption key file not found, or not valid [" + keyfile + "]");
                    Environment.Exit(1);
                }
            }
        }
示例#2
0
        public override void PerformTest()
        {
            base.PerformTest();

            byte[] kek1 = Hex.Decode("255e0d1c07b646dfb3134cc843ba8aa71f025b7c0838251f");
            byte[] iv1  = Hex.Decode("5dd4cbfc96f5453b");
            byte[] in1  = Hex.Decode("2923bf85e06dd6ae529149f1f1bae9eab3a7da3d860d3e98");
            byte[] out1 = Hex.Decode("690107618ef092b3b48ca1796b234ae9fa33ebb4159604037db5d6a84eb3aac2768c632775a467d4");

            WrapTest(1, kek1, iv1, in1, out1);

            //
            // key generation
            //
            SecureRandom       random = new SecureRandom();
            DesEdeKeyGenerator keyGen = new DesEdeKeyGenerator();

            keyGen.Init(new KeyGenerationParameters(random, 112));

            byte[] kB = keyGen.GenerateKey();

            if (kB.Length != 16)
            {
                Fail("112 bit key wrong length.");
            }

            keyGen.Init(new KeyGenerationParameters(random, 168));

            kB = keyGen.GenerateKey();

            if (kB.Length != 24)
            {
                Fail("168 bit key wrong length.");
            }

            try
            {
                keyGen.Init(new KeyGenerationParameters(random, 200));

                Fail("invalid key length not detected.");
            }
            catch (ArgumentException)
            {
                // expected
            }

            try
            {
                DesEdeParameters.IsWeakKey(new byte[4], 0);
                Fail("no exception on small key");
            }
            catch (ArgumentException e)
            {
                if (!e.Message.Equals("key material too short."))
                {
                    Fail("wrong exception");
                }
            }

            try
            {
                new DesEdeParameters(weakKey);
                Fail("no exception on weak key");
            }
            catch (ArgumentException e)
            {
                if (!e.Message.Equals("attempt to create weak DESede key"))
                {
                    Fail("wrong exception");
                }
            }
        }