public void Bug3017248()
        {
            CryptoKey key    = new CryptoKey(new DSA(true));
            BIO       output = BIO.MemoryBuffer();

            key.WritePrivateKey(output, Cipher.Null, "password");
            output.SetClose(BIO.CloseOption.Close);
            Console.WriteLine(output.ReadString());
        }
Example #2
0
        public void Execute(string[] args)
        {
            try
            {
                options.ParseArguments(args);
            }
            catch (Exception)
            {
                Usage();
                return;
            }

            if (options.IsSet("pubin") && options.IsSet("check"))
            {
                Console.Error.WriteLine("Only private keys can be checked");
                return;
            }

            BIO bin = Program.GetInFile(options.GetString("in"));

            RSA rsa;

            if (options.IsSet("pubin"))
            {
                rsa = RSA.FromPublicKey(bin, Program.OnPassword, this.options["passin"]);
            }
            else
            {
                rsa = RSA.FromPrivateKey(bin, Program.OnPassword, this.options["passin"]);
            }

            Cipher enc = null;

            if (options.IsSet("des"))
            {
                enc = Cipher.DES_CBC;
            }
            else if (options.IsSet("des3"))
            {
                enc = Cipher.DES_EDE3_CBC;
            }
            else if (options.IsSet("aes128"))
            {
                enc = Cipher.AES_128_CBC;
            }
            else if (options.IsSet("aes192"))
            {
                enc = Cipher.AES_192_CBC;
            }
            else if (options.IsSet("aes256"))
            {
                enc = Cipher.AES_256_CBC;
            }

            if (options.IsSet("text"))
            {
                Console.Write(rsa);
            }

            if (options.IsSet("modulus"))
            {
                Console.WriteLine("Modulus={0}", rsa.PublicModulus);
            }

            if (options.IsSet("check"))
            {
                if (rsa.Check())
                {
                    Console.WriteLine("RSA key ok");
                }
                else
                {
                    Console.WriteLine("RSA key error");
                }
            }

            if (!options.IsSet("noout"))
            {
                Console.Error.WriteLine("writing RSA key");
                using (BIO bio = BIO.MemoryBuffer())
                {
                    if (this.options.IsSet("pubout"))
                    {
                        rsa.WritePublicKey(bio);
                    }
                    else
                    {
                        rsa.WritePrivateKey(bio, enc, Program.OnPassword, this.options["passout"]);
                    }

                    string outfile = this.options["out"] as string;
                    if (string.IsNullOrEmpty(outfile))
                    {
                        Console.WriteLine(bio.ReadString());
                    }
                    else
                    {
                        File.WriteAllText(outfile, bio.ReadString());
                    }
                }
            }
        }
Example #3
0
        public void Execute(string[] args)
        {
            try
            {
                options.ParseArguments(args);
            }
            catch (Exception)
            {
                Usage();
                return;
            }

            int bits = 512;

            if (this.options.Arguments.Count == 1)
            {
                bits = Convert.ToInt32(this.options.Arguments[0]);
            }

            BigNumber e = null;

            if (options.IsSet("3"))
            {
                e = 3;
            }
            else if (options.IsSet("f4"))
            {
                e = 0x10001;
            }

            Console.Error.WriteLine("Generating RSA private key, {0} bit long modulus", bits);

            RSA rsa = new RSA();

            rsa.GenerateKeys(bits, e, Program.OnGenerator, null);

            Console.Error.WriteLine("e is {0} (0x{1})", e.ToDecimalString(), e.ToHexString());

            Cipher enc = null;

            if (options.IsSet("des"))
            {
                enc = Cipher.DES_CBC;
            }
            else if (options.IsSet("des3"))
            {
                enc = Cipher.DES_EDE3_CBC;
            }
            else if (options.IsSet("idea"))
            {
                enc = Cipher.Idea_CBC;
            }
            else if (options.IsSet("aes128"))
            {
                enc = Cipher.AES_128_CBC;
            }
            else if (options.IsSet("aes192"))
            {
                enc = Cipher.AES_192_CBC;
            }
            else if (options.IsSet("aes256"))
            {
                enc = Cipher.AES_256_CBC;
            }

            using (BIO bio = BIO.MemoryBuffer())
            {
                rsa.WritePrivateKey(bio, enc, Program.OnPassword, this.options["passout"]);

                string outfile = this.options["out"] as string;
                if (string.IsNullOrEmpty(outfile))
                {
                    Console.WriteLine(bio.ReadString());
                }
                else
                {
                    File.WriteAllText(outfile, bio.ReadString());
                }
            }
        }