地址参数加密(转载)
1 package p; 2 3 4 import java.security.SecureRandom; 5 import javax.crypto.Cipher; 6 import javax.crypto.KeyGenerator; 7 import javax.crypto.SecretKey; 8 9 10 11 public class AEStest { 12 13 public static void main(String[] args) throws Exception { 14 // TODO Auto-generated method stub 15 String str = "user=admin&pwd=admin"; 16 String key = "12345678"; 17 String encrytStr; 18 byte[] encrytByte; 19 20 byte[] byteRe = enCrypt(str,key); 21 22 //加密过的二进制数组转化成16进制的字符串 23 encrytStr = parseByte2HexStr(byteRe); 24 System.out.println("加密后:"+encrytStr); 25 26 //加密过的16进制的字符串转化成二进制数组 27 encrytByte = parseHexStr2Byte(encrytStr); 28 System.out.println("解密后:"+deCrypt(encrytByte,key)); 29 30 31 } 32 33 /** 34 * 加密函数 35 * @param content 加密的内容 36 * @param strKey 密钥 37 * @return 返回二进制字符数组 38 * @throws Exception 39 */ 40 public static byte[] enCrypt(String content,String strKey) throws Exception{ 41 KeyGenerator keygen; 42 SecretKey desKey; 43 Cipher c; 44 byte[] cByte; 45 String str = content; 46 47 keygen = KeyGenerator.getInstance("AES"); 48 keygen.init(128, new SecureRandom(strKey.getBytes())); 49 50 desKey = keygen.generateKey(); 51 c = Cipher.getInstance("AES"); 52 53 c.init(Cipher.ENCRYPT_MODE, desKey); 54 55 cByte = c.doFinal(str.getBytes("UTF-8")); 56 57 return cByte; 58 } 59 60 /** 解密函数 61 * @param src 加密过的二进制字符数组 62 * @param strKey 密钥 63 * @return 64 * @throws Exception 65 */ 66 public static String deCrypt (byte[] src,String strKey) throws Exception{ 67 KeyGenerator keygen; 68 SecretKey desKey; 69 Cipher c; 70 byte[] cByte; 71 72 keygen = KeyGenerator.getInstance("AES"); 73 keygen.init(128, new SecureRandom(strKey.getBytes())); 74 75 desKey = keygen.generateKey(); 76 c = Cipher.getInstance("AES"); 77 78 c.init(Cipher.DECRYPT_MODE, desKey); 79 80 81 cByte = c.doFinal(src); 82 83 return new String(cByte,"UTF-8"); 84 } 85 86 87 /**2进制转化成16进制 88 * @param buf 89 * @return 90 */ 91 public static String parseByte2HexStr(byte buf[]) { 92 StringBuffer sb = new StringBuffer(); 93 for (int i = 0; i < buf.length; i++) { 94 String hex = Integer.toHexString(buf[i] & 0xFF); 95 if (hex.length() == 1) { 96 hex = '0' + hex; 97 } 98 sb.append(hex.toUpperCase()); 99 } 100 return sb.toString(); 101 } 102 103 104 /**将16进制转换为二进制 105 * @param hexStr 106 * @return 107 */ 108 public static byte[] parseHexStr2Byte(String hexStr) { 109 if (hexStr.length() < 1) 110 return null; 111 byte[] result = new byte[hexStr.length()/2]; 112 for (int i = 0;i< hexStr.length()/2; i++) { 113 int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16); 114 int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16); 115 result[i] = (byte) (high * 16 + low); 116 } 117 return result; 118 } 119 120 121 122 }
代码主要考虑两个问题:1、加密过的字符必须能有使用Url传输 2、加密算法必须是对称算法,通过私钥可以解密
另外代码中强制把byte数组转化成String的话,会出现乱码,其次是强制转换过的字符串,再转回byte数组的时候,二进制会变化,而且二进制的位数不是16的倍数(解密算法中的输入二进制数组的大小必须是16的倍数)。因此需要二进制的相互转换
jsp页面跳转链接:<a href="<%=path %>/device/queryDetails.action?deviceid=<%=AEStest.parseByte2HexStr(AEStest.enCrypt(request.getAttribute("******").toString(),"12345678"))%>" ><s:property value="device_id" /></a>
action中在set 方法中解码:
public void setDeviceid(String ****) throws Exception {
this.deviceid = AEStest.deCrypt(AEStest.parseHexStr2Byte(****),"12345678");
}