Created
December 22, 2020 22:10
-
-
Save daniel-e/f7bae1f16ba7132c435a6d7b4a573280 to your computer and use it in GitHub Desktop.
Compute Elliptic Curve Signatures 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
// Compile with: javac Ecc.java | |
// Run with: java Ecc | |
import java.security.*; | |
import java.security.spec.*; | |
public class Ecc { | |
public static void main(String[] args) throws Exception { | |
// Create a public and private key for secp224r1. | |
KeyPairGenerator g = KeyPairGenerator.getInstance("EC","SunEC"); | |
ECGenParameterSpec ecsp = new ECGenParameterSpec("secp224r1"); | |
g.initialize(ecsp); | |
KeyPair kp = g.genKeyPair(); | |
PrivateKey privKey = kp.getPrivate(); | |
PublicKey pubKey = kp.getPublic(); | |
// Select the signature algorithm. | |
Signature s = Signature.getInstance("SHA256withECDSA","SunEC"); | |
s.initSign(privKey); | |
// Compute the signature. | |
byte[] msg = "Hello, World!".getBytes("UTF-8"); | |
byte[] sig; | |
s.update(msg); | |
sig = s.sign(); | |
// Verify the signature. | |
Signature sg = Signature.getInstance("SHA256withECDSA", "SunEC"); | |
sg.initVerify(pubKey); | |
sg.update(msg); | |
boolean validSignature = sg.verify(sig); | |
System.out.println(validSignature); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment