我正在尝试使用带有secp256r1曲线(P256)的ECDSA和用于消息散列的SHA256算法生成签名.我也在使用Bouncy Castle图书馆.
代码如下,
public class MyTest {
/**
* @param args
*/
public static void main(String[] args) {
new MyTest().getSign();
}
void getSign() {
// Get the instance of the Key Generator with "EC" algorithm
try {
KeyPairGenerator g = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec kpgparams = new ECGenParameterSpec("secp256r1");
g.initialize(kpgparams);
KeyPair pair = g.generateKeyPair();
// Instance of signature class with SHA256withECDSA algorithm
Signature ecdsaSign = Signature.getInstance("SHA256withECDSA");
ecdsaSign.initSign(pair.getPrivate());
System.out.println("Private Keys is::" + pair.getPrivate());
System.out.println("Public Keys is::" + pair.getPublic());
String msg = "text ecdsa with sha256";//getSHA256(msg)
ecdsaSign.update((msg + pair.getPrivate().toString())
.getBytes("UTF-8"));
byte[] signature = ecdsaSign.sign();
System.out.println("Signature is::"
+ new BigInteger(1, signature).toString(16));
// Validation
ecdsaSign.initVerify(pair.getPublic());
ecdsaSign.update(signature);
if (ecdsaSign.verify(signature))
System.out.println("valid");
else
System.out.println("invalid!!!!");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
这里使用KeyPair生成密钥对,但根据我的要求,我将使用静态privateKey和公钥.此外,签名验证始终返回false.
需要帮助,我如何拥有静态私钥和验证部分.
本文介绍了一种使用ECDSA算法和SHA256散列函数生成数字签名的方法,并通过BouncyCastle库实现了这一过程。文章提供了Java代码示例,演示了如何创建密钥对、生成签名及验证签名的有效性。
9155

被折叠的 条评论
为什么被折叠?



