Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Please help I am stuck at "not a DSA public key"

843810Jun 3 2002 — edited Apr 26 2006
Hi

I am just starting with the certificate/security API so this may be a naive question. I have a certificate,private key signature & data from a third-party. I am using following program to verify the signature.
import java.io.*;
import java.security.*;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.spec.*;
import java.util.Collection;
import java.util.Iterator;

class VerSig {

    public static void main(String[] args) {

        /* Verify a DSA signature */

        if (args.length != 3) {
            System.out.println("Usage: VerSig publickeyfile signaturefile datafile");
        }
        else try{

            InputStream inStream = new FileInputStream(args[0]);
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            Collection c = cf.generateCertificates(inStream);
            Iterator i = c.iterator();
            X509Certificate cert = null;
            while (i.hasNext()) {
                cert = (X509Certificate)i.next();
            }
            inStream.close();
            PublicKey pubKey = null;
            if (cert != null)
                pubKey = cert.getPublicKey();

            // create a Signature object and initialize it with the public key
            Signature sig = Signature.getInstance("SHA1withDSA","SUN");
            sig.initVerify(pubKey);

            // Update and verify the data

            FileInputStream datafis = new FileInputStream(args[2]);
            BufferedInputStream bufin = new BufferedInputStream(datafis);

            byte[] buffer = new byte[1024];
            int len;
            while (bufin.available() != 0) {
                len = bufin.read(buffer);
                sig.update(buffer, 0, len);
            };

            bufin.close();
            //input the signature bytes
            FileInputStream sigfis = new FileInputStream(args[1]);
            byte[] sigToVerify = new byte[sigfis.available()];
            sigfis.read(sigToVerify );

            sigfis.close();
            boolean verifies = sig.verify(sigToVerify);

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


        } catch (Exception e) {
            System.err.println("Caught exception " + e.toString());
        };

    }

}
First of all :
1) I wrote "Signature sig = Signature.getInstance("SHA1withDSA","SUN");" because somewhere in the certificate, I saw Signature Algorithm: SHA1withDSA. Is this correct?
2)I am getting following exception after
Signature sig = Signature.getInstance("SHA1withDSA","SUN");"
sig.initVerify(pubKey);
Caught exception java.security.InvalidKeyException: not a DSA public key: algorithm = SHA1withDSA, params unparsed, unparsed keybits =
0000: 02 41 00 91 89 17 2D 83 2D 19 51 96 8F D3 A7 CE .A....-.-.Q.....
0010: 33 E7 B0 1F 6C 79 F4 91 3E B5 5E 81 92 42 65 BA 3...ly..>.^..Be.
0020: 56 F8 8B F4 FF 54 4F D6 ED 38 A4 71 BD BE D4 69 V....TO..8.q...i
0030: 21 02 E3 CD 48 96 BC B3 14 F4 42 90 4D 38 5C 78 !...H.....B.M8\x
0040: D3 26 58 .&X

what am I doing wrong?

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 24 2006
Added on Jun 3 2002
7 comments
1,016 views