DES
DES(Data Encryption Standard),即数据加密算法。是IBM公司于1975年研究成功并公开发表的。DES算法的入口参数有三个:Key、Data、Mode。
其中Key为8个字节共64位,是DES算法的工作密钥;Data也为8个字节64位,是要被加密或被解密的数据;Mode为DES的工作方式,有两种:加密或解密。
安卓端对请求Web服务器请求字符串进行加密
加密公共方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| package com.sz.kcygl.common.DESUtil; import java.security.Key; import java.security.SecureRandom; import java.security.spec.AlgorithmParameterSpec; import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.IvParameterSpec; import com.sun.org.apache.xml.internal.security.utils.Base64;
public class DESUtil { public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";
public static String encode(String key, String data) throws Exception { return encode(key, data.getBytes()); }
public static String encode(String key, byte[] data) throws Exception { try { DESKeySpec dks = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); Key secretKey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(ALGORITHM_DES); IvParameterSpec iv = new IvParameterSpec("12345678".getBytes()); AlgorithmParameterSpec paramSpec = iv; cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
byte[] bytes = cipher.doFinal(data); return Base64.encode(bytes); } catch (Exception e) { throw new Exception(e); } }
public static byte[] decode(String key, byte[] data) throws Exception { try { SecureRandom sr = new SecureRandom(); DESKeySpec dks = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); Key secretKey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(ALGORITHM_DES); IvParameterSpec iv = new IvParameterSpec("12345678".getBytes()); AlgorithmParameterSpec paramSpec = iv; cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec); return cipher.doFinal(data); } catch (Exception e) { throw new Exception(e); } }
public static String decodeValue(String key, String data) throws Exception { byte[] datas; String value = null;
datas = decode(key, Base64.decode(data));
value = new String(datas); if (value.equals("")) { throw new Exception(); } return value; }
}
|
java后台服务器
通过一个拦截器,拦截掉所有需要拦截的路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| package com.sz.kcygl.web.interceptor;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import com.sz.kcygl.common.DESUtil.MD5; import com.sz.kcygl.common.DESUtil.DESUtil; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import java.io.BufferedInputStream;
public class SignInterceptor extends HandlerInterceptorAdapter { protected final Log log = LogFactory.getLog(this.getClass());
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String requestUri = request.getRequestURI(); String contextPath = request.getContextPath(); String url = requestUri.substring(contextPath.length());
log.info("requestUri:" + requestUri); log.info("contextPath:" + contextPath); log.info("url:" + url);
StringBuffer requestData=new StringBuffer(); BufferedInputStream buf = new BufferedInputStream(request.getInputStream()); byte[] buffer=new byte[1024]; int iRead; while((iRead=buf.read(buffer))!=-1){ requestData.append(new String(buffer,0,iRead,"utf-8")); } JSONObject jsonObject = JSONObject.fromObject(requestData.toString());
String requestDES = jsonObject.getString("requestMessage"); String signvalue = jsonObject.getString("sign");
log.info("加密后的字符串:"+requestDES); log.info("MD5签名:"+signvalue);
String afterDES=""; if(StringUtils.isNotEmpty(requestDES)){ afterDES = DESUtil.decodeValue("tiananapp", requestDES); log.info("解密后请求:"+afterDES); }
MD5 md5 =new MD5(); String localSign = md5.getMD5ofStr("tiananapp"+afterDES); log.info("本地MD5签名:"+localSign); if(signvalue!=null&&signvalue.equalsIgnoreCase(localSign)){ request.setAttribute("requestMessage",afterDES); return true; } return false; }
}
|
在Spring MVC 配置文件添加拦截器配置
1 2 3 4 5 6 7 8
| <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" /> <mvc:exclude-mapping path="/front/**"/>
|