Last active
December 19, 2023 18:35
-
-
Save ymnk/fec39e033394ee2ec47c to your computer and use it in GitHub Desktop.
ECC with Java
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
The examples to demonstrate how to use Elliptic Curve Cryptography in Java from | |
http://www.academicpub.org/PaperInfo.aspx?PaperID=14496 |
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
// 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()); | |
} | |
} |
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
// 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()); | |
} | |
} |
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
// 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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can u tell me where is the input text which is being encrypted?