Skip to main content

I have a use case of encrypting a file and decrypt the same using Groovy/Jython script. Can you please suggest any script.

@AmreshHalder 

  • Add the entry for the data file location in Security file. Specify the read and write folders:
// groovy source code
grant codebase "file:///groovy/script" {
permission java.util.PropertyPermission "*", "read";
permission java.io.FilePermission "/tmp/*", "read";
permission java.io.FilePermission "/tmp/output/*", "write";
};

 Add the code to Groovy Evaluator

import java.security.*
import javax.crypto.*
import javax.crypto.spec.*

def source = new File('/tmp/test.txt').text
def cipher = Cipher.getInstance('DES')
cipher.init(Cipher.ENCRYPT_MODE, SecretKeyFactory.getInstance('DES').generateSecret(new DESKeySpec('secret12'.getBytes('UTF-8'))))
def target = cipher.doFinal(source.bytes).encodeBase64()

new File('/tmp/output/output.dat').write("$target")

Specify your relevant encryption values

 

The source file will be encrypted and stored in the output folder. You can write the code to decrypt it if you need and verify if the data is decrypted correctly.

 

In your StreamSets pipeline, you can provide the source/target file name as a field.

 

 


@saleempothiwala Could you please help with decryption script as I am not sure how the key should be passed while decrypting.


@AmreshHalder 

There should be a reverse process to do the same. Give it a try, or else will try and play with it later today.


@saleempothiwala

 

I tried by replacing ENCRYPT_MODE with DECRYPT_MODE and given the source file as the encrypted file. But I am getting the error:

“com.streamsets.pipeline.api.StageException: SCRIPTING_06 - Script error while processing batch: javax.script.ScriptException: javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption”. 

 

Can you suggest what else can be changed in the code?

 


Reply