Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

Sign Mexico Pedimento

See more Misc Examples

Add a signature to a Mexico pedimento file.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Asn;
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.PrivateKey;
import com.chilkatsoft.chilkat2.Rsa;
import com.chilkatsoft.chilkat2.StringBuilder;
import com.chilkatsoft.chilkat2.Xml;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // This is the contents before signing:

        // 500|1|3621|4199800|400||
        // 601|3621|4199800|400|IN|1||EKU9003173C9|EKU9003173C9FRNN09|1||
        // 507|4199800|IM|2006-7888">
        // 507|4199800|MS|2">
        // 800|4199800|1">
        // 801|M3621037.222|1|5|011|

        // This is the contents after signing

        // 500|1|3621|4199800|400||
        // 601|3621|4199800|400|IN|1||EKU9003173C9|EKU9003173C9FRNN09|1||
        // 507|4199800|IM|2006-7888">
        // 507|4199800|MS|2">
        // 800|4199800|1|fhP2Ker54D2+3+UZch23F0E72 .... 9qNSPIuAqpj524qLZbbA==|30001000000500003416|
        // 801|M3621037.222|1|5|011|

        // First create the text to be signed.
        boolean bCRLF = true;
        StringBuilder sb = new StringBuilder();
        // Use CRLF line endings.
        sb.appendLine("500|1|3621|4199800|400||", bCRLF);
        sb.appendLine("601|3621|4199800|400|IN|1||EKU9003173C9|EKU9003173C9FRNN09|1||", bCRLF);
        sb.appendLine("507|4199800|IM|2006-7888">", bCRLF);
        sb.appendLine("507|4199800|MS|2">", bCRLF);

        // Generate the MD5 hash of what we have so far..
        String md5_base64 = sb.getHash("md5", "base64", "utf-8");
        System.out.println("MD5 hash = " + md5_base64);

        // Complete the original file.
        // After signing, we'll update the BASE64_SIGNATURE and CERT_SERIAL
        sb.appendLine("800|4199800|1|BASE64_SIGNATURE|CERT_SERIAL|", bCRLF);
        sb.appendLine("801|M3621037.222|1|5|011|", bCRLF);

        // We're going to sign the MD5 hash using the private key.
        PrivateKey privKey = new PrivateKey();
        try {
            privKey.loadAnyFormatFile("qa_data/certs/mexico_test/Certificados_de_Prueba/Certificados_Pruebas/Personas Morales/EKU9003173C9_20230517223532/CSD_EKU9003173C9_20230517223903/CSD_Sucursal_1_EKU9003173C9_20230517_223850.key", "12345678a");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Generate the ASN.1 to be signed.

        // <sequence>
        //     <sequence>
        //         <oid>1.2.840.113549.2.5</oid>
        //         <null/>
        //     </sequence>
        //     <octets>SwxHfaJhG+N3pPqay6UzVA==</octets>
        // </sequence>

        Xml xml = new Xml();
        xml.setTag("sequence");
        xml.updateChildContent("sequence|oid", "1.2.840.113549.2.5");
        xml.updateChildContent("sequence|null", "");
        xml.updateChildContent("octets", md5_base64);

        Asn asn = new Asn();
        asn.loadAsnXml(xml.getXml());
        System.out.println("ASN.1 = " + asn.getEncodedDer("base64"));

        // Sign with the private key.
        Rsa rsa = new Rsa();
        try {
            rsa.usePrivateKey(privKey);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Create the opaque signature.
        BinData bdSig = new BinData();
        bdSig.appendEncoded(asn.getEncodedDer("base64"), "base64");
        try {
            rsa.signRawBd(bdSig);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // bd now contains the opaque signature, which embeds the ASN.1, which contains the MD5 hash.
        // We're going to add this line:
        // 800|4199800|1|BASE64_SIGNATURE|CERT_SERIAL_NUM|

        Cert cert = new Cert();
        try {
            cert.loadFromFile("qa_data/certs/mexico_test/Certificados_de_Prueba/Certificados_Pruebas/Personas Morales/EKU9003173C9_20230517223532/CSD_EKU9003173C9_20230517223903/CSD_Sucursal_1_EKU9003173C9_20230517_223850.cer");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        String serialHex = cert.getSerialNumber();
        // The serial in hex form looks like this:   3330303031303030303030353030303033343136
        // Decode to us-ascii.
        StringBuilder sbSerial = new StringBuilder();
        sbSerial.decodeAndAppend(serialHex, "hex", "us-ascii");
        System.out.println("serial number in us-ascii: " + sbSerial.getAsString());

        int numReplaced = sb.replace("CERT_SERIAL", sbSerial.getAsString());
        numReplaced = sb.replace("BASE64_SIGNATURE", bdSig.getEncoded("base64"));

        System.out.println("------------------------------------");
        System.out.println("Result:");
        System.out.println(sb.getAsString());
    }
}