/** * Created by 磊磊tua on 2019/9/23/023. */ public class AesUtils { public static final String VIPARA = "1234567890123456"; public static final String bm = "UTF-8"; public static final String password = "1234567890123456";//password 生成秘钥的关键字 /** * 字节数组转化为大写16进制字符串 * * @param b * @return */ private static String byte2HexStr(byte[] b) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < b.length; i++) { String s = Integer.toHexString(b[i] & 0xFF); if (s.length() == 1) { sb.append("0"); } sb.append(s.toUpperCase()); } return sb.toString(); } /** * 16进制字符串转字节数组 * * @param s * @return */ private static byte[] str2ByteArray(String s) { int byteArrayLength = s.length() / 2; byte[] b = new byte[byteArrayLength]; for (int i = 0; i < byteArrayLength; i++) { byte b0 = (byte) Integer.valueOf(s.substring(i * 2, i * 2 + 2), 16) .intValue(); b[i] = b0; } return b; } /** * AES 加密 * * @param content 明文 * @param * @return */ public static String aesEncrypt(String content) { try { IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes()); SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv); byte[] encryptedData = cipher.doFinal(content.getBytes(bm)); // return new String(encryptedData,bm); return Base64.encode(encryptedData); // return byte2HexStr(encryptedData); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } return null; } /** * AES 解密 * * @param content 密文 * @param password 生成秘钥的关键字 * @return */ public static String aesDecrypt(String content, String password) { try { byte[] byteMi = Base64.decode(content); // byte[] byteMi= str2ByteArray(content); IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes()); SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key, zeroIv); byte[] decryptedData = cipher.doFinal(byteMi); return new String(decryptedData, "utf-8"); } 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(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } return null; } }
注意:在ios和后台上
VIPARA(偏移量)
password (键值)都得统一标准 来实现多终端的解密
public static final String VIPARA = ""; //注意需要16字符 public static final String password = "";//password 生成秘钥的关键字。注意需要16字符
应用项目中是关于json传输的(只要是String字符串就可以)
Map<String, String> map = new HashMap<>(); map.put("order_id", order_id); map.put("input_password", payWord); String aesStr = AesUtils.aesEncrypt(JsonUtil.getInstance().toJson(map));
磊磊tua