This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Questoin about java.security ?

 
Kewei Yan
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.security.*;
import java.security.spec.*;
public class testsign
{
public static void main(String[] args)
{
String myinfo="My Signature Test.";
try
{
//产生密钥对
KeyPairGenerator keygen=KeyPairGenerator.getInstance("RSA");
keygen.initialize(1024);
KeyPair keys=keygen.generateKeyPair();
PublicKey PubKey=keys.getPublic();
PrivateKey PriKey=keys.getPrivate();

//System.out.println("pubkey: " + PubKey + ", prikey: " + PriKey);

//用私钥签名
Signature sig=Signature.getInstance("SHA1withRSA");
sig.initSign(PriKey);
sig.update(myinfo.getBytes());
byte[] signed=sig.sign();

//验证签名
Signature sig2=Signature.getInstance("SHA1withRSA");
sig.initVerify(PubKey);
sig.update(myinfo.getBytes());
if(sig.verify(myinfo.getBytes()))
System.out.println("Check Signature Success");
else
System.out.println("Check Signature Failed");
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
Why the result is "Check Signature Failed" ?
I am so puzzled, can anyone help?
Thanks!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Kewei Yan!
I'm moving this thread to the Security forum...
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try

instead of

If that wasn't the problem, you might want to take a look at this guide for generating and verifying digital signatures with java.security.Signature.
 
Kewei Yan
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To: Lasse Koskela
Thanks for you help!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic