Forked from madsunrise/Validate bitcoin wallet passphrase
Created
December 3, 2024 02:08
-
-
Save ii0/360b02524bdca5a0d985adfabc560f58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.crypto.Cipher; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.Arrays; | |
public class PasswordChecker { | |
public static void main(String[] args) { | |
String hash = "$bitcoin$64$0839e385bbc0e714376eb9919713b754a69175e79de9641441e495832f46f8eb$16$53ed1412a321a660$199514$2$00$2$00"; | |
String password = "your_supposed_passphrase"; | |
System.out.println(new PasswordChecker().check(password, hash)); | |
} | |
public boolean check(String passphrase, String walletHash) { | |
String withoutPrefix = walletHash.replaceFirst("^\\$bitcoin\\$", ""); | |
String[] split = withoutPrefix.split("\\$"); | |
String temp = bytesToHex(passphrase.getBytes()) + split[3]; | |
byte[] passToHash = hexStringToByteArray(temp); | |
int iterCount = Integer.parseInt(split[4]); | |
for (int i = 0; i < iterCount; ++i) { | |
passToHash = calculateSha512(passToHash); | |
} | |
byte[] key = Arrays.copyOfRange(passToHash, 0, 32); | |
byte[] iv = Arrays.copyOfRange(passToHash, 32, 48); | |
try { | |
decrypt(hexStringToByteArray(split[1]), key, iv); | |
return true; | |
} catch (Exception e) { | |
return false; | |
} | |
} | |
private byte[] calculateSha512(byte[] data) { | |
try { | |
MessageDigest md = MessageDigest.getInstance("SHA-512"); | |
md.update(data); | |
return md.digest(); | |
} catch (NoSuchAlgorithmException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
private void decrypt(byte[] encrypted, byte[] key, byte[] iv) throws Exception { | |
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); | |
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES"); | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); | |
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec); | |
cipher.doFinal(encrypted); | |
} | |
private static byte[] hexStringToByteArray(String s) { | |
int len = s.length(); | |
byte[] data = new byte[len / 2]; | |
for (int i = 0; i < len; i += 2) { | |
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) | |
+ Character.digit(s.charAt(i+1), 16)); | |
} | |
return data; | |
} | |
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); | |
private static String bytesToHex(byte[] bytes) { | |
char[] hexChars = new char[bytes.length * 2]; | |
for (int j = 0; j < bytes.length; j++) { | |
int v = bytes[j] & 0xFF; | |
hexChars[j * 2] = HEX_ARRAY[v >>> 4]; | |
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; | |
} | |
return new String(hexChars); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment