[英]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.