aspose file tools*
The moose likes Security and the fly likes Questoin about java.security ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Security
Bookmark "Questoin about java.security ?" Watch "Questoin about java.security ?" New topic
Author

Questoin about java.security ?

Kewei Yan
Greenhorn

Joined: Jan 08, 2004
Posts: 6
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

Joined: Dec 10, 2001
Posts: 7023
Welcome to JavaRanch, Kewei Yan!
I'm moving this thread to the Security forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Kewei Yan
Greenhorn

Joined: Jan 08, 2004
Posts: 6
To: Lasse Koskela
Thanks for you help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Questoin about java.security ?