開発ボスは、朝、ケトルでテーブルからデータを抽出するときに、テーブルの特定のフィールドを暗号化するタスクを私に与えました。要件はAES暗号化を使用することです。私は最初に兄貴のBase64暗号化アルゴリズムを検索して見つけました。これがBase64とAESを使用した暗号化です。
方法1:Base64の暗号化と復号化
まず、独自の完全なプロセス図を作成します。
Javaコードの主な操作は次のとおりです。メッセージステップはデータソースステップであり、ターゲットステップはデータ出力ステップです。
暗号化および復号化コードは次のとおりです。
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;
}
方法2:AES暗号化
方法1を使用した後、上司がAES暗号化を使用すると言い、ケトル内に暗号化されたコンポーネントがあり、コンポーネントにAES暗号化アルゴリズムがあるため、このコンポーネントを使用します
。全体像は次のとおりです。
対称暗号化コンポーネントの使用方法:
キーmysqlで生成されます。
キーを生成するためのSQLは次のとおりです。selecthex( '1234567890adbcde')
これは、挿入/更新のフィールドの宣言です。