Skip to content

Instantly share code, notes, and snippets.

@ymnk
Last active December 19, 2023 18:35
Show Gist options
  • Save ymnk/fec39e033394ee2ec47c to your computer and use it in GitHub Desktop.
Save ymnk/fec39e033394ee2ec47c to your computer and use it in GitHub Desktop.
ECC with Java
The examples to demonstrate how to use Elliptic Curve Cryptography in Java from
http://www.academicpub.org/PaperInfo.aspx?PaperID=14496
// The following code is from http://www.academicpub.org/PaperInfo.aspx?PaperID=14496 .
import java.math.BigInteger;
import java.security.*;
import java.security.spec.*;
import javax.crypto.KeyAgreement;
public class ECCKeyAgreement {
public static void main(String[] args) throws Exception {
KeyPairGenerator kpg;
kpg = KeyPairGenerator.getInstance("EC","SunEC");
ECGenParameterSpec ecsp;
ecsp = new ECGenParameterSpec("secp192k1");
kpg.initialize(ecsp);
KeyPair kpU = kpg.genKeyPair();
PrivateKey privKeyU = kpU.getPrivate();
PublicKey pubKeyU = kpU.getPublic();
System.out.println("User U: " + privKeyU.toString());
System.out.println("User U: " + pubKeyU.toString());
KeyPair kpV = kpg.genKeyPair();
PrivateKey privKeyV = kpV.getPrivate();
PublicKey pubKeyV = kpV.getPublic();
System.out.println("User V: " + privKeyV.toString());
System.out.println("User V: " + pubKeyV.toString());
KeyAgreement ecdhU = KeyAgreement.getInstance("ECDH");
ecdhU.init(privKeyU);
ecdhU.doPhase(pubKeyV,true);
KeyAgreement ecdhV = KeyAgreement.getInstance("ECDH");
ecdhV.init(privKeyV);
ecdhV.doPhase(pubKeyU,true);
System.out.println("Secret computed by U: 0x" +
(new BigInteger(1, ecdhU.generateSecret()).toString(16)).toUpperCase());
System.out.println("Secret computed by V: 0x" +
(new BigInteger(1, ecdhV.generateSecret()).toString(16)).toUpperCase());
}
}
// The following code is from http://www.academicpub.org/PaperInfo.aspx?PaperID=14496 .
import java.security.*;
import java.security.spec.*;
public class ECCKeyGeneration {
public static void main(String[] args) throws Exception {
KeyPairGenerator kpg;
kpg = KeyPairGenerator.getInstance("EC","SunEC");
ECGenParameterSpec ecsp;
ecsp = new ECGenParameterSpec("secp192r1");
kpg.initialize(ecsp);
KeyPair kp = kpg.genKeyPair();
PrivateKey privKey = kp.getPrivate();
PublicKey pubKey = kp.getPublic();
System.out.println(privKey.toString());
System.out.println(pubKey.toString());
}
}
// The following code is from http://www.academicpub.org/PaperInfo.aspx?PaperID=14496 .
import java.math.BigInteger;
import java.security.*;
import java.security.spec.*;
public class ECCSignature {
public static void main(String[] args) throws Exception {
KeyPairGenerator kpg;
kpg = KeyPairGenerator.getInstance("EC","SunEC");
ECGenParameterSpec ecsp;
ecsp = new ECGenParameterSpec("sect163k1");
kpg.initialize(ecsp);
KeyPair kp = kpg.genKeyPair();
PrivateKey privKey = kp.getPrivate();
PublicKey pubKey = kp.getPublic();
System.out.println(privKey.toString());
System.out.println(pubKey.toString());
Signature ecdsa;
ecdsa = Signature.getInstance("SHA1withECDSA","SunEC");
ecdsa.initSign(privKey);
String text = "In teaching others we teach ourselves";
System.out.println("Text: " + text);
byte[] baText = text.getBytes("UTF-8");
ecdsa.update(baText);
byte[] baSignature = ecdsa.sign();
System.out.println("Signature: 0x" + (new BigInteger(1, baSignature).toString(16)).toUpperCase());
Signature signature;
signature = Signature.getInstance("SHA1withECDSA","SunEC");
signature.initVerify(pubKey);
signature.update(baText);
boolean result = signature.verify(baSignature);
System.out.println("Valid: " + result);
}
}
@hellodipti
Copy link

hellodipti commented Sep 3, 2016

sir how to convert biginteger into ecpoint ??like

BigInteger S = java.math.BigInteger.valueOf(0);
BigInteger M = s.multiply(temp);
S = P.multiply(temp);ECPoint R = S; 
```// error in converting BigInteger to ECPoint.. 

@shivangi15
Copy link

please help me to convert this ECDH for three parties AS there is error as it support only 2 parties

@honeymathur2020
Copy link

Sir in these files , which is the' input ' to be encrypted?

@honeymathur2020
Copy link

can u tell me where is the input text which is being encrypted?

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