Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: Sanka555 on December 18, 2021, 07:50:05 PM



Title: I have WIF private key and try convert to HEX private key (JAVA)
Post by: Sanka555 on December 18, 2021, 07:50:05 PM
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


Title: Re: I have WIF private key and try convert to HEX private key (JAVA)
Post by: NeuroticFish on December 18, 2021, 08:04:27 PM
You should start with https://en.bitcoin.it/wiki/Wallet_import_format
From want I understand you want to do the second part (https://en.bitcoin.it/wiki/Wallet_import_format#WIF_to_private_key).

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  (https://en.bitcoin.it/wiki/Wallet_import_format#Private_key_to_WIF)and verify the checksum for the sake of your users.


Title: Re: I have WIF private key and try convert to HEX private key (JAVA)
Post by: BlackHatCoiner on December 18, 2021, 08:05:45 PM
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


Title: Re: I have WIF private key and try convert to HEX private key (JAVA)
Post by: PawGo on December 18, 2021, 08:46:25 PM
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 (https://github.com/PawelGorny/WifSolver), you will find answers for all (ok, most of) your questions.


Title: Re: I have WIF private key and try convert to HEX private key (JAVA)
Post by: Sanka555 on December 19, 2021, 06:56:32 AM
Many thanks to everyone. It's a pity I saw the last answer late)))
Here's what I wrote. It works ;D

Quote

   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;
   }

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




Title: Re: I have WIF private key and try convert to HEX private key (JAVA)
Post by: pooya87 on December 19, 2021, 07:30:49 AM
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.


Title: Re: I have WIF private key and try convert to HEX private key (JAVA)
Post by: MixMAx123 on December 19, 2021, 06:44:09 PM
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<a.length;j++)
{
for(int i=0;i<ALPHABET.length;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<a.length;i++) nullLaenge++;
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);
}