What I have
1. I am able to create new account with personal.newAccount() method of web3 api.
2. I am working on my private blockchain.
3. geth node start with following command:-
geth --datadir ~/privateBlockChain1 --networkid 1300 --rpcport 8002 --port 30301 --rpcapi="db,eth,net,web3,personal,admin,txpool,debug" --rpc --maxpeers 0 --nodiscover --targetgaslimit 110000000000 --dev --rpccorsdomain "*" --rpcaddr "127.0.0.1" console
4. I am using web3 api and ethereumjs-wallet npm api.
5. Keystore file is successfully store in private blockchain directory.

What I want
1. I want to get private key of respective account.
2. actually, i want to get all data of keystore file of respective account from my nodejs side and will be able to save in my regular database.

Case 1:
1. when i create account with personal.newAccount() method with password, how to get private key, if i got private key, then i will be use that key to get all keystore data by following code:---

   var Wallet = require('ethereumjs-wallet');  
    var Key=Buffer.from('account_privatekey','hex');  
    var wallet = Wallet.fromPrivateKey(Key);  
    var json=wallet.toV3String(user_define_password);  // it will return keystore object data. 

but i am unable to get actual private key from account address and password.
` Case 2:
1. suppose i create private key by following code:-

     var Wallet = require('ethereumjs-wallet');  
        var privateKey=Wallet.generate(password)._privKey;  
        var wallet = Wallet.fromPrivateKey(privateKey);  
        var json=wallet.toV3String(password); // it will get keystore data      `
  2. account creation on my private blockchain this way   
           `  personal.importRawKey(key,password)  
          but error showing like , importRawKey not a function `

How to fix these

  • Isn't it the case that the whole point of a private key is that you cannot get it from the account address and password? The private key is on your hard drive somewhere, but the whole point of the security of public/private keys is that you cannot get it--even if you spent an near-infinite amount of computing resources--from the public key. – Thomas Jay Rush Mar 7 '17 at 15:06
  • you should add --rpcapi "personal" – user22021 Nov 1 '17 at 11:53

How to get private key from account address and password?

One of the founding notions of public/private key cryptography is that what you are describing should be impossible. No cryptographic system in existence would be trusted if you could reverse engineer the private key from the public address.

So not only is it impossible, but it should be impossible.


You state:

when i create account with personal.newAccount() method with password, how to get private key, if i got private key, then i will be use that key to get all keystore data by following code:---

Are you ask for the directory location of your keystore? If so this may answer your question : Getting private keys of geth accounts

Also see : https://ethereum.gitbooks.io/frontier-guide/content/backup_restore.html

Everything geth persists gets written inside its data directory (except for the PoW Ethash DAG, see note below). The default data directory locations are platform specific:

  • Mac: ~/Library/Ethereum
  • Linux: ~/.ethereum
  • Windows: %APPDATA%/Ethereum

Accounts are stored in the keystore subdirectory. The contents of this directories should be transportable between nodes, platforms, implementations (C++, Go, Python).

  • 6
    The password is used to decrypt the keystore, and thus, the private key. So not only is it not impossible, but it HAS to be possible otherwise the wallet won't be able to spend funds. The OP has clearly stated that the account was created on the same node where he is entering the password. – Jus12 Jul 16 '17 at 18:42
  • this is not the answer to the question, downvote – Nulik May 10 at 21:46

I used this code to recover the private key from account address and password. firstly, install Keythereum:

npm install keythereum

Keythereum is a JavaScript tool to generate, import and export Ethereum keys. read the documentation: GitHub Link

and this is my code:

var keythereum = require("keythereum");
var datadir = "/home/super/.ethereum/rinkeby";
var address= "0xc8096d713000002c77e4eb62f0000ead5f105a7e";
const password = "mypass";

var keyObject = keythereum.importFromFile(address, datadir);
var privateKey = keythereum.recover(password, keyObject);
console.log(privateKey.toString('hex'));

You cannot get the private key from the address and password. But you can get the private key from the keyfile and the password, which should work in your case, as I understand the question. For more information on how to get the private key from the keyfile see here.

Private key is often required to import your account in a different wallet. Even Metamask needs a private key to import the account. This is how I was able to extract the private key from the node where I created my account. Ethereum keys in a Linux node will be encrypted and stored in the following location.

~/.ethereum/keystore/ (mainnet)
~/.ethereum/rinkeby/keystore/ (rinkeby testnet)

If you have the public address of the account and the password used to lock the account, you should be able to extract the private key. I used web3 python package to extract the private key. Install this using pip.

pip install web3

Execute the following code

>>> from web3.auto import w3
>>> with open("~/.ethereum/rinkeby/keystore/UTC--2018-06-
    10T05-43-22.134895238Z--9e63c0d223d9232a4f3076947ad7cff353cc1a28") 
     as keyfile:
...     encrypted_key = keyfile.read()
...     private_key = w3.eth.account.decrypt(encrypted_key, 
                                             'password')

UTC--2018-06-10T05-43-22.134895238Z--9e63c0d223d9232a4f3076947ad7cff353cc1a28 is the file containing stored key. This will return a private key in byte format.

You can get the private key in hex format as follows.

import binascii
binascii.b2a_hex(private_key)

The answer to your question is here:

https://github.com/ethereum/go-ethereum/blob/master/accounts/keystore/keystore_passphrase_test.go

Change the filename , password add a line to print the private key to the console and run

go build ./keystore_passphrase_test.go

You can try to brute-force the private key and check if it matches the public key in mind. https://en.wikipedia.org/wiki/Brute-force_attack. It will take you some time because it's designed to be mathematically impossible. Have a look at https://en.wikipedia.org/wiki/Public-key_cryptography

  • It's designed to be infeasible (based on our current understanding of mathematics and physics), not mathematically impossible. I guess this comment also applies to the other answer provided. – lungj Oct 24 '17 at 16:41

I found this because I'm trying to export a private key - which I can fully use to send transfers inside geth - so that I can load it into an android app. For that, I noticed that you can download MyEtherWallet from GitHub and run it locally on your machine. You can then safely (as it's all local to your PC) import the geth keystore JSON into that offline MEW and use MEW to decrypt the wallet and show you the private key.

https://github.com/kvhnuke/etherwallet/releases/tag/v3.21.11

I wrote this Go program to export the private key in Hex format:

package main

import (
    "fmt"
    "io/ioutil"
    "github.com/ethereum/go-ethereum/accounts/keystore"
    "github.com/ethereum/go-ethereum/crypto"
)

func main() {
    inPath:=".ethereum/keystore/UTC--2018-11-05T16-07-58.071516100Z--21f2bda7bb26e113687ca8827fac1966a68e8b4c"
    outPath:="key.hex"
    password:="my-wallet-password"
    keyjson,e:=ioutil.ReadFile(inPath); if e != nil { panic(e) }
    key,e:=keystore.DecryptKey(keyjson,password); if e != nil { panic(e) }
    e=crypto.SaveECDSA(outPath,key.PrivateKey); if e!=nil { panic(e) }
    fmt.Println("Key saved to:",outPath)
}

I build it like this:

go build showkey.go

I run it like this:

./showkey 

It produces an output file that looks like this (i changed some of the numbers):

$ cat key.hex 
e33b57632115a6015cc7721fdda48357b468b73a17e33e3a250c4de266efb1c0

I am able to import the resulting private key into MetaMask.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.