• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DSA - Signature failed

 
Murali Sekhar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following program failed in verifying. Can somebody tell me where I am doing mistake...
import java.io.*;
import java.security.*;
import java.security.interfaces.*;
import sun.misc.BASE64Encoder;
import java.security.spec.*;
public class JCETest {
public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("c:/ebmessage.xml");
BufferedReader br = new BufferedReader(fr);
String message = "";
String record = "";
while((record = br.readLine()) != null)
{
record = br.readLine();
message+=record;

}

byte[] messageBytes = message.getBytes("UTF8");

KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA","SUN");
SecureRandom raandom = SecureRandom.getInstance("SHA1PRNG", "SUN");
kpg.initialize(1024, raandom);
System.out.println("Generating a key pair...");
KeyPair mykeyPair = kpg.generateKeyPair();
System.out.println("Done generating keys.\n");

Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");

DSAPrivateKey priKey = (DSAPrivateKey)mykeyPair.getPrivate();
dsa.initSign(priKey);
dsa.update(messageBytes);
byte[] sign = dsa.sign();


DSAPublicKey pKey = (DSAPublicKey)mykeyPair.getPublic();
dsa.initVerify(pKey);
//dsa.update(sign);
boolean verifies = dsa.verify(sign);

System.out.println("signature verifies: " + verifies);


}
}
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Murali, please update your display name to meet the requirements of JavaRanch. You must use you real first anme -space - your real last name.
As to your question:
You aren't using the signatures correctly. Let's think about what you are trying to do. You have a message and you want to verify that it came from Al Capone. Al Capone sends you his public cipher and a message that can only be generated with Al Capone's private cipher. You want to verify that the message that came in the clear is identical to Al's secret message. If they are then the original message really did come from Al and you can make your Valetine's Day plans.
How to do that? Change this line:
//dsa.update(sign);
to
dsa.update(messageBytes);
happy encrypting!
 
Murali Sekhar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Paul,
Its working.great!
Murali
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic