FIRM PKCS # 1 RSA Sha256WithRsa

0

I need to sign a chain to send to a financial institution by means of a WebService .

The steps are the following:

Procedure to generate the value of the signature parameter

  • Generate the original string of the query message or the output message, as the case may be.
  • Apply the signature schema procedure described in the section 8.2 RSASSA-PKCS1-v1_5 of the document to the original string of the query message or the output message, as the case may be. > RFC 3447 Public-Key Cryptography Standards (PKCS) # 1: RSA Cryptography Specifications Version 2.1.
  • Apply a base 64 filtering to the result of the previous procedure, as described in the document RFC 4648 The Base16, Base32, and Base64 Data Encodings.
  • Set the result of paragraph 3 in the "signature" parameter, to be included as part of the query parameters in the corresponding request.
  • Sample guide document

    a. The following original string of the query message is generated

    |14/12/2016 18:01:08|operador0125|ABCD1234567F8|fisica|
    

    b. The original string of the query message is followed by steps 2 and 3 of the signature process described in section 3, from which the following string is obtained:

    Htu1kKxYUPLKSDDYZ0g2lR0qH5grAOwuW3RRyCTXuJU68jD170hCmNg8m7SGgrIWh07YkRlnhcZs6zwwiHpPSCQtVKLgkMsZ0Q0ENZ9h2/M88ZiNDTrUQft/WGlNsvNKNbb5oOZQ4lM/mCvTl3Zdy7A40kpS54QqMUIEdyWkVpibriHAsuWv8KZHQBvSYm2rZ0rJBEuGs7I2IMWxgCy9yHh7ub7dyqmzWmtQL87HC7yEPgLPnNFXyj53ylS2Pv/B0tgdHfmvx3lyZ2BDJdN0ImW2JXwMlNOhzLvQF80JsJSIeNH+m5JUrjA2Ntfb3/m71obg9tfAuwU7NsPZoSJ2pg==
    
  • They give me a .cer file:

    00000100000100012095.cer
    
  • A .cve that contains:

    -----BEGIN RSA PRIVATE KEY-----
    Proc-Type: 4,ENCRYPTED
    DEK-Info: AES-128-CBC,041BEED517ABF2499A0A70247B392DC3
    
    RA3VnZHkktMcBpCa7u0+j1l56CRoKezQwMWdrU0Nevj3y0Ff+VFsuFKPDyoTmN45
    60okZEazBwI+jPF3sLbHvuQMPE2pUTrU0Y1x6cjnTMqg9FKxo/o3Za64KjIkbGde
    ............................................... más contenido...
    -----END RSA PRIVATE KEY-----
    
  • A 14-character security phrase:

    123_XXXXXXXXXX
    
  • What I have researched, to generate a signature ( PKCS ) # 1, with Sha256WithRSA , and in the end It should be passed to Base64 , of all the files, I only need the .cer , which is where I get the public key, and I only have to encrypt the original string, it is correct, but every time I execute the code it generates a different result. Is that normal?

    My code is as follows:

    byte[] encodedBytes;
    
    
    String encodedBytesStr;
    System.out.println("firmaCompuesta: " + firmaCompuesta);
    try {
        boolean useBouncyCastleProvider = true;
    
        Provider provider = null;
        if (useBouncyCastleProvider) {
            provider = new BouncyCastleProvider();
            Security.addProvider(provider);
        }
    
        // Message Digest
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    
        FileInputStream fin = new FileInputStream(FILENAME_CER);
        CertificateFactory f = CertificateFactory.getInstance("X.509");
        X509Certificate certificate = (X509Certificate) f.generateCertificate(fin);
        PublicKey publickey = certificate.getPublicKey();
    
        String hashingAlgorithm = "SHA-256";
    
        MessageDigest messageDigestProvider = null;
        if (null != provider) {
            messageDigestProvider = MessageDigest.getInstance(hashingAlgorithm, provider);
        } else {
            messageDigestProvider = MessageDigest.getInstance(hashingAlgorithm);
        }
        messageDigestProvider.update(firmaCompuesta.getBytes());
    
        byte[] hash = messageDigestProvider.digest();
    
        DigestAlgorithmIdentifierFinder hashAlgorithmFinder = new DefaultDigestAlgorithmIdentifierFinder();
        AlgorithmIdentifier hashingAlgorithmIdentifier = hashAlgorithmFinder.find(hashingAlgorithm);
    
        DigestInfo digestInfo = new DigestInfo(hashingAlgorithmIdentifier, hash);
        byte[] hashToEncrypt = digestInfo.getEncoded();
    
        // Crypto
        // You could also use "RSA/ECB/PKCS1Padding" for both the BC and SUN Providers.
        Cipher encCipher = null;
        if (null != provider) {
            encCipher = Cipher.getInstance("RSA/NONE/PKCS1Padding", provider);
        } else {
            encCipher = Cipher.getInstance("RSA");
        }
    
        //=============== JEMJ CIFRADO DESDE .CER =============
        encCipher.init(Cipher.ENCRYPT_MODE, publickey);
    
        byte[] encrypted = encCipher.doFinal(hashToEncrypt);
    
        System.out.println("\n\nHash and Encryption Output : \n\n");
        //
        encodedBytes = Base64.encodeBase64(encrypted);
        encodedBytesStr = new String(encodedBytes);
        //
        System.out.println(encodedBytesStr);
    
        
    asked by Elias Mendieta 26.07.2017 в 16:43
    source

    0 answers