可以参考以下的代码示例解决此问题:
import java.security.; import java.security.spec.;
public class EcdsaVerify {
public static void main(String[] args) throws Exception {
// Use P-256 curve
String algorithm = "SHA256withECDSA";
Signature signature = Signature.getInstance(algorithm);
// Generate key pair
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec ecGenParameterSpec = new ECGenParameterSpec("secp256r1");
keyPairGenerator.initialize(ecGenParameterSpec);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
// Create data to sign
byte[] data = "Hello, World!".getBytes("UTF-8");
// Sign the data with private key
signature.initSign(privateKey);
signature.update(data);
byte[] signatureBytes = signature.sign();
// Verify the signature with public key
signature.initVerify(publicKey);
signature.update(data);
boolean verified = signature.verify(signatureBytes);
System.out.println("Verified: " + verified);
}
}
此代码中使用了ECDSA算法来生成椭圆P256签名。生成和验证签名的过程中使用了公钥和私钥。如果运行代码没有报错,就表示签名验证成功。