Encryption RSA strange problem

asked 2018-10-15 09:23:59 -0600

MarcoM gravatar image

Hi Guys,

I have a strange behaviour with a copy of a pipeline. I made a duplicate and tried to change the value to make it works for a different source but the encryption stopped working for the new pipeline. So i made another one, then i made it from scratches but the problem is always there. In addition, it's seeems to have affected any new pipeline i have made.

I removed the "try/catch" on the function and i get an error saying "the provider for the RSA hasn't been found, but on the others old pipelines the encryption still works.

Can someone help me if already solved this problem?

Thank you.

PFB the code i use.

import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.security.KeyPair
import java.security.Key
import java.security.KeyPairGenerator
import java.security.NoSuchAlgorithmException
import java.security.PrivateKey
import java.security.PublicKey
import javax.crypto.Cipher

def encrypt_fields = [:]
    def PUBLIC_KEY_FILE = "/path_to_keys/rsa_pub.pem"

    def CALLERID = ''

    for (record in records) {
          try {

            if(record.value['callerid'] != null ){ 
            CALLERID =  record.value['callerid']
                encrypt_fields.put( 'callerid_encrypted', CALLERID )
            }

            ObjectInputStream inputStream = null
            inputStream = new ObjectInputStream( new FileInputStream( PUBLIC_KEY_FILE ) )
            PublicKey publicKey = (PublicKey) inputStream.readObject()
            if (!encrypt_fields.isEmpty()) {

                encrypted_map = do_encrypt( encrypt_fields, publicKey )

                encrypted_map.each { k, v -> 
                  record.value[k]=v
                }
            }
            output.write(record)
            encrypt_fields = [:]
            CALLERID = ''

          }
           catch (e) {
             def rec=sdcFunctions.createEvent( 'Error Event Capture', 1 )
             rec.value=record.value
             sdcFunctions.toEvent(rec)
            // Write a record to the error pipeline
            log.error(e.toString(), e)
            error.write(record, e.toString())
            }
        }

    /**
     * Encrypt the plain text using public key.
     **/
    def public do_encrypt(Map fields,publicKey){
        byte[] cipherText = null;
        def new_map = [:]
        try {
            final Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            for(ent in fields){

                def String plainText = ent.value;
                cipherText = cipher.doFinal(plainText.bytes)
                def final_Cipher = new String(Base64.getEncoder().encodeToString(cipherText))

              new_map.put(ent.key,final_Cipher)

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
      return new_map
    }
edit retag flag offensive close merge delete