-
-
Save yeshengwu/ba02a2522f2adc65a5c35e3825bea4c2 to your computer and use it in GitHub Desktop.
ECDSA with secp256k1 in Java: generate ECC keys, sign, verify
This file contains hidden or 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 org.bouncycastle.util.encoders.Hex; | |
import org.web3j.crypto.*; | |
import java.math.BigInteger; | |
public class ECCExample { | |
public static String compressPubKey(BigInteger pubKey) { | |
String pubKeyYPrefix = pubKey.testBit(0) ? "03" : "02"; | |
String pubKeyHex = pubKey.toString(16); | |
String pubKeyX = pubKeyHex.substring(0, 64); | |
return pubKeyYPrefix + pubKeyX; | |
} | |
public static void main(String[] args) throws Exception { | |
//BigInteger privKey = Keys.createEcKeyPair().getPrivateKey(); | |
BigInteger privKey = new BigInteger("97ddae0f3a25b92268175400149d65d6887b9cefaf28ea2c078e05cdc15a3c0a", 16); | |
BigInteger pubKey = Sign.publicKeyFromPrivate(privKey); | |
ECKeyPair keyPair = new ECKeyPair(privKey, pubKey); | |
System.out.println("Private key: " + privKey.toString(16)); | |
System.out.println("Public key: " + pubKey.toString(16)); | |
System.out.println("Public key (compressed): " + compressPubKey(pubKey)); | |
String msg = "Message for signing"; | |
byte[] msgHash = Hash.sha3(msg.getBytes()); | |
Sign.SignatureData signature = Sign.signMessage(msgHash, keyPair, false); | |
System.out.println("Msg: " + msg); | |
System.out.println("Msg hash: " + Hex.toHexString(msgHash)); | |
System.out.printf("Signature: [v = %d, r = %s, s = %s]\n", | |
signature.getV() - 27, | |
Hex.toHexString(signature.getR()), | |
Hex.toHexString(signature.getS())); | |
System.out.println(); | |
BigInteger pubKeyRecovered = Sign.signedMessageToKey(msg.getBytes(), signature); | |
System.out.println("Recovered public key: " + pubKeyRecovered.toString(16)); | |
boolean validSig = pubKey.equals(pubKeyRecovered); | |
System.out.println("Signature valid? " + validSig); | |
} | |
} |
This file contains hidden or 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
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>bc-examples</groupId> | |
<artifactId>bc-examples</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>org.web3j</groupId> | |
<artifactId>crypto</artifactId> | |
<version>3.3.1</version> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment