package com.future.dispatch.utils; import android.util.Log; import com.example.oktrip.netUtls.NetKitKt; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import cn.sinata.util.Base64DES; public class DES { /* * 不要写成:Cipher cipher = Cipher.getInstance("DES"); * 或:Cipher cipher = Cipher.getInstance("DES/CBC/NoPadding"); * 原因是Cipher cipher = Cipher.getInstance("DES"); * 与Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); * 等同,填充方式错误,加密的时候会得到16长度的字节数组。 * 应该写成 * Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding"); */ private static final String KEY = "ForUApp="; private static byte[] iv = {25, 24, 40, 41, 60, 68, 70, 97}; /** * 指定密钥加密 * @param encryptString 原文 * @param encryptKey 密钥 * @return 密文 * @throws Exception */ public static String encryptDES(String encryptString, String encryptKey) throws Exception { IvParameterSpec zeroIv = new IvParameterSpec(iv); SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES"); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv); byte[] encryptedData = cipher.doFinal(encryptString.getBytes()); return Base64DES.encode(encryptedData); } /** * 指定密钥进行解密 * @param decryptString 密文 * @param decryptKey 密钥 * @return 原文 * @throws Exception */ public static String decryptDES(String decryptString, String decryptKey) throws Exception { byte[] byteMi = Base64DES.decode(decryptString); IvParameterSpec zeroIv = new IvParameterSpec(iv); SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(), "DES"); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key, zeroIv); byte decryptedData[] = cipher.doFinal(byteMi); return new String(decryptedData); } /** * 默认密钥加密 * @param encryptString 原文 * @return 密文 * @throws Exception */ public static String encryptDES(String encryptString) throws Exception { IvParameterSpec zeroIv = new IvParameterSpec(iv); SecretKeySpec key = new SecretKeySpec(KEY.getBytes(), "DES"); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv); byte[] encryptedData = cipher.doFinal(encryptString.getBytes()); return Base64DES.encode(encryptedData); } /** * 默认密钥解密 * @param decryptString 密文 * @return 解密得到的原文 * @throws Exception */ public static String decryptDES(String decryptString) throws Exception { byte[] byteMi = Base64DES.decode(decryptString); IvParameterSpec zeroIv = new IvParameterSpec(iv); SecretKeySpec key = new SecretKeySpec(KEY.getBytes(), "DES"); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key, zeroIv); byte decryptedData[] = cipher.doFinal(byteMi); return new String(decryptedData, "UTF-8"); } public 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(); } public static void main(String[] args) { String plaintext = "server=/app/public/getCostSet?type=1"; try { System.out.println("明文:" + plaintext); System.out.println("密钥:" + KEY); System.out.println("DES密文:" + DES.encryptDES(plaintext)); System.out.println("DES解密"+DES.decryptDES(DES.encryptDES(plaintext))); System.out.println("URLEncoder加密"+URLEncoder.encode(DES.encryptDES(plaintext),"UTF-8")); System.out.println("URLEncoder解密"+URLDecoder.decode(URLEncoder.encode(DES.encryptDES(plaintext),"UTF-8"), "UTF-8")); } catch (Exception e) { e.printStackTrace(); } } /** * 生成签名 * @param map * @return */ public static String getSign(Map map) { String result = ""; try { List> infoIds = new ArrayList<>(map.entrySet()); // 对所有传入参数按照字段名的 ASCII 码从小到大排序(字典序) Collections.sort(infoIds, (o1, o2) -> (o1.getKey()).compareTo(o2.getKey())); // 构造签名键值对的格式 StringBuilder sb = new StringBuilder(); for (Map.Entry item : infoIds) { if (item.getKey() == null) { item.getKey(); } String key = item.getKey(); Object val = item.getValue(); if (!(val == "" || val == null)) { sb.append(key).append("=").append(val).append("&"); } } result = sb.toString(); result = result.substring(0,result.length() - 1); Log.i("getSign====", "getSign: ==="+result); Log.i("getSign====", "AppId: ==="+ NetKitKt.getAppId()); result = Base64DES.encode(HMACSHA1.HmacSHA1Encrypt(result, NetKitKt.getAppId())); //进行MD5加密 } catch (Exception e) { return null; } return result; } }