kettle-使用 java 脚本 对某个字段进行 AES 解密

拜拜、爱过 提交于 2020-11-18 15:27:29

 

 kettle 加解密 java 脚本

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import com.sun.jersey.core.util.Base64;
import java.io.UnsupportedEncodingException;


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());
  //解密 NAME
    String sSrc_NAME = get(Fields.In, "NAME").getString(r);
	String dSrc_NAME = decrypt(sSrc_NAME);
 	get(Fields.Out, "NAME").setValue(r, dSrc_NAME);
	
  //解密 PHONE
    String sSrc_PHONE = get(Fields.In, "PHONE").getString(r);
	String dSrc_PHONE = decrypt(sSrc_PHONE);
 	get(Fields.Out, "PHONE").setValue(r, dSrc_PHONE);
	

	putRow(data.outputRowMeta, r);
    return true;

}


public static String decrypt(String sSrc) {

	try {
		SecretKeySpec skeySpec = new SecretKeySpec("AEA密钥".getBytes("UTF-8"), "AES");
		Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");//"算法/模式/补码方式"  
		cipher.init(javax.crypto.Cipher.DECRYPT_MODE, skeySpec);
		byte[] encrypted1 = Base64.decode(sSrc);
		byte[] original = cipher.doFinal(encrypted1);
		String encoded = new String(original, "UTF-8").trim();
		return encoded;
	} catch (Exception e) {
		return "";
	}
}


base64 工具类

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
            <version>1.19.1</version>
        </dependency>

 

另外附AES对称加解密工具类


import java.io.UnsupportedEncodingException;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.jersey.core.util.Base64;

/**
 * 加解密工具
 * @author Jack
 *
 */
public class CopyOfIShenZhenAESUtil {
	private static final Logger log=LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
	public static byte[] sKey;	//秘钥
	private static String charset="UTF-8"; 
	static{
		try {
			sKey="AES密钥".getBytes(charset);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		String str="3XGb50nCvqNih6mKQN56/g==";
		str=encrypt(str,sKey);
		System.out.println(str);
		str=decrypt(str,sKey);
		System.out.println(str);


	}

	public static String decrypt(String sSrc, byte[] raw) {
		if ((sSrc == null) || (sSrc.length() == 0))
			return sSrc;
		sSrc = sSrc.replaceAll("\r?\n?", "");
		try {
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");//"算法/模式/补码方式"  
			cipher.init(Cipher.DECRYPT_MODE, skeySpec);
			byte[] encrypted1 = Base64.decode(sSrc);
			byte[] original = cipher.doFinal(encrypted1);
			return new String(original, charset).trim();
		} catch (Exception e) {
			log.error("解密出错,sSrc=" + sSrc);
			log.error(e.getMessage());
		}
		return sSrc;
	}

	public static String encrypt(String sSrc, byte[] raw) {
		if ((sSrc == null) || (sSrc.length() == 0))
			return sSrc;
		try {
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"  
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
			byte[] encrypted = cipher.doFinal(sSrc.getBytes(charset));
			return new String(Base64.encode(encrypted), charset);
			//return new String(Base64.encode(encrypted));
		} catch (Exception e) {
			log.error("加密出错,sSrc=" + sSrc);
			log.error(e.getMessage());
		}
		return sSrc;
	}
}

 

 



 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!