Kettle uses java script to encrypt and decrypt data

The development boss gave me a task in the morning to encrypt a field in a table when extracting data from a table with a kettle. The requirement is to use AES encryption. I first searched and found a big brother's Base64 encryption algorithm. Here is the encryption using Base64 and AES.

Method 1: Base64 encryption and decryption

First put up your own complete process diagram:

Insert picture description here
Here are the main operations in the java code. The message step is the data source step, and the target step is the data output step.

Insert picture description here
The encryption and decryption code is as follows:

import java.util.Base64;

import java.util.*;
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    
    
  if (first) {
    
    
    first = false;

  }

  Object[] r = getRow();

  if (r == null) {
    
    
    setOutputDone();
    return false;
  }

    r = createOutputRow(r, data.outputRowMeta.size());
  //加密
    String foobar1 = get(Fields.In, "NAME").getString(r);
  byte[] bytes = foobar1.getBytes();
  String encoded = Base64.getEncoder().encodeToString(bytes);
  get(Fields.Out, "NAME").setValue(r, encoded);
  //String foobar2 = get(Fields.In, "IC_CARD").getString(r);
  //byte[] bytes2 = foobar2.getBytes();
  //String encoded1 = Base64.getEncoder().encodeToString(bytes2);
  //get(Fields.Out, "IC_CARD").setValue(r, encoded1);
   //解密(加密解密我是分了两张表,加密的内容在表一,解密的内容是从把表一的内动解密到表二,所以这样写解密的代码)
   //String foobar1 = get(Fields.In, "NAME").getString(r);
   //byte[] decoded = Base64.getDecoder().decode(foobar1);
  //String decodeStr = new String(decoded);
  //get(Fields.Out, "NAME").setValue(r, decodeStr);
  //String foobar2 = get(Fields.In, "IC_CARD").getString(r);
    //byte[] decoded2 = Base64.getDecoder().decode(foobar2);
  //String decodeStr2 = new String(decoded2);
  //get(Fields.Out, "IC_CARD").setValue(r, decodeStr2);
  putRow(data.outputRowMeta, r);

  return true;
}

Method 2: AES encryption

After using method 1, the boss said to use AES encryption, and then there is an encrypted component in the kettle, and the component has an AES encryption algorithm, so use this component
. The complete picture is as follows: How to use the
Insert picture description here
symmetric encryption component:
Insert picture description here
the key To be generated in mysql.
The sql to generate the key is: select hex('1234567890adbcde')

This is the declaration of the field in the insert/update
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_39040527/article/details/106813294