相关原理:https://www.cnblogs.com/block2016/p/5596676.html
相关: http://blog.csdn.net/hbcui1984/article/details/5201247
package com.lvtu.csa.framework.utils;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AESUtil {
private static final String key = "iiiiii";
private static final Logger log = LoggerFactory.getLogger(InternetProtocol.class);
public static String getEncode(String text) {
try {
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] b = cipher.doFinal(text.getBytes());
String ret = new Base64().encodeAsString(b);
log.debug("转换成功的BASE64: " + ret);
String safeBase64Str = ret.replace('+', '-').replace('/', '_').replaceAll("=", "");
log.debug("去掉特殊字符如下: " + safeBase64Str);
return safeBase64Str;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getStr(String text) {
if(StringUtils.isBlank(text)){
return "";
}
String ret = text.replace('-', '+').replace('_', '/');
int mod4 = ret.length() % 4;
if (mod4 > 0) {
ret = ret + "====".substring(mod4);
}
log.debug("转换成功的BASE64: " + ret);
return ret;
}
public static String getDeCode(String text) {
if(StringUtils.isBlank(text)){
return null;
}
String ret = getStr(text);
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(Charset.defaultCharset()), "AES");
try {
byte[] decode = new Base64().decode(ret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, keySpec);
byte[] ret1 = cipher.doFinal(decode);
log.debug("解码后如下: " + new String(ret1, "utf-8"));
return new String(ret1, "utf-8");
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
try {
String text = "10.255.255.255"; // 要加密的字符串
String text1 = getEncode(text);
getDeCode(text1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
IpUtill
package com.lvtu.csa.framework.utils;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class IpUtil {
private static final Logger log = LoggerFactory.getLogger(IpUtil.class);
// 10.x.x.x/8
private static final byte SECTION_10 = 0x0A;
private static final byte SECTION_127 = (byte) 0x7F;
// 172.16.x.x/12
private static final byte SECTION_172 = (byte) 0xAC;
private static final byte SECTION_16 = (byte) 0x10;
private static final byte SECTION_31 = (byte) 0x1F;
// 192.168.x.x/16
private static final byte SECTION_192 = (byte) 0xC0;
private static final byte SECTION_168 = (byte) 0xA8;
/** 处理IP地址 **/
private static byte[] textToNumericFormatV4(String src) {
if (src.length() == 0) {
return null;
}
byte[] res = new byte[4];
String[] s = src.split("\\.", -1);
if (s.length != 4) {
return null;// 非4为IP,置空
}
long val;
try {
for (int i = 0; i < 4; i++) {
val = Integer.parseInt(s[i]);
if (val < 0 || val > 0xff)
return null;
res[i] = (byte) (val & 0xff);
}
} catch (NumberFormatException e) {
log.warn("非法IP:" + src);
return null;
}
return res;
}
/**
* 远程地址是否有效.
*
* @param remoteAddr
* 远程地址
* @return true代表远程地址有效,false代表远程地址无效
*/
public static boolean isEffective(final String remoteAddr) {
if ((null != remoteAddr) && (!"".equals(remoteAddr.trim())) && (!"unknown".equalsIgnoreCase(remoteAddr.trim()))) {
return true;
}
return false;
}
/**判断 是否是内网IP,并对传输IP进行解密 **/
public static String getDeCodeAesIp(final String remoteAddr, String aesIp, HttpServletRequest request) {
String result = "";
if (IpUtil.isInnerOrErrIp(remoteAddr)) {
result = AESUtil.getDeCode(aesIp);
if (!IpUtil.isEffective(result)) {
log.warn("非法IP,inputIp:" + aesIp + ",httpIp:" + remoteAddr + ",user-agent:" + request.getHeader("user-agent") +
",referer:" + request.getHeader("referer"));
result = remoteAddr;
}
} else {
result = remoteAddr;
}
return result;
}
/** 默认传入四位数字的处理 非数字自己处理异常 **/
private static boolean isInnerOrErrIp(String src) {
byte[] addr = textToNumericFormatV4(src);
if (addr == null || addr.length != 4) {
return true;// 非法IP
}
final byte b0 = addr[0];
final byte b1 = addr[1];
if (b0 == SECTION_127) {
return true;
}
if (b0 == SECTION_10) {
return true;
}
if (b0 == SECTION_192 && b1 == SECTION_168) {
return true;
}
if (b0 == SECTION_172 && b1 <= SECTION_31 && b1 >= SECTION_16) {
return true;
}
return false;
}
}