Skip to content

Instantly share code, notes, and snippets.

@pfmiles
Created August 28, 2017 12:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfmiles/318edcad0edf1de590e5e0ad939d1489 to your computer and use it in GitHub Desktop.
Save pfmiles/318edcad0edf1de590e5e0ad939d1489 to your computer and use it in GitHub Desktop.
简易的字段隐藏工具,用于选择性地对原值做全部、部分加密/解密; 用于一些页面展示/表单提交的信息展示场景
package test;
import java.nio.charset.Charset;
import org.apache.commons.codec.binary.Base64;
import org.springframework.security.crypto.encrypt.BytesEncryptor;
import org.springframework.security.crypto.encrypt.Encryptors;
/**
* 简易的字段隐藏工具,用于选择性地对原值做全部、部分加密/解密; 用于一些页面展示/表单提交的信息展示场景
* @author pf-miles Mar 31, 2016 4:27:53 PM
*/
public class ValueHidingUtil {
private static final String commonPass = "`],9,cFPEO(\"WU2b";
private static final String commonSalt = "2e461d09b7c3ad3f";
private static final Charset charset = Charset.forName("UTF-8");
private static final BytesEncryptor be = Encryptors.standard(commonPass, commonSalt);
/**
* 做了信息隐藏的值的特征前缀
*/
public static final String VAL_HIDE_PREF = "_v_h_";
/**
* 加密
*/
private static String encrypt(String text) {
byte[] enc = be.encrypt(text.getBytes(charset));
return Base64.encodeBase64String(enc);
}
/**
* 解密
*/
private static String decrypt(String cipher) {
byte[] enc = Base64.decodeBase64(cipher);
return new String(be.decrypt(enc), charset);
}
/**
* 对输入值做信息隐藏加密
*/
public static String valHideEncrypt(String val) {
if (val != null && !val.startsWith(VAL_HIDE_PREF)) {
return VAL_HIDE_PREF + ValueHidingUtil.encrypt(val);
} else {
return val;
}
}
/**
* 对被隐藏的密文做解密
*/
public static String valHideDecrypt(String cipher) {
if (cipher != null && cipher.startsWith(VAL_HIDE_PREF)) {
return ValueHidingUtil.decrypt(cipher.substring(VAL_HIDE_PREF.length()));
} else {
return cipher;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment