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!

Invalid signature value under java 1.5.0_10 and PKCS11

843811Jan 10 2007 — edited Apr 27 2007
Hi!

I have a problem with my GemSafe "Smart Card" and Java 1.5. I've developed a program that signs a document, every thing seems to be fine, but when I try to verify the signature a "Signature encoding error" is thrown. If we change the line:
Signature verify = Signature.getInstance("SHA1withRSA");
using:
Signature verify = Signature.getInstance("SHA1withRSA", pkcs11Provider);
The verification status is "OK"! But it has no sense to use that provider in the server!

What's wrong? I cannot continue :'(

Many thanks.

Here is the sample code:
import java.io.*;
import java.util.*;
import java.security.*;
import java.security.cert.*; 


public class PatoFirma {

    public static void main (String args[]) {

        try {
            PatoFirma pf=new PatoFirma ();
            pf.go();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public void go () throws Exception {
        // Provider.
        Provider pkcs11Provider = new sun.security.pkcs11.SunPKCS11("c:/cardConfig.txt");

        // Access to the smart card.
        char[] pin = "1234".toCharArray();
        KeyStore keyStore = KeyStore.getInstance("PKCS11", pkcs11Provider);
        keyStore.load(null, pin);

        // Get the first alias in the smart card.
        Enumeration aliasesEnum = keyStore.aliases();
        String alias = (String) aliasesEnum.nextElement();

        // Use this sample doc:
        byte[] doc = "pato".getBytes();

        // Sign.
        Signature signatureAlgorithm = Signature.getInstance("SHA1withRSA", pkcs11Provider);
        signatureAlgorithm.initSign((PrivateKey) keyStore.getKey(alias, null));
        signatureAlgorithm.update(doc);
        byte[] digitalSignature = signatureAlgorithm.sign();

        // Verify.
        Signature verify = Signature.getInstance("SHA1withRSA");
        verify.initVerify(keyStore.getCertificate(alias));
        verify.update(doc);
        boolean flag = verify.verify(digitalSignature);
        System.out.println("-->" + (flag ? "TRUE":"FALSE"));


    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 25 2007
Added on Jan 10 2007
3 comments
334 views