用于对字符串进行签名的 ECDSA 算法教程
2022-09-02 12:35:53
你能帮我找到一个简单的教程,说明如何使用java中的ECDSA算法对字符串进行签名。但是不使用任何第三方库,如弹跳城堡。只是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进行修改?