[英]Generating a digital signature with DSA
我想要一个类似下面代码的数字签名:Sign[publickey, a||b]。
Integer a;
String b,c;
a=12; b= "i am fine";
c=a+b;
Signature DSA = Signature.getInstance(c, "SUN");
DSA.initSign(pvKey);
"12iamfine"
不是Signature.getInstance()
的有效参数。 您需要传递算法的名称,而不是您要签名的数据(我认为这就是c
是......)。
我总结了以下博客中用于生成签名的代码: https : //compscipleslab.wordpress.com/2012/11/18/generating-verifying-digital-signatures/
//Create a KeyPairGenerator object for generating keys for the DSA signature algorithm.
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN");
//Instantiate SecureRandom Object, which will act as a source of random numbers.
//Here, we use SHA1PRNG
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
//Initialize the KeyPairGenerator with the key-length and the source of randomness
keyGen.initialize(1024, random);
//Generate the key-pair
KeyPair pair = keyGen.generateKeyPair();
//Collect the public & private key from the KeyPair into separate objects
PrivateKey privkey = pair.getPrivate();
PublicKey pubkey = pair.getPublic();
//Create a Signature object, you have to supply two arguments,first the algorithm
//name & the provider. Here we use SHA1withDSA supplied by the SUN provider
Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");
//Initialize it with the private key before using it for signing.
dsa.initSign(privkey);
//Supply the Signature Object the data to Be Signed
BufferedInputStream bufin = new BufferedInputStream(new FileInputStream(inputfile));
byte[] buffer = new byte[1024];
int len;
while ((len = bufin.read(buffer)) >=0) {
dsa.update(buffer, 0, len);
}
bufin.close();
//Sign the data i.e. generate a signature for it
byte[] realSig = dsa.sign();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.