I want a digital signature like below code: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"
is not a valid parameter for Signature.getInstance()
. You need to pass the name of the algorithm, not your data that you want to sign (I assume that's what c
is...).
I've summarized the code from the following blog for generating the signature: 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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.