• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Questoin about java.security ?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Kewei Yan!
I'm moving this thread to the Security forum...
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To: Lasse Koskela
Thanks for you help!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic