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);// algorithmStrbyte[] 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);// algorithmStrcipher.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" };}


更多相关文章

  1. Android(安卓)快速使用 string.xml 中的array标签
  2. Android中的MD5加密
  3. 第一章:初入Android大门(Gallery拖动相片特效)
  4. 基于Android的推箱子小游戏 源码
  5. 关于android中的Toast的用法
  6. Android学习札记48:将TextView中的指定文字转换为表情显示
  7. Activity之间传递类对象
  8. Android(安卓)build.gradle buildConfigField 配置数组
  9. Android开发便签9:在android资源文件中定义字符串数组

随机推荐

  1. Android(安卓)应用程序运行shell命令
  2. Android(安卓)Mediacodec H.265文件播放
  3. 判断android设备是否支持多点触控
  4. Calculator-Android
  5. Android(安卓)通过经纬度获取地理位置信
  6. Android学习 数据存储之_文件存储
  7. ListView多次调用getView方法
  8. [置顶] 通过SIM卡获取GPS,android基站定位
  9. android java 执行shell命令(笔记)
  10. [置顶] android adb adbd analyse