android 参数 加密,解密 参数提交,数据返回
package com.esnai.zhhx.mobile.utils;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class UAES {
public static void main(String[] args) {
String content = "密钥生成器是使用此类的某个!@#$%^&*()_asd";
System.out.println("加密前:" + content);
System.out.println("加密后:" + encode(content));
System.out.println("解密后:" + decode(encode(content)));
}
// 注意: 这里的password(秘钥必须是16位的)
private static final String keyBytes = "yksjks_&@ISa(#)*";
/**
* 加密
*/
public static String encode(String content) {
// 加密之后的字节数组,转成16进制的字符串形式输出
return parseByte2HexStr(encrypt(content, keyBytes));
}
/**
* 解密
*/
public static String decode(String content) {
// 解密之前,先将输入的字符串按照16进制转成二进制的字节数组,作为待解密的内容输入
byte[] b = decrypt(parseHexStr2Byte(content), keyBytes);
return new String(b);
}
private static final String algorithmStr = "AES/ECB/PKCS5Padding";
private static byte[] encrypt(String content, String password) {
try {
byte[] keyStr = getKey(password);
SecretKeySpec key = new SecretKeySpec(keyStr, "AES");
Cipher cipher = Cipher.getInstance(algorithmStr);// algorithmStr
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// ʼ
byte[] result = cipher.doFinal(byteContent);
return result; //
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
private static byte[] decrypt(byte[] content, String password) {
try {
byte[] keyStr = getKey(password);
SecretKeySpec key = new SecretKeySpec(keyStr, "AES");
Cipher cipher = Cipher.getInstance(algorithmStr);// algorithmStr
cipher.init(Cipher.DECRYPT_MODE, key);// ʼ
byte[] result = cipher.doFinal(content);
return result; //
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
private static byte[] getKey(String password) {
byte[] rByte = null;
if (password != null) {
rByte = password.getBytes();
} else {
rByte = new byte[24];
}
return rByte;
}
/**
* 将二进制转换成16进制
*
* @param buf
* @return
*/
private static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**
* 将16进制转换为二进制
*
* @param hexStr
* @return
*/
private static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
/********* md5加密 ***************************************************/
/**
* 文忠接口需要的秘钥
*/
private static final String MD5_SEED = "ZHKJHX_&@IISOa(#)S00230*_DSO";
/**
* md5加密
*
* @param verifyCode
* @return
*/
public static String getVerifyCode(String verifyCode) {
return getMD5Value(MD5_SEED + getCurrentDate() + verifyCode);
}
private static String getCurrentDate() {
StringBuffer buffer = new StringBuffer();
Calendar now = Calendar.getInstance();
String monthValue = null;
String dayValue = null;
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1;
int DAY_OF_MONTH = now.get(Calendar.DAY_OF_MONTH);
if (month < 10) {
monthValue = "0" + String.valueOf(month);
} else {
monthValue = String.valueOf(month);
}
if (DAY_OF_MONTH < 10) {
dayValue = "0" + String.valueOf(DAY_OF_MONTH);
} else {
dayValue = String.valueOf(DAY_OF_MONTH);
}
return buffer.append(year).append(monthValue).append(dayValue)
.toString();
}
private static String getMD5Value(String preValue) {
String resultString = null;
if (preValue != null && preValue.length() != 0) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
resultString = byteToString(md.digest(preValue.getBytes()));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
return resultString;
}
// 转换字节数组为16进制字串
private static String byteToString(byte[] bByte) {
StringBuffer sBuffer = new StringBuffer();
for (int i = 0; i < bByte.length; i++) {
sBuffer.append(byteToArrayString(bByte[i]));
}
return sBuffer.toString();
}
// 返回形式为数字跟字符串
private static String byteToArrayString(byte bByte) {
int iRet = bByte;
if (iRet < 0) {
iRet += 256;
}
int iD1 = iRet / 16;
int iD2 = iRet % 16;
return strDigits[iD1] + strDigits[iD2];
}
// 全局数组
private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
}