Author

Topic: I have WIF private key and try convert to HEX private key (JAVA) (Read 224 times)

full member
Activity: 161
Merit: 168
In Java:
Code:
public static String Base58ToHexString(String str, int laenge)
{
char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();
char[] a = str.toCharArray();
BigInteger erg = new BigInteger("0");
BigInteger b58 = new BigInteger("58");
int e = a.length-1;
for(int j=0;j {
for(int i=0;i {
if(a[j]==ALPHABET[i])
{
    BigInteger I = new BigInteger(String.valueOf(i));
    erg = erg.add(I.multiply(b58.pow(e)));
}   
}
e--;
}
char[] c = erg.toString().toCharArray();
int nullLaenge = 0;
for(int i=0; a[i]=='1' && i char[] EINS   = {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
char[] KeyOut = new char[nullLaenge + c.length];
System.arraycopy(c, 0, KeyOut, nullLaenge, c.length);
System.arraycopy(EINS, 0, KeyOut, 0, nullLaenge);
String out = new String(KeyOut);
BigInteger big = new BigInteger(out,10);
out =  big.toString(16);
String nullen = "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
out = nullen+out;
return out.substring(out.length()-laenge);
}
legendary
Activity: 3472
Merit: 10611
Code:
	public static String wifToHex(String wifKey) throws Exception {
byte[] bytes = Base58.decode(wifKey);
String pk = bytesToHex(bytes);
pk = pk.substring(2, pk.length() - 8);
return pk;
}
Your code lacks verification (byte length, first byte, last byte, checksum) and it also works only for uncompressed WIFs if you are given a compressed WIF your code will return a 33 byte hex result because you aren't removing the last byte in forth line here.
member
Activity: 96
Merit: 36
Many thanks to everyone. It's a pity I saw the last answer late)))
Here's what I wrote. It works Grin

Quote

   public static String wifToHex(String wifKey) throws Exception {
      byte[] bytes = Base58.decode(wifKey);
      String pk = bytesToHex(bytes);
      pk = pk.substring(2, pk.length() - Cool;
      return pk;
   }

public static String bytesToHex(byte[] in) {
      final StringBuilder builder = new StringBuilder();
      for (byte b : in) {
         builder.append(String.format("%02x", b));
      }
      return builder.toString();
   }


legendary
Activity: 952
Merit: 1385
Of course my dear Sanka555, this is the solution:

Code:
String toCheck = "5KWDpqqbx24hZmqMzHUAsVZVEJiJukK4MP25ZuyDMapAVs4s6p3";
boolean compressed = toCheck.length()==52;
try{
byte[] bytes = Base58.decode(toCheck);
ECKey ecKey = ECKey.fromPrivate(Arrays.copyOfRange(bytes, 1, bytes.length - 4), compressed);
String privKey = ecKey.getPrivateKeyAsHex();
System.out.println(privKey);
}catch(Exception e){
System.err.println(e.getLocalizedMessage());
}

I hope your project is progressing well ;-)

I have told you already 5 times - check sourcecode of my https://github.com/PawelGorny/WifSolver, you will find answers for all (ok, most of) your questions.
legendary
Activity: 1512
Merit: 7340
Farewell, Leo
Disclaimer:  I want you to understand the technical part. I could just give you few lines of code that do the work, but I insist on acknowledging this procedure.

A WIF private key is a base58 encoding of four things;

  • A version byte prefix. (0x80 for mainnet)
  • The 256-bit number which is the private key.
  • A compression byte suffix. (0x01 if the public key is compressed, nothing if it's uncompressed)
  • A checksum which is the first 4 bytes of the double SHA256 hash of the above three.

For instance, let's say that I want to convert to WIF the following number:
Code:
b483cd7d8a5245e6d7c4fc4b06de669a15ce3256b7c4d0a08a5a0ab75a0db46d

Step 1:
Code:
80

Step 2:
Code:
80b483cd7d8a5245e6d7c4fc4b06de669a15ce3256b7c4d0a08a5a0ab75a0db46d

Step 3: (Compressed public key)
Code:
80b483cd7d8a5245e6d7c4fc4b06de669a15ce3256b7c4d0a08a5a0ab75a0db46d01

(Double hash of the above is 01c46123132816f6f7158ddb53fad901bcc9896b84d1b960c26f8dd62e42039a)

Thus, step 4:
Code:
80b483cd7d8a5245e6d7c4fc4b06de669a15ce3256b7c4d0a08a5a0ab75a0db46d0101c46123

By converting it to base58 you get:
Code:
L3GcE8o5kgmRhJF1gavfUMjHsz2tSgZYseQeQbD1iTkHBvQoR7A2



To achieve what you want, just go backwards.
Code:
5KWDpqqbx24hZmqMzHUAsVZVEJiJukK4MP25ZuyDMapAVs4s6p3

Decode the base58:
Code:
80DE5FBB6104D65246E31486BF004701E8A9FA0C388BFA0D6CB8E6F1C1A117CB9006F6CF1C

Remove the last 8 characters:
Code:
80DE5FBB6104D65246E31486BF004701E8A9FA0C388BFA0D6CB8E6F1C1A117CB90

I can recognize it's an uncompressed address. Therefore, you don't remove the last 2 characters. Just remove the prefix at the start and you have your number:
Code:
DE5FBB6104D65246E31486BF004701E8A9FA0C388BFA0D6CB8E6F1C1A117CB90
legendary
Activity: 3668
Merit: 6382
Looking for campaign manager? Contact icopress!
You should start with https://en.bitcoin.it/wiki/Wallet_import_format
From want I understand you want to do the second part.

For base58 decoder, take a look if this works: https://gist.github.com/vrotaru/1753908

I'd add that it may be nice if you do then the steps from the first part and verify the checksum for the sake of your users.
member
Activity: 96
Merit: 36
I need to do like in this calculator https://secretscan.org/PrivateKeyWif
the entrance is invited
5KWDpqqbx24hZmqMzHUAsVZVEJiJukK4MP25ZuyDMapAVs4s6p3

at the output I get
DE5FBB6104D65246E31486BF004701E8A9FA0C388BFA0D6CB8E6F1C1A117CB90

how to do it on JAva?

i tried to convert wif to hex both as string and as byte array.

 the correct answer is not obtained.

I would be very grateful for any ideas
Jump to: