你能帮我找到一个简单的教程,说明如何在 Java 中使用 ECDSA 算法对字符串进行签名。但不使用任何第三方库,如 bouncycastle。只是JDK 7。我发现很难搜索一个简单的例子,我是密码学的新手。
import java.io.*;
import java.security.*;
public class GenSig {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
/*
* Generate a DSA signature
*/
try {
/*
* Generate a key pair
*/
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keyGen.initialize(1024, random);
KeyPair pair = keyGen.generateKeyPair();
PrivateKey priv = pair.getPrivate();
PublicKey pub = pair.getPublic();
/*
* Create a Signature object and initialize it with the private key
*/
Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");
dsa.initSign(priv);
String str = "This is string to sign";
byte[] strByte = str.getBytes();
dsa.update(strByte);
/*
* Now that all the data to be signed has been read in, generate a
* signature for it
*/
byte[] realSig = dsa.sign();
System.out.println("Signature: " + new String(realSig));
} catch (Exception e) {
System.err.println("Caught exception " + e.toString());
}
}
}
如何为 ECDSA 修改它?