0

I need to sign a large file in java, using a MakeCert.exe (from from the Windows SDK 8.) generated DSA private key.

makecert.exe -sy 13 -sv C:\SignFile3\dsasign.pvk -pe -r -n "CN=LGS CA" C:\SignFile3\dsasign.crt

The pvk is the private key I want to sign with.

Next my complete Java code:

import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.DataInputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.security.*;
import java.security.spec.*;

class GenSig {
    public static final String PRIVATE_KEY_FILE = "dsasign.pvk";

    public static byte[] fullyReadFile(File file) throws IOException {
        DataInputStream dis = new DataInputStream(new FileInputStream(file));
        byte[] bytesOfFile = new byte[(int) file.length()];
        dis.readFully(bytesOfFile);
        dis.close();
        return bytesOfFile;
    }

    public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println("Usage: GenSig nameOfFileToSign");
        }
        else {
            try { 
                KeyFactory keyFactory = KeyFactory.getInstance("DSA");

                File myfile = new File(PRIVATE_KEY_FILE);
                byte[] decodedprivatekey = fullyReadFile(myfile);
                PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(decodedprivatekey);
                PrivateKey priv = keyFactory.generatePrivate(priKeySpec);

                Signature dsa = Signature.getInstance("SHA1withDSA", "SUN"); 
                dsa.initSign(priv);

                /* Update and sign the data */
                FileInputStream fis = new FileInputStream(args[0]);
                BufferedInputStream bufin = new BufferedInputStream(fis);
                byte[] buffer = new byte[1024];
                int len;
                while (bufin.available() != 0) {
                    len = bufin.read(buffer);
                    dsa.update(buffer, 0, len);
                };

                bufin.close();

                /* Now that all the data to be signed has been read in, generate a signature for it */
                byte[] realSig = dsa.sign();

                /* Save the signature in a file */
                FileOutputStream sigfos = new FileOutputStream("signature.binary");
                sigfos.write(realSig);
                sigfos.close();
            }
        }
        catch (Exception e) {
            System.err.println("Caught exception " + e.toString());
        }
    };
}

The error I get running it is: Caught exception java.security.spec.InvalidKeySpecException: Inappropriate key specification: IOException : DerInputStream.getLength(): lengthTag=113, too big.

bytecode77
  • 14,163
  • 30
  • 110
  • 141
Bertrand_Szoghy
  • 880
  • 1
  • 11
  • 26
  • Might help to give a stack trace for the exception. – Peter Brittain Aug 05 '15 at 22:33
  • http://stackoverflow.com/questions/2292495/what-is-the-difference-between-a-cer-pvk-and-pfx-file says PVK is a Microsoft proprietary format and NOT the same as PKCS#8. https://msdn.microsoft.com/en-us/library/windows/hardware/ff550672%28v=vs.85%29.aspx suggests suggests at least some DDK has PVK2PFX to convert PVK+SPC to PFX which is Microsoft's name for PKCS#12 which Java can read as a KeyStore from which you get the key, rather than reading directly as a key object. Warning: I don't know what DSA size MS-crypto uses, but if it's over 1024 Java7 (and 6) can't handle it you need 8. – dave_thompson_085 Aug 05 '15 at 23:18
  • Thank you Dave! I am following your advice but have run into: http://stackoverflow.com/questions/31857536/pvk2pfx-exe-error-converting-private-key-and-certificate-to-pfx-file – Bertrand_Szoghy Aug 06 '15 at 14:44
  • Meta: if you want a commenter (like me) to be aware of a response, use @name as explained in the help. I can't help with the Microsoft PVK2PFX problem, but maybe an alternative: why do need the key and cert generated by makecert? Could you use a DSA key and self-signed cert **generated by Java** with `keytool`? Java can (also) put it in a PFX which Windows will import to the "Personal" store (although I can't test use of the result). – dave_thompson_085 Aug 14 '15 at 05:17

0 Answers0