java使用ASE128對kettle加密傳輸數據,具體方法如下:
package com;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class AES128 {
private static String sKey = "6d782000a3625132";
private static String ivParameter = "66692k0da001d091";
private static AES128 instance = null;
private AES128() {
}
public static AES128 getInstance() {
if (instance == null)
instance = new AES128();
return instance;
}
// 加密
public static String encrypt(String sSrc, String encodingFormat,String sKey, String ivParameter) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
// 使用CBC模式,需要一個向量iv,可增加加密算法的強度
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes(encodingFormat));
// 此處使用BASE64做轉碼。
return new BASE64Encoder().encode(encrypted);
}
public static void main(String[] args) throws Exception {
// 需要加密的字串
String cSrc = "12026198505162431";
System.out.println("加密前的字串是:" + cSrc);
// 加密
long lStart = System.currentTimeMillis();
String enString = AES128.getInstance().encrypt(cSrc, "utf-8", sKey,ivParameter);
System.out.println("加密后的字串是:" + enString);
long lUseTime = System.currentTimeMillis() - lStart;
System.out.println("加密耗時:" + lUseTime + "毫秒");
}