29 lines
987 B
Java
29 lines
987 B
Java
package sheet02.a2;
|
|
|
|
import java.security.KeyFactory;
|
|
import java.security.Signature;
|
|
import java.security.spec.X509EncodedKeySpec;
|
|
import java.util.Base64;
|
|
|
|
public class VerifyECDSA {
|
|
public static void main(String[] args) throws Exception {
|
|
if (args.length < 3) {
|
|
System.out.println(
|
|
"Please specify in the following order: Public Key (X509), Signature (Base64), Message (plain text)");
|
|
return;
|
|
}
|
|
|
|
final String PUBLIC_KEY = args[0];
|
|
final String SIGNATURE = args[1];
|
|
final String MESSAGE = args[2];
|
|
|
|
final var kf = KeyFactory.getInstance("EC");
|
|
final var pub = kf.generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(PUBLIC_KEY)));
|
|
|
|
final var sig = Signature.getInstance("SHA256withECDSA");
|
|
sig.initVerify(pub);
|
|
sig.update(MESSAGE.getBytes());
|
|
System.out.println("Correct: " + sig.verify(Base64.getDecoder().decode(SIGNATURE)));
|
|
}
|
|
}
|