Hello,
I have written a small program for signing byte-arrays with DSA. The
provider is Cryptix. Unfortunately my program doesn't work correct. The
verification says, that the signature is not correct. Can anybody help me? I
don't know how to find the bug.
Peter
import java.security.*;
class Sign
{
//Signature dsa;
public static void main(String[] dxfds)
{
byte[] signature=null;
byte[] input="This text will be signed with DSA".getBytes();
int keysize=512;
//Keygeneration
KeyPair myKeys=null;
java.security.Security.addProvider(new cryptix.provider.Cryptix());
KeyPairGenerator keyGen;
try{
//random = java.security.SecureRandom.getInstance("RSA","Cryptix");
keyGen = KeyPairGenerator.getInstance("DSA");
keyGen.initialize(keysize);
myKeys=keyGen.generateKeyPair();
}
catch(NoSuchAlgorithmException n){System.out.println("Der Algorithmus ist
unbekannt");}
//Generation of the signature
Signature dsa=null;
try
{
dsa=Signature.getInstance("DSA");
dsa.initSign(myKeys.getPrivate());
}
//catch(NoSuchProviderException n){System.out.println("Provider ist
unbekannt!");}
catch(NoSuchAlgorithmException n){System.out.println("Algorithm doesn't
exist!");}
catch(InvalidKeyException n){System.out.println("Imnvalid Key");}
try{
dsa.update(input);
}
catch(SignatureException n){}
try
{
signature=dsa.sign();
for(int i=0; i<signature.length;i++ )
System.out.println(signature[i]);
}
catch(SignatureException n){}
// boolean ok;
// Verifikation:
Signature dsaver=null;
try
{
dsaver=Signature.getInstance("DSA");
dsaver.initVerify(myKeys.getPublic());
}
//catch(NoSuchProviderException n){System.out.println("Provider ist
unbekannt!");}
catch(NoSuchAlgorithmException n){System.out.println("Algo does't
exist!");}
catch(InvalidKeyException n){System.out.println("Key is invalid");}
try{
//System.out.println("Catch-Block!");
boolean ok=dsaver.verify(signature);
//System.out.println("DEr Wert ist "+ok);
//System.out.println("test1");
if(ok)
{System.out.println("Verfication is o.k.");}
// System.out.println("test2");}
else
{System.out.println("Verfication is not o.k.");}
}
catch(SignatureException n){System.out.println("Exception");}
}
}
nobody - 04 Jan 2004 15:44 GMT
> Hello,
> I have written a small program for signing byte-arrays with DSA. The
[quoted text clipped - 71 lines]
> try{
> //System.out.println("Catch-Block!");
You have to do:
dsaver.update(input);
before verifying. i.e., sequence of steps is:
Signature dsaver = Signature.getInstance("DSA");
dsaver.initSign(keys.getPrivate());
dsaver.update(input);
byte[] sig = dsaver.sign();
// sig now holds signature
dsaver.initVerify(keys.getPublic());
dsaver.update(input);
boolean ok = dsaver.verify(sig);
> boolean ok=dsaver.verify(signature);
> //System.out.println("DEr Wert ist "+ok);
[quoted text clipped - 10 lines]
>
> }