Generate Public Key From Private Key Ecdsa Encryption Java
Generate Public Key From Private Key Ecdsa Encryption Java 3,7/5 6561 reviews
  1. Generate Public Key From Private Key Ecdsa Encryption Java Server

Oct 04, 2019  ECDSA with secp256k1 in Java: generate ECC keys, sign, verify - ECDSA-secp256k1-example.java. Sep 11, 2012  This site uses cookies for analytics, personalized content and ads. By continuing to browse this site, you agree to this use.

ECDSA with secp256k1 in Java: generate ECC keys, sign, verify
ECDSA-secp256k1-example.java

Generate Public Key From Private Key Ecdsa Encryption Java Server

importorg.bouncycastle.util.encoders.Hex;
importorg.web3j.crypto.*;
importjava.math.BigInteger;
publicclassECCExample {
publicstaticStringcompressPubKey(BigIntegerpubKey) {
String pubKeyYPrefix = pubKey.testBit(0) ?'03':'02';
String pubKeyHex = pubKey.toString(16);
String pubKeyX = pubKeyHex.substring(0, 64);
return pubKeyYPrefix + pubKeyX;
}
publicstaticvoidmain(String[] args) throwsException {
//BigInteger privKey = Keys.createEcKeyPair().getPrivateKey();
BigInteger privKey =newBigInteger('97ddae0f3a25b92268175400149d65d6887b9cefaf28ea2c078e05cdc15a3c0a', 16);
BigInteger pubKey =Sign.publicKeyFromPrivate(privKey);
ECKeyPair keyPair =newECKeyPair(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);
}
}
pom.xml
<?xml version='1.0' encoding='UTF-8'?>
<projectxmlns='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>

commented Apr 5, 2018

The expected output is as follows:

commented Sep 16, 2018

java.security package contains ECDSA classes for generating the key pair, signing and verifying signatures.

(Disclaimer)I recently used this solution to remote log in to new deployed VM images on GCE.Tools used:.Steps to perform:. Ssh-rsa AAAA./VqDjtS5 ubuntu@ubuntuFor keys that were added to the SSH Agent (a program that runs in the background and avoids the need for re-entering the keyfile passphrase over and over again), you can use the ssh-add -L command to list the public keys for keys that were added to the agent (via ssh-add -l). Generate a public/private key pair using puttygen. This is a solution is specifically for users using Windows to SSH into their remote machines, including cloud images on Amazon AWS and GCE. This is useful when the SSH key is stored on a smart card (and access to the private key file is not possible). Generate public key using ssh-keygen linux

Key
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment