com.scudata.expression.fn.string.DESEncrypt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of esproc Show documentation
Show all versions of esproc Show documentation
SPL(Structured Process Language) A programming language specially for structured data computing.
package com.scudata.expression.fn.string;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.UnsupportedEncodingException;
import com.scudata.common.Logger;
import com.scudata.common.MessageManager;
import com.scudata.common.RQException;
import com.scudata.expression.IParam;
import com.scudata.expression.fn.CharFunction;
import com.scudata.resources.EngineMessage;
/**
* des(input, key, iv; charset) DES????/????
* parament input ???룬blob????String
* parament key ???룬??????ͳһ
* parament iv ??ʼ??????????????ͳһ
* parament charset ?ַ????룬???input/key/ivΪ?ַ???ʱʹ?ã?Ĭ??Ϊutf8
* ѡ??@d ???ܣ?Ĭ??ִ?м???
* ѡ??@c CBC(Cipher Block Chaining)ģʽ??Ĭ??ʹ??ECB(Electronic Codebook)ģʽ
* ѡ??@n ???ģʽNoPadding??ȱʡPKCS5Padding
* ???أ?inputΪ?ַ???ʱ?????ַ?????Ϊ??????????ʱ???ض?????????
* @author bd
*
*/
public class DESEncrypt extends CharFunction {
// DES??Կ?㷨
private static final String KEY_ALGORITHM = "DES";
// ????/?????㷨/????ģʽ/??䷽ʽ
private static final String CIPHER_ALGORITHM_E = "DES/ECB/PKCS5Padding";
private static final String CIPHER_ALGORITHM_C = "DES/CBC/PKCS5Padding";
private static final String CIPHER_ALGORITHM_EN = "DES/ECB/NoPadding";
private static final String CIPHER_ALGORITHM_CN = "DES/CBC/NoPadding";
protected static final byte[] DEF_IV = DESEncrypt.encode("1a3b5c7d", "utf-8");
protected Object doQuery(Object[] objs) {
try {
if (objs==null || objs.length<2){
MessageManager mm = EngineMessage.get();
throw new RQException("des" + mm.getMessage("function.invalidParam"));
}
Object[] params = objs;
String charset = "utf-8";
if (param.getType() == IParam.Semicolon) {
Object obj = objs[0];
if (obj instanceof Object[]) {
params = (Object[])obj;
}
else {
params = new Object[1];
params[0] = obj;
}
//des(...;charset)?????
if (objs[1] instanceof String) {
charset = (String) objs[1];
}
else {
MessageManager mm = EngineMessage.get();
throw new RQException("des(...;charset), charset is invalid." + mm.getMessage("function.invalidParam"));
}
}
if (params == null || params.length < 2){
MessageManager mm = EngineMessage.get();
throw new RQException("des" + mm.getMessage("function.invalidParam"));
}
//boolean string = params[0] instanceof String;
byte[] inputb = DESEncrypt.encode(params[0], charset);
byte[] keyb = DESEncrypt.encode(params[1], charset);
byte[] ivb = null;
if (params.length > 2) {
ivb = DESEncrypt.encode(params[2], charset);
}
boolean dec = false;
boolean modeC = false;
boolean nopad = false;
boolean autopad = false;
boolean decStr = false;
if (option != null) {
if (option.indexOf('d')>-1) {
dec = true;
}
if (option.indexOf('c')>-1) {
modeC = true;
}
if (option.indexOf('n')>-1) {
nopad = true;
}
if (option.indexOf('a')>-1) {
autopad = true;
}
if (option.indexOf('s')>-1) {
decStr = true;
}
}
if (nopad) {
if (inputb.length % 8 != 0 ) {
if (autopad) {
MessageManager mm = EngineMessage.get();
Logger.info("des(input, key, iv; charset): " + mm.getMessage("encrypt.autopadding", "input", 8));
inputb = AESEncrypt.autoPadding(inputb, 8);
}
else {
MessageManager mm = EngineMessage.get();
throw new RQException("des(input, key, iv; charset): " + mm.getMessage("encrypt.nopadding", "input", 8));
}
}
}
if (keyb.length < 8) {
if (autopad) {
keyb = AESEncrypt.padding(keyb, 8);
}
else {
MessageManager mm = EngineMessage.get();
throw new RQException("des(input, key, iv; charset): " + mm.getMessage("encrypt.lenless", "key", 8));
}
}
Cipher ci = null;
if (modeC) {
if (ivb == null) {
MessageManager mm = EngineMessage.get();
Logger.info("des(input, key, iv; charset): " + mm.getMessage("encrypt.CBCnoiv"));
ivb = DEF_IV;
}
else if (ivb.length != 8) {
if (autopad) {
ivb = AESEncrypt.padding(keyb, 8);
}
else {
MessageManager mm = EngineMessage.get();
throw new RQException("des(input, key, iv; charset): " + mm.getMessage("encrypt.wronglen", "iv", 8));
}
}
if (nopad) {
ci = Cipher.getInstance(DESEncrypt.CIPHER_ALGORITHM_CN);
} else {
ci = Cipher.getInstance(DESEncrypt.CIPHER_ALGORITHM_C);
}
} else {
if (nopad) {
ci = Cipher.getInstance(DESEncrypt.CIPHER_ALGORITHM_EN);
} else {
ci = Cipher.getInstance(DESEncrypt.CIPHER_ALGORITHM_E);
}
}
IvParameterSpec iv = null;
if (ivb != null) {
iv = new IvParameterSpec(ivb);
}
DESKeySpec dks = new DESKeySpec(keyb);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
//??????Կ
SecretKey key = keyFactory.generateSecret(dks);
byte[] result = null;
if (dec) {
if (!modeC) {
ci.init(Cipher.DECRYPT_MODE, key);
result = ci.doFinal(inputb);
}
else {
ci.init(Cipher.DECRYPT_MODE, key, iv);
result = ci.doFinal(inputb);
}
if (decStr) {
return DESEncrypt.encode(result, charset);
}
} else {
if (!modeC) {
ci.init(Cipher.ENCRYPT_MODE, key);
result = ci.doFinal(inputb);
}
else {
ci.init(Cipher.ENCRYPT_MODE, key, iv);
result = ci.doFinal(inputb);
}
}
/*
if (string) {
if (charset.equalsIgnoreCase("unicodeescape") ||
charset.equalsIgnoreCase("unicode escape") ||
charset.equalsIgnoreCase("unicode-escape") ){
return CharEncode.utf_unescape(new String(result));
}else{
return new String(result, charset);
}
}
*/
return result;
} catch (Exception e) {
Logger.error(e.getMessage());
}
return null;
}
protected static String encode(byte[] bytes, String charset) throws UnsupportedEncodingException {
if (charset.equalsIgnoreCase("unicodeescape") ||
charset.equalsIgnoreCase("unicode escape") ||
charset.equalsIgnoreCase("unicode-escape") ){
return CharEncode.utf_unescape(new String(bytes));
}else{
return new String(bytes, charset);
}
}
protected static byte[] encode(Object obj, String encodeName) {
if (obj == null) return null;
if(obj instanceof byte[] ){
return (byte[])obj;
}else { //encode
String str = null;
if(obj instanceof String ){
str = (String)obj;
}
else {
str = obj.toString();
}
if (str != null) {
if (encodeName.equalsIgnoreCase("unicodeescape") ||
encodeName.equalsIgnoreCase("unicode escape") ||
encodeName.equalsIgnoreCase("unicode-escape") ){
str = CharEncode.utf_escape(str);
return str.getBytes();
} else {
try {
return str.getBytes(encodeName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Logger.error(e.getMessage());
return null;
}
}
}
else {
MessageManager mm = EngineMessage.get();
throw new RQException("encode error: " + mm.getMessage("function.paramTypeError"));
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy