Jump to content
Rui Patronillo

Assinatura faturas (saft)

Recommended Posts

Rui Patronillo

Estou a muito pouco tempo a trabalhar em java.

Preciso de fazer a assinatura digital em documentos para serem incluídos no saft e não estou a conseguir.

Tenho o problemas das guias de transporte e assinatura de documentos resolvido em vb.net, se alguém precisar é só dizer.

Fiz isto mas não saio daqui..

Obrigado...

 private void Assinar_Documento()  {
        String FASE="1";
        String Assinatura="2010-05-18;2010-05-18T11:22:19;FAC 001/14;3.12;";
        String privateKeyString = ""+
                //-----BEGIN RSA PRIVATE KEY-----\n"+
        "---------------------------------------------------------------+\n" +
        "---------------------------------------------------------------+\n" +
        "---------------------------------------------------------------+\n" +
        "---------------------------------------------------------------+\n" +
        "---------------------------------------------------------------+\n" +
        "---------------------------------------------------------------+\n" +
        "---------------------------------------------------------------+\n" +
        "---------------------------------------------------------------+\n" +
        "---------------------------------------------------------------+\n" +
        "---------------------------------------------------------------+\n" +
        "---------------------------------------------------------------+\n" +
        "-----------------------------------";
        //"-----END RSA PRIVATE KEY-----";
        try {
            //PrivateKey key = getPrivateKey(privateKeyString);
            KeyFactory kf = KeyFactory.getInstance("RSA");
            PKCS8EncodedKeySpec keySpecPKCS8 = new PKCS8EncodedKeySpec(Base64.decode(privateKeyString,Base64.DEFAULT));
            PrivateKey key = kf.generatePrivate(keySpecPKCS8);

            byte[] message = Assinatura.getBytes(StandardCharsets.UTF_8);
            byte[] signature = rsaSign(message,key);
            FASE="2";
            String Ass = new String(signature, StandardCharsets.UTF_8);
            Msg_Info("LEN="+Ass.length(),Ass);

        }
        catch (Exception Erro){
            Msg_Error(FASE,Erro.getMessage());
        }

    }
    public byte[] rsaSign (byte[] data,PrivateKey key) {

        byte[] cipherData = null;

        try {

            Signature s = Signature.getInstance("SHA1withRSA");
            s.initSign(key);
            s.update(data);
            Log.e("s.sign()", Base64.encodeToString(s.sign(), Base64.DEFAULT));
            return s.sign();


        } catch (Exception e) {
            Msg_Error("ERRO",e.getMessage());
        }

        return cipherData;
    }
    public PrivateKey getPrivateKey(String key) {
        PrivateKey privateKey = null;
        try {
            String privateString = key;
            if(privateString!=null){
                byte[] binCpk = Base64.decode(privateString,Base64.DEFAULT);
                KeyFactory keyFactory = KeyFactory.getInstance("RSA");
                PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(binCpk);
                privateKey = keyFactory.generatePrivate(privateKeySpec);
            }
        }
        catch(Exception e){
        }
        return privateKey;
    }

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.