Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var bouncyCastle = new BouncyCastle();
            bouncyCastle.GenerateCertUsingExistigCertificate();

            //bouncyCastle.GenerateCertUsingExistigCertificate();

            var certRequestNet = new CertificateRequestNet();
            certRequestNet.GenerateHierrachyUsingNetClass();

        }
Ejemplo n.º 2
0
    static void Main()
    {
        if (!BouncyCastle.IntegrityCheck())
        {
            MessageBox.Show("BouncyCastle assembly integrity check failed.");
            return;
        }

        try
        {
            WindowMain window = new WindowMain();
            window.WindowStartupLocation = WindowStartupLocation.CenterScreen;

            window.Width      = (int)ConfigFile.RegistryGet("Width", 800);
            window.Height     = (int)ConfigFile.RegistryGet("Height", 600);
            window.FontFamily = new FontFamily((string)ConfigFile.RegistryGet("FontFamily", "Courier New"));
            window.FontSize   = (int)ConfigFile.RegistryGet("FontSize", 12);

            MenuItem menuOptions = Add(window.m_menu.Items, "Options");
            foreach (var family in Fonts.SystemFontFamilies)
            {
                MenuItem item = Add(menuOptions.Items, family);
                item.IsChecked = family.Source == window.FontFamily.Source;
                item.Click    += (sender, e) =>
                {
                    window.FontFamily = family;

                    foreach (MenuItem x in menuOptions.Items)
                    {
                        x.IsChecked = x == item;
                    }
                };
            }
            Add(window.m_menu.Items, " - ").Click += (sender, e) => window.FontSize = Math.Max(8.0, window.FontSize / 1.1);
            Add(window.m_menu.Items, " + ").Click += (sender, e) => window.FontSize *= 1.1;

            MenuItem menuHelp = Add(window.m_menu.Items, "Help");
            Add(menuHelp.Items, "http://minicryptowallet.com/").Click += (sender, e) => System.Diagnostics.Process.Start("http://minicryptowallet.com/");

            new Application().Run(window);

            ConfigFile.RegistrySet("Width", (int)window.Width);
            ConfigFile.RegistrySet("Height", (int)window.Height);
            ConfigFile.RegistrySet("FontFamily", window.FontFamily.Source);
            ConfigFile.RegistrySet("FontSize", (int)window.FontSize);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }
Ejemplo n.º 3
0
    void Output(BigInteger privateKey)
    {
        BigInteger publicKeyX;
        BigInteger publicKeyY;

        BouncyCastle.ECPrivateKeyToPublicKey(privateKey, out publicKeyX, out publicKeyY);

        byte[] publicKey = EllipticCurve.PublicKeyToBytes(publicKeyX, publicKeyY);
        byte[] address   = ETHAddress.PublicKeyToETHAddress(publicKey);

        m_paragraphAddress.Inlines.Add("Private key (confidential): 0x" + Encode.BytesToHex(Encode.BigIntegerToBytes(privateKey, 32)) + "\n");
        m_paragraphAddress.Inlines.Add("Public key: 0x" + Encode.BytesToHex(publicKey) + "\n");
        m_paragraphAddress.Inlines.Add("ETH address: 0x" + Encode.BytesToHex(address) + "\n");
        m_paragraphAddress.Inlines.Add("ETC address: 0x" + Encode.BytesToHex(address) + "\n");
    }
Ejemplo n.º 4
0
    public void ECDsaSign(string currency, BigInteger privateKey)
    {
        int chainId = GetChainId(currency);

        m_v = new BigInteger(chainId);
        m_r = new BigInteger(0);
        m_s = new BigInteger(0);

        byte[] e   = BouncyCastle.Keccak(EncodeRLP());
        byte[] sig = EllipticCurve.ECDsaSignHash(privateKey, e);

        BigInteger publicKeyX;
        BigInteger publicKeyY;

        BouncyCastle.ECPrivateKeyToPublicKey(privateKey, out publicKeyX, out publicKeyY);
        int recoveryId = BouncyCastle.ECCalcRecoveryId(sig.Take(32).ToArray(), sig.Skip(32).ToArray(), e, publicKeyX, publicKeyY);

        m_v = new BigInteger(chainId * 2 + 35 + recoveryId);
        m_r = Encode.BytesToBigInteger(sig.Take(32).ToArray());
        m_s = Encode.BytesToBigInteger(sig.Skip(32).ToArray());
    }
    static void Main(string[] args)
    {
        int passwordStrengthBit = 128;          // value less than 64 is strongly not recommended

        if (args.Length != 2)
        {
            Console.WriteLine("usage: charset number");
            return;
        }

        string charset = Password.GetCharset(args[0]);

        if (charset.Length == 0)
        {
            Console.WriteLine("Invalid charset.");
            return;
        }

        ulong number;

        if (!ulong.TryParse(args[1], out number))
        {
            Console.WriteLine("Invalid number.");
            return;
        }

        for (ulong i = 0; i < number; i++)
        {
            BigInteger publicKeyX;
            BigInteger publicKeyY;

            string     password   = Password.GeneratePassword(charset, BigInteger.Pow(2, passwordStrengthBit));
            BigInteger privateKey = Password.PasswordToPrivateKey(password);
            BouncyCastle.ECPrivateKeyToPublicKey(privateKey, out publicKeyX, out publicKeyY);
            byte[] publicKey = EllipticCurve.PublicKeyToBytes(publicKeyX, publicKeyY);
            byte[] address   = ETHAddress.PublicKeyToETHAddress(publicKey);

            Console.WriteLine("0x" + Encode.BytesToHex(address) + " " + password);
        }
    }
    void UpdateTransaction(bool loadNonce)
    {
        m_paragraphTransaction.Inlines.Clear();

        // Password / Private key
        BigInteger privateKey;

        if (EllipticCurve.HexToPrivateKey(m_passwordBoxPP.Password, out privateKey))
        {
            m_passwordBoxPP.Background = Brushes.LightSkyBlue;
        }
        else
        {
            m_passwordBoxPP.Background = Brushes.White;

            if (Password.GetKeySpace(m_passwordBoxPP.Password) < BigInteger.Pow(2, m_passwordStrengthBit))
            {
                return;
            }

            privateKey = Password.PasswordToPrivateKey(m_passwordBoxPP.Password);
        }

        BigInteger publicKeyX;
        BigInteger publicKeyY;

        BouncyCastle.ECPrivateKeyToPublicKey(privateKey, out publicKeyX, out publicKeyY);

        byte[] publicKey = EllipticCurve.PublicKeyToBytes(publicKeyX, publicKeyY);
        byte[] address   = ETHAddress.PublicKeyToETHAddress(publicKey);

        PrintAddressAndBlockExplorer("From", address);

        // Nonce
        if (loadNonce)
        {
            string text = "";
            if (m_dAddressNonce.ContainsKey(Encode.BytesToHex(address)))
            {
                text = m_dAddressNonce[Encode.BytesToHex(address)];
            }

            Dispatcher.BeginInvoke(new Action(() => m_comboBoxNonce.Text = text));
        }

        if (!BigInteger.TryParse(m_comboBoxNonce.Text, out m_tx.m_nonce))
        {
            m_paragraphTransaction.Inlines.Add("Nonce: <Invalid>\n"); return;
        }
        if (m_tx.m_nonce < 0)
        {
            m_paragraphTransaction.Inlines.Add("Nonce: <Invalid>\n"); return;
        }

        m_paragraphTransaction.Inlines.Add("Nonce: " + m_tx.m_nonce + "\n");

        // To
        string to    = m_comboBoxTo.Text.Trim();
        int    index = to.IndexOf(" ");

        if (index != -1)
        {
            to = to.Substring(0, index);
        }
        m_tx.m_to = Encode.HexToBytes(to);

        if (m_tx.m_to == null)
        {
            m_paragraphTransaction.Inlines.Add("To: <Invalid>\n");       return;
        }
        else if (m_tx.m_to.Length == 0)
        {
            m_paragraphTransaction.Inlines.Add("To: Empty\n");
        }
        else if (m_tx.m_to.Length == 20)
        {
            PrintAddressAndBlockExplorer("To", m_tx.m_to);
        }
        else
        {
            m_paragraphTransaction.Inlines.Add("To: <Invalid>\n");       return;
        }

        // Value
        if (Encode.DecimalToBigInteger(m_comboBoxValue.Text, 18, out m_tx.m_value))
        {
            m_paragraphTransaction.Inlines.Add("Value: " + Encode.BigIntegerToDecimal(m_tx.m_value, 18) + " " + GetCurrency() + "\n");
        }
        else
        {
            m_paragraphTransaction.Inlines.Add("Value: <Invalid>\n");
            return;
        }

        // Init / Data
        if (m_tx.m_initOrData.Length > 0)
        {
            m_paragraphTransaction.Inlines.Add("Init / Data: " + m_tx.m_initOrData.Length + " bytes\n");
        }

        // Gas price
        if (!Encode.DecimalToBigInteger(m_comboBoxGasPrice.Text, 18, out m_tx.m_gasPrice))
        {
            m_paragraphTransaction.Inlines.Add("Gas price: <Invalid>\n");
            return;
        }

//		m_paragraphTransaction.Inlines.Add("Gas price: " + Encode.BigIntegerToDecimal(m_tx.m_gasPrice, 18) + " " + GetCurrency() + "\n");

        // Gas limit
        if (!BigInteger.TryParse(m_comboBoxGasLimit.Text, out m_tx.m_gasLimit))
        {
            m_paragraphTransaction.Inlines.Add("Gas limit: <Invalid>\n");   return;
        }
        if (m_tx.m_gasLimit < 0)
        {
            m_paragraphTransaction.Inlines.Add("Gas limit: <Invalid>\n");   return;
        }

//		m_paragraphTransaction.Inlines.Add("Gas limit: " + m_tx.m_gasLimit + "\n");

        BigInteger gasLimitMin = m_tx.GetGasLimitMin();

        if (m_tx.m_gasLimit < gasLimitMin)
        {
            m_paragraphTransaction.Inlines.Add(new Run("Minimal gas limit is " + gasLimitMin + ".\n")
            {
                Foreground = Brushes.Red
            });
        }

        // calculation
        m_paragraphTransaction.Inlines.Add(new Run("Fee: " + Encode.BigIntegerToDecimal(m_tx.m_gasPrice * m_tx.m_gasLimit, 18) + " " + GetCurrency() + "\n")
        {
            ToolTip = "Fee = Gas price * Gas limit"
        });
        m_paragraphTransaction.Inlines.Add(new Run("Value + Fee: " + Encode.BigIntegerToDecimal(m_tx.m_value + m_tx.m_gasPrice * m_tx.m_gasLimit, 18) + " " + GetCurrency() + "\n")
        {
            ToolTip = "Fee = Gas price * Gas limit"
        });

        // additional check
        if (m_tx.m_to.Length == 0 && m_tx.m_initOrData.Length == 0)
        {
            return;
        }

        // tx
        m_tx.ECDsaSign(GetCurrency(), privateKey);
        byte[] tx   = m_tx.EncodeRLP();
        byte[] hash = BouncyCastle.Keccak(tx);

        // button
        foreach (string node in m_lNode)
        {
            Button buttonSend = new Button()
            {
                Margin = new Thickness(2), Content = "Send to " + node, ToolTip = "Send raw transaction to " + node
            };
            buttonSend.Click += (sender, e) =>
            {
                // save
                string file = GetCurrency() + "_tx_" + DateTime.Now.ToString("yyMMdd_HHmmss") + "_0x" + Encode.BytesToHex(hash);
                File.WriteAllBytes(file, tx);

                // nonce read
                m_dAddressNonce.Clear();

                foreach (var entry in ConfigFile.ReadAddressInfo(GetCurrency() + "_nonce"))
                {
                    m_dAddressNonce[entry.Item1] = entry.Item2;
                }

                // nonce update
                m_dAddressNonce[Encode.BytesToHex(address)] = m_tx.m_nonce.ToString();

                // nonce write
                List <string> lLine = new List <string>();
                foreach (var entry in m_dAddressNonce)
                {
                    lLine.Add("0x" + entry.Key + " " + entry.Value);
                }
                File.WriteAllLines(GetCurrency() + "_nonce", lLine);

                // send
                System.Diagnostics.Process.Start("send_transaction.exe", file + " " + node + " -pause");
            };
            m_paragraphTransaction.Inlines.Add(buttonSend);
        }

        Button buttonCopy = new Button()
        {
            Margin = new Thickness(2), Content = "Copy", ToolTip = "Copy raw transaction to clipboard"
        };
        Button buttonSave = new Button()
        {
            Margin = new Thickness(2), Content = "Save", ToolTip = "Save raw transaction to file"
        };

        buttonCopy.Click += (sender, e) => Clipboard.SetText(Encode.BytesToHex(tx));
        buttonSave.Click += (sender, e) =>
        {
            var dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = GetCurrency() + "_tx_0x" + Encode.BytesToHex(hash);
            if (dlg.ShowDialog() == true)
            {
                File.WriteAllBytes(dlg.FileName, tx);
            }
        };

        m_paragraphTransaction.Inlines.Add(buttonCopy);
        m_paragraphTransaction.Inlines.Add(buttonSave);
    }
 public static byte[] PublicKeyToETHAddress(byte[] publicKey)
 {
     return(BouncyCastle.Keccak(publicKey).Skip(12).ToArray());
 }
Ejemplo n.º 8
0
        private BouncyCastle.FpFieldElement GetX(
            byte[] input, 
            BouncyCastle.FpCurve curve, 
            int index, 
            int counter)
        {
            int numIterations = (int)System.Math.Ceiling(
                (double)(curve.Q.BitLength / 8) / (double)hashByteSize);

            byte[] digest = new byte[numIterations * hashByteSize];

            for (int iteration = 0; iteration < numIterations; iteration++)
            {
                byte[] hashInput = ProtocolHelper.Concatenate(
                    input, 
                    encoding.GetBytes(
                        index.ToString() + counter.ToString() + iteration.ToString()));
                hash.HashWithoutFormatting(hashInput);
                Array.Copy(hash.Digest, 0, digest, hashByteSize * iteration, hashByteSize);
            }

            BCBigInt x = new BCBigInt(1, digest).Mod(curve.Q);
            return curve.FromBigInteger(x) as BouncyCastle.FpFieldElement;
        }