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中限制EditText最大输入字节数
  2. android 读取资源字符串的 方法
  3. Android开发便签9:在android资源文件中定义字符串数组
  4. Android build.gradle buildConfigField 配置数组
  5. Android下如何计算要显示的字符串所占的宽度和高度
  6. Android期末项目(一)—— 解析二维数组对象
  7. Java 字节码编译为Dex,d8比dx更好用!
  8. [置顶] android中使用jni对字符串加解密实现分析
  9. android 复制字符串到剪贴板

随机推荐

  1. Android Studio 安装 Emulator: PANIC: C
  2. Android - 小功能 - 传感器之重力传感器
  3. Lua学习 1) —— Android调用变量取值与
  4. Lottie 浅析
  5. Android开发工具Android Studio安装教程
  6. Android 10 适配攻略
  7. 深入android数据库操作
  8. 使用Flutter_Boost进行混合开发-Android
  9. 手机侦探 奇迹 (android)
  10. Firefly-RK3288 Android 5.1 HDMI输出4K