public static void Execute(BitcoinSecret privateKey)
        {
            // PRNG (Pseudo-Random-Number-Generator)
            // add entropy to the PRNG output that NBitcoin is using
            RandomUtils.AddEntropy("Hello");
            RandomUtils.AddEntropy(new byte[] { 1, 2, 3 });
            Key nsaProofKey = new Key();

            // KDF (Key Derivation Function)
            byte[] derived = SCrypt.BitcoinComputeDerivedKey("Hello", new byte[] { 1, 2, 3 });
            RandomUtils.AddEntropy(derived);

            string password = "******";
            BitcoinEncryptedSecret encryptedBitcoinPrivateKey = privateKey.Encrypt(password);
            BitcoinSecret          decryptedBitcoinPrivateKey = encryptedBitcoinPrivateKey.GetSecret(password);

            Console.WriteLine("-------------------Key Derivation Function------------------");
            Console.WriteLine("BitcoinPrivateKey : " + privateKey);
            Console.WriteLine("BitcoinPrivateKey(encrypted) : " + encryptedBitcoinPrivateKey);
            Console.WriteLine("BitcoinPrivateKey(decrypted) : " + decryptedBitcoinPrivateKey);

            // BIP38 (Part 2)
            string mySecret = "Secret Phrase";
            BitcoinPassphraseCode passphraseCode     = new BitcoinPassphraseCode(mySecret, Network.Main, null);
            EncryptedKeyResult    encryptedKeyResult = passphraseCode.GenerateEncryptedSecret();
            var generatedAddress  = encryptedKeyResult.GeneratedAddress;
            var encryptedKey      = encryptedKeyResult.EncryptedKey;
            var confirmationCode  = encryptedKeyResult.ConfirmationCode;
            var bitcoinPrivateKey = encryptedKey.GetSecret(mySecret);

            Console.WriteLine("---------------------BIP38 (Part 2)---------------------");
            Console.WriteLine("check generatedAddress: " + confirmationCode.Check(mySecret, generatedAddress));   // True
            Console.WriteLine("check generatedAddress: " + (bitcoinPrivateKey.GetAddress() == generatedAddress)); // True
            Console.WriteLine("bitcoinPrivateKey: " + bitcoinPrivateKey);
        }
Esempio n. 2
0
        private void Code8()
        {
            var           key = new Key();
            BitcoinSecret wif = key.GetBitcoinSecret(Network.Main);

            Console.WriteLine(wif);
            BitcoinEncryptedSecret encrypted = wif.Encrypt("secret");

            Console.WriteLine(encrypted);
            wif = encrypted.GetSecret("secret");
            Console.WriteLine(wif);
        }
Esempio n. 3
0
        private static void Main(string[] args)
        {
            // Private keys are often represented in Base58Check called a Bitcoin Secret
            // (also known as Wallet Import Format or simply WIF), like Bitcoin Addresses.
            var privateKey = new Key();

            /*
             * Note:
             *  it is easy to go from BitcoinSecret to private Key, however
             *  it is impossible to go from a Bitcoin Address to Public Key
             *  because the Bitcoin Address contains a hash of the Public Key, not the Public Key itself.
             */

            // pass in enum value: Main/TestNet, for use on respective networks
            BitcoinSecret createdWithGetWif           = privateKey.GetWif(Network.Main); // method has same body of .GetBitcoinSecret, returns same result
            BitcoinSecret createdWithGetBitcoinSecret = privateKey.GetBitcoinSecret(Network.Main);

            var wifIsBitcoinSecret = createdWithGetWif == createdWithGetBitcoinSecret;

            Console.WriteLine(wifIsBitcoinSecret); // true

            PubKey publicKey = privateKey.PubKey;
            BitcoinPubKeyAddress bitcoinPubKey = publicKey.GetAddress(Network.Main);

            Console.WriteLine(publicKey);
            Console.WriteLine(bitcoinPubKey);

            BitcoinEncryptedSecret encryptedBitcoinPrivateKey = createdWithGetWif.Encrypt("ilovecrypto");

            Console.WriteLine(encryptedBitcoinPrivateKey);

            BitcoinSecret secret = encryptedBitcoinPrivateKey.GetSecret("ilovecrypto");

            Console.WriteLine(secret);

            Console.WriteLine(secret == createdWithGetWif);

            // keep the console open to read
            Console.ReadLine();
        }
