Bitcoin Forum
May 22, 2024, 11:45:16 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: I have WIF private key and try convert to HEX private key (JAVA)  (Read 210 times)
Sanka555 (OP)
Member
**
Offline Offline

Activity: 96
Merit: 36


View Profile
December 18, 2021, 07:50:05 PM
 #1

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
NeuroticFish
Legendary
*
Offline Offline

Activity: 3682
Merit: 6406


Looking for campaign manager? Contact icopress!


View Profile
December 18, 2021, 08:04:27 PM
 #2

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.

█████████████████████████
████▐██▄█████████████████
████▐██████▄▄▄███████████
████▐████▄█████▄▄████████
████▐█████▀▀▀▀▀███▄██████
████▐███▀████████████████
████▐█████████▄█████▌████
████▐██▌█████▀██████▌████
████▐██████████▀████▌████
█████▀███▄█████▄███▀█████
███████▀█████████▀███████
██████████▀███▀██████████
█████████████████████████
.
BC.GAME
▄▄░░░▄▀▀▄████████
▄▄▄
██████████████
█████░░▄▄▄▄████████
▄▄▄▄▄▄▄▄▄██▄██████▄▄▄▄████
▄███▄█▄▄██████████▄████▄████
███████████████████████████▀███
▀████▄██▄██▄░░░░▄████████████
▀▀▀█████▄▄▄███████████▀██
███████████████████▀██
███████████████████▄██
▄███████████████████▄██
█████████████████████▀██
██████████████████████▄
.
..CASINO....SPORTS....RACING..
█░░░░░░█░░░░░░█
▀███▀░░▀███▀░░▀███▀
▀░▀░░░░▀░▀░░░░▀░▀
░░░░░░░░░░░░
▀██████████
░░░░░███░░░░
░░█░░░███▄█░░░
░░██▌░░███░▀░░██▌
░█░██░░███░░░█░██
░█▀▀▀█▌░███░░█▀▀▀█▌
▄█▄░░░██▄███▄█▄░░▄██▄
▄███▄
░░░░▀██▄▀


▄▄████▄▄
▄███▀▀███▄
██████████
▀███▄░▄██▀
▄▄████▄▄░▀█▀▄██▀▄▄████▄▄
▄███▀▀▀████▄▄██▀▄███▀▀███▄
███████▄▄▀▀████▄▄▀▀███████
▀███▄▄███▀░░░▀▀████▄▄▄███▀
▀▀████▀▀████████▀▀████▀▀
BlackHatCoiner
Legendary
*
Online Online

Activity: 1526
Merit: 7408


Farewell, Leo


View Profile
December 18, 2021, 08:05:45 PM
Merited by Pmalek (1)
 #3

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

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
PawGo
Legendary
*
Offline Offline

Activity: 952
Merit: 1367


View Profile
December 18, 2021, 08:46:25 PM
Merited by pooya87 (1), ABCbits (1), Pmalek (1), nc50lc (1)
 #4

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.
Sanka555 (OP)
Member
**
Offline Offline

Activity: 96
Merit: 36


View Profile
December 19, 2021, 06:56:32 AM
 #5

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


pooya87
Legendary
*
Offline Offline

Activity: 3458
Merit: 10576



View Profile
December 19, 2021, 07:30:49 AM
Merited by Pmalek (1)
 #6

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.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
MixMAx123
Full Member
***
Offline Offline

Activity: 161
Merit: 168


View Profile
December 19, 2021, 06:44:09 PM
 #7

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);
}
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!