Solved

Java Security Signature Exception problem

Posted on 2007-12-02
8
3,529 Views
Last Modified: 2013-11-23
Hi,
I'm having some problems with a piece of code that i'm trying to write where it throws a SignatureException - invalid encoding for signature.

- I declare the Signature as a 'global' variable.

- Before signing the message, I do the following:
 sig = Signature.getInstance("DSA");
 sig.initSign(PrivateKey);      

- When I want to verify the signature, first I do this:
sig.initVerify(PublicKey);

The error is thrown right at the end when I come to the verification:
if (!sig.verify(sig))
              System.out.print("Signature NOT");
              System.out.print("Verified");

Anyone understand why I get an invalid coding message and what I should do to fix this?

Cheers,

Phil.

0
Comment
Question by:phil8258
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20393296
where r you're key vars coming from?
0
 

Author Comment

by:phil8258
ID: 20393342
Some code below:
// Declared in the main class:
public static PrivateKey PrivateKey = null;
public static PublicKey PublicKey = null;	
public static Signature sig = null;
 
//A bit of code from a method that creates the keys:
KeyPair keys = keypair.generateKeyPair();
PrivateKey = keys.getPrivate();
PublicKey = keys.getPublic();
 
//A bit of code from the method that signs the message:
sig = Signature.getInstance("DSA");
sig.initSign(PrivateKey);
 
//All of the above seems to work ok. Then...
 
//A bit of code from the method that verifies the signature:
sig.initVerify(PublicKey);
 
File f = new File("Input.txt");   //Open signed file
DataInputStream f = new DataInputStream(new FileInputStream(f));
 
byte[] sigTemp = new byte[SignedFile.read()];	//Read signed file into byte array
f.read(sigTemp, 0, f.read());
 
int length = (int) f.length();
byte[] Input = new byte[length];
f.read(Input, 0, length);
f.close();
 
sig.update(Input);
       
if (!sig.verify(sig))
              System.out.print("Signature NOT");
              System.out.print("Verified");
	}

Open in new window

0
 

Author Comment

by:phil8258
ID: 20393347
Error occurs at line 33 in the above code
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 20393357
byte[] sigTemp = new byte[SignedFile.read()];   //Read signed file into byte array
f.read(sigTemp, 0, f.read());

whats that meant to be doing.
does not look like it would even compile
0
 

Author Comment

by:phil8258
ID: 20393390
Good question!
I removed it, and the first thing that went wrong (or right) was that line 33 could no longer refer to sig
I changed line 33 to if (!sig.verify(Input))
and exception gone :O)

Only problem now is that its always reporting Signature Not Verified...
Changing to:

        if (!signature.verify(Input)){
              System.out.println("Signature not verified");
        }
        else {
              System.out.println("Signature verified sucessfully");
        }

and it still says "Signature not Verified"
0
 
LVL 92

Expert Comment

by:objects
ID: 20393410
>         if (!signature.verify(Input)){

the verify method expects the bytes of a signature, is that what Input contains?
0
 

Author Comment

by:phil8258
ID: 20393438
Input contains:
0,ljaØÄaÉ"¼]~ÆSNxMQk[ªhzDÞ/mRyÒ¬!î=ïhÜHello World!
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 20393482
u sure thats a signature? seems to also include data.
how was it created?

there an example here to sign and verify

http://www.java2s.com/Code/Java/Security/Testthesignature.htm
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maven module vs maven project 3 73
Java 8 to Java 6 8 33
Html split(text) 2 30
Strange router problem - can't access hotmail.com 14 54
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question