Esempio n. 4
0
        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxPassword.Text))
            {
                MessageBox.Show(this, "You must enter a password and a Bitcoin Secret or Encrypted Key.", "Knoledge-keychain", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (string.IsNullOrEmpty(textBoxEncrypted.Text) && string.IsNullOrEmpty(textBoxSecret.Text))
            {
                MessageBox.Show(this, "You must enter a Bitcoin Secret or Encrypted Key.", "Knoledge-keychain", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (string.IsNullOrEmpty(textBoxEncrypted.Text))
            {
                using (new HourGlass())
                {
                    _result = Util.Interpret(textBoxSecret.Text);
                }

                if (_result == null || !(_result is BitcoinSecret))
                {
                    MessageBox.Show(this, "Unrecognized or invalid key.", "Knoledge-keychain", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                using (new HourGlass())
                {
                    try
                    {
                        BitcoinSecret          secret    = _result as BitcoinSecret;
                        BitcoinEncryptedSecret encrypted = secret.Encrypt(textBoxPassword.Text);
                        textBoxEncrypted.Text = encrypted.ToString();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this, ex.Message, "Knoledge-keychain", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else if (string.IsNullOrEmpty(textBoxSecret.Text))
            {
                using (new HourGlass())
                {
                    _result = Util.Interpret(textBoxEncrypted.Text);
                }

                if (_result == null || !(_result is BitcoinEncryptedSecretNoEC))
                {
                    MessageBox.Show(this, "Unrecognized or invalid key.", "Knoledge-keychain", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                using (new HourGlass())
                {
                    try
                    {
                        BitcoinEncryptedSecretNoEC encEC = _result as BitcoinEncryptedSecretNoEC;
                        textBoxSecret.Text = encEC.GetKey(textBoxPassword.Text).GetBitcoinSecret(Network.TestNet).ToString();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this, ex.Message, "Knoledge-keychain", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
Esempio n. 5
0
        public void TestAddressCreation()
        {
            Key privateKey;                                                                               // = new Key();

            privateKey = Key.Parse("L5e7GTVzDEFTS2YwRJcRse5LkgpAetKApCafc6avoKLovPcnFE74", Network.Main); // For getting the same outputs
            PubKey         publicKey         = privateKey.PubKey;
            BitcoinAddress addressLegacy     = publicKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main);
            BitcoinAddress addressSegwit     = publicKey.GetAddress(ScriptPubKeyType.Segwit, Network.Main);
            BitcoinAddress addressSegwitP2SH = publicKey.GetAddress(ScriptPubKeyType.SegwitP2SH, Network.Main);
            KeyId          publicKeyHash     = publicKey.Hash;

            // Wif -> 
            // Priv. key -> Wif (Check README.md - Wif)
            BitcoinSecret wif  = privateKey.GetWif(Network.Main);
            BitcoinSecret wif2 = new BitcoinSecret("L5e7GTVzDEFTS2YwRJcRse5LkgpAetKApCafc6avoKLovPcnFE74", Network.Main);

            Assert.AreEqual(wif, wif2);

            // Private key -> 
            // Wif -> Priv. key
            Key privateKey1 = Key.Parse("L5e7GTVzDEFTS2YwRJcRse5LkgpAetKApCafc6avoKLovPcnFE74", Network.Main);

            Assert.AreEqual(privateKey, privateKey1);

            // Base58 encoded priv key
            var privKeyVersionBytes = Network.Main.GetVersionBytes(Base58Type.SECRET_KEY, true);

            byte[] privKeyWithVersionBytes = Helper.Concat(privKeyVersionBytes, privateKey.ToBytes());
            var    privKeyBase58           = Encoders.Base58Check.EncodeData(privKeyWithVersionBytes);

            // Encrypted priv key (BIP38)
            BitcoinEncryptedSecret encryptedPrivKey = wif.Encrypt("password");

            // Decrypted priv key
            BitcoinEncryptedSecret encryptedPrivKeyFromStr = BitcoinEncryptedSecret.Create("6PYWRpL1zPnz5kVxn74H9WdGJZVQ3ah6Pnq54GNinkfrXdd9fWNVS6dn95", Network.Main);
            BitcoinSecret          decryptedPrivKey        = encryptedPrivKeyFromStr.GetSecret("password");

            Assert.AreEqual(wif, decryptedPrivKey);

            // Public key hash -> 
            // Check README (Hash160(Public Key))
            var hash1 = NBitcoin.Crypto.Hashes.SHA256(publicKey.ToBytes());
            var pkh   = NBitcoin.Crypto.Hashes.RIPEMD160(hash1, hash1.Length); // SHA256(RIPEMD160(PUB_KUY))
            var generatedPubKeyHash = new KeyId(pkh);

            Assert.AreEqual(publicKeyHash, generatedPubKeyHash);

            // Bitcoin Address > 
            var versionBytes = Network.Main.GetVersionBytes(Base58Type.PUBKEY_ADDRESS, true); // 0x00

            byte[] PKHWithVersionBytes = Helper.Concat(versionBytes, pkh);                    // 0x00 + PKH
            var    address1            = Encoders.Base58Check.EncodeData(PKHWithVersionBytes);

            Assert.AreEqual(addressLegacy.ToString(), address1); // 1Co3ZZ3U5ELVmZrV3oXk2qbv58AjuwRrnB

            Console.WriteLine();
            Console.WriteLine($"Pri : {privateKey.ToHex()} (Hex)");         // fb401b5261327d8543382c065af27e28a9775c278b7c3dba8cd0d88f0ad042b1
            Console.WriteLine($"Pri : {privKeyBase58} (Base58)");           // 5KiwSFcZuAM3N5Ruf8cfiNzdCFupxFoFuqPafmK7JHqtsEms7HB
            Console.WriteLine($"Pri : {encryptedPrivKey} (Encrypted)");     // 6PYWRpL1zPnz5kVxn74H9WdGJZVQ3ah6Pnq54GNinkfrXdd9fWNVS6dn95
            Console.WriteLine($"Pri : {wif} (Wif)");                        // L5e7GTVzDEFTS2YwRJcRse5LkgpAetKApCafc6avoKLovPcnFE74
            Console.WriteLine($"Pub : {publicKey}");                        // 03c092d451383dedd6052de6778f9b7c393252ecbd4cd05e842638a84a2c6e2528
            Console.WriteLine($"Pub : {publicKey.Decompress()} (Decomp.)"); // 04c092d451383dedd6052de6778f9b7c393252ecbd4cd05e842638a84a2c6e2528fe41f265a1d8884eb304a33b6f8a6a245fab83f7bf503d7dfc532ffc6593df15
            Console.WriteLine($"PKH : {publicKey.Hash}");                   // 815ea7e8372ae215c40dc3d07a024adc7ddaf858
            Console.WriteLine($"Add : {addressLegacy} (Legacy)");           // 1Co3ZZ3U5ELVmZrV3oXk2qbv58AjuwRrnB
            Console.WriteLine($"Add : {addressSegwitP2SH} (SegwitP2SH)");   // 3JCvRhjrEyw9pvZiE3TxsdSHNPJQh1vHTe
            Console.WriteLine($"Add : {addressSegwit} (Segwit)");           // bc1qs90206ph9t3pt3qdc0g85qj2m37a47zclpwws7
        }