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!

SunMSCAPI and SHA2 signature

843811Apr 20 2010 — edited May 9 2010
Hello,
I'm trying to create a simple digital signature, using a certificate stored in the Windows-MY storage, by signature algorithm SHA256withRSA. Java version 1.6.0_18 should improve SunMSCAPI provider, which has to support the signature algorithm SHA2-RSA now. I didn't expect any problems, but I get java.security.SignatureException every time. According to stack trace, the error occurs in the native library, which is called by sun.security.mscapi.RSASignature class.
java.security.SignatureException: Byl zadán neplatný algoritmus. ( <-- This is a localized message to czech. I wasnt able to find the original message)

	at sun.security.mscapi.RSASignature.signHash(Native Method)
	at sun.security.mscapi.RSASignature.engineSign(RSASignature.java:279)
	at java.security.Signature$Delegate.engineSign(Signature.java:1128)
	at java.security.Signature.sign(Signature.java:522)
	at cz.dezadata.tools.SimpleTextSignature.main(SimpleTextSignature.java:63)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110)
Do you think this library could contain an error? Is here another way to sign algorithm RSA-SHA256 certificate obtained from the Windows storage? I attach the code snippet.
import java.io.FileOutputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Enumeration;

public class SimpleTextSignature {

    public static void main(String[] args) {
        KeyStore aKeyStore;
        PrivateKey privateKey;
        X509Certificate certificate;
        String alias = "Test Certificate";
        char[] password = "password".toCharArray();
        byte[] data = "Data to be signed!".getBytes();
        String aliasCode = null;

        try {
            aKeyStore = KeyStore.getInstance("Windows-MY", "SunMSCAPI");
            aKeyStore.load(null);
            Enumeration<String> aliases = aKeyStore.aliases();

            while (aliases.hasMoreElements()) {
                String x = aliases.nextElement();
                if (Arrays.equals(x.getBytes(), alias.getBytes())) aliasCode = x;
            }

            privateKey = (PrivateKey) aKeyStore.getKey(aliasCode, password);
            certificate = (X509Certificate) aKeyStore.getCertificate(aliasCode);

            Signature signature = Signature.getInstance("SHA256withRSA");
            signature.initSign(privateKey);
            signature.update(data);

            // Error occurs when signing
            byte[] sig = signature.sign();

            /* save the signature in a file */
            FileOutputStream sigfos = new FileOutputStream("c:/signature");
            sigfos.write(sig);
            sigfos.close();

            byte[] publicKey = certificate.getPublicKey().getEncoded();
            FileOutputStream keyfos = new FileOutputStream("c:/myPublicKey");
            keyfos.write(publicKey);
            keyfos.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 6 2010
Added on Apr 20 2010
10 comments
2,807 views