Hello everybody,
the problem is that the class asks PrivateKeySignature
java.security.RSAPrivatekey as one parameter and returns a
sun.security.mscapi.RSAPrivateKey my provider. So when I try to instantiate
the class an exception is thrown:


SEVERE: null
java.lang.ClassCastException: sun.security.mscapi.RSAPrivateKey cannot be
cast to java.security.interfaces.RSAKey
        at 
sun.security.pkcs11.P11Signature.engineInitSign(P11Signature.java:384)
        at java.security.Signature$Delegate.engineInitSign(Unknown Source)
        at java.security.Signature.initSign(Unknown Source)
        at
com.itextpdf.text.pdf.security.PrivateKeySignature.sign(PrivateKeySignature.java:114)
        at
com.itextpdf.text.pdf.security.MakeSignature.signDetached(MakeSignature.java:150)

==================================================
Meu codigo:


BouncyCastleProvider providerBC = new BouncyCastleProvider();               
                Security.addProvider(providerBC);
               
Provider pkcs11Provider = new SunPKCS11(new
ByteArrayInputStream(tokenConfiguration.getBytes()));
                Security.addProvider(pkcs11Provider);


KeyStore ks = KeyStore.getInstance("Windows-MY");

ks.load(null, null);

key = (PrivateKey)ks.getKey(alias,password);  

chain = ks.getCertificateChain(alias);  
               
                OcspClient ocspClient = new OcspClientBouncyCastle();
                TSAClient tsaClient = null;
                for (int i = 0; i < chain.length; i++) {
                    X509Certificate cert = (X509Certificate) chain[i];
                    String tsaUrl = CertificateUtil.getTSAURL(cert);
                    if (tsaUrl != null) {
                        tsaClient = new TSAClientBouncyCastle(tsaUrl);
                        break;
                    }
                }
                List<CrlClient> crlList = new ArrayList<CrlClient>();
                crlList.add(new CrlClientOnline(chain));



LoggerFactory.getInstance().setLogger(new SysoLogger());
       boolean isOK = false;
        PdfReader reader;
        try {
            filenameforsign = this.temp+filenameforsign;
            reader = new PdfReader(filenameforsign);
            String arquivoAssinado = filenameforsign.substring(0,
filenameforsign.lastIndexOf(".")) + "_signed.pdf";
            FileOutputStream fout = new FileOutputStream(arquivoAssinado);
            PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0');
            PdfSignatureAppearance sap = stp.getSignatureAppearance();
            //sap.setCrypto( key,  chain, null,
PdfSignatureAppearance.WINCER_SIGNED);
            sap.setReason(reason);
            sap.setLocation(location);
            sap.setCertificate(chain[0]);
          
         
            sap.setVisibleSignature(new Rectangle(alturaAssinatura,
margemEsquerda, alturaAssinatura-50, margemEsquerda+50),
reader.getNumberOfPages(), null); 
           
            //Creating the signature
            ExternalDigest digest = new BouncyCastleDigest();           
            ExternalSignature signature = new PrivateKeySignature( key,
DigestAlgorithms.SHA256, provider);           
            MakeSignature.signDetached( sap, digest, signature, chain, 
crlList, ocspClient,  tsaClient , 0, MakeSignature.CryptoStandard.CMS);
           
            stp.close();
            isOK = true;
        } catch (IOException ex) {
            ex.printStackTrace();
            System.out.println("Erro de IO");
        } catch (DocumentException e) {
            e.printStackTrace();
            System.out.println("Erro de Documento");
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null,
ex);
             JOptionPane.showMessageDialog(null, "Falha ao receber
parĂ¢metros."+ex.getMessage(), "ERRO", JOptionPane.INFORMATION_MESSAGE);

        }

==================================================

Alguem ja passed why or know how solve the problem?

Hugs.



--
View this message in context: 
http://itext-general.2136553.n4.nabble.com/I-m-having-trouble-signing-pdf-in-my-application-tp4658891.html
Sent from the iText - General mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Get 100% visibility into Java/.NET code with AppDynamics Lite!
It's a free troubleshooting tool designed for production.
Get down to code-level detail for bottlenecks, with <2% overhead. 
Download for free and get started troubleshooting in minutes. 
http://pubads.g.doubleclick.net/gampad/clk?id=48897031&iu=/4140/ostg.clktrk
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to