package com.hdl.photovoltaic.utils; import android.annotation.SuppressLint; import android.text.TextUtils; import com.hdl.sdk.link.common.utils.LogUtils; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import io.dcloud.common.adapter.util.CryptoProvider; /** * AES加密解密 */ public class AesUtils { private static final String SHA1PRNG = "HDLRDCENTER1985."; // SHA1PRNG 强随机种子算法 private static final String AES = "AES"; //AES 加密 private static final String CIPHERMODE = "AES/CBC/PKCS7Padding"; //AES算法/CBC模式/PKCS5Padding填充模式 /** * 解密 * * @param contentByte 待解密待字符串hexStr * @return 解密后byte[]数组 */ public static byte[] decrypt(byte[] contentByte) { try { //KEY转换 Key key = new SecretKeySpec(SHA1PRNG.getBytes(), AES); //解密 Cipher cipher = Cipher.getInstance(CIPHERMODE); IvParameterSpec ivps = new IvParameterSpec(SHA1PRNG.getBytes()); cipher.init(Cipher.DECRYPT_MODE, key, ivps); return cipher.doFinal(contentByte); } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException | InvalidAlgorithmParameterException e) { LogUtils.e(e.getMessage()); } return null; } /** * 加密 */ public static String encrypt(String key, String cleartext) { if (TextUtils.isEmpty(cleartext)) { return cleartext; } try { byte[] result = encrypt(key, cleartext.getBytes()); return parseByte2HexStr(result); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 加密 */ public static byte[] encrypt(String key, byte[] clear) throws Exception { byte[] raw = getRawKey(key.getBytes()); SecretKeySpec keySpec = new SecretKeySpec(raw, AES); Cipher cipher = Cipher.getInstance(CIPHERMODE); cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(new byte[cipher.getBlockSize()])); return cipher.doFinal(clear); } /** * 解密 */ public static String decrypt(String key, String encrypted) { if (TextUtils.isEmpty(encrypted)) { return encrypted; } try { byte[] enc = parseHexStr2Byte(encrypted); byte[] result = decrypt(key, enc); return new String(result); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 解密 */ public static byte[] decrypt(String key, byte[] encrypted) throws Exception { byte[] raw = getRawKey(key.getBytes()); SecretKeySpec keySpec = new SecretKeySpec(raw, AES); Cipher cipher = Cipher.getInstance(CIPHERMODE); cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(new byte[cipher.getBlockSize()])); return cipher.doFinal(encrypted); } /** * 处理密钥 */ @SuppressLint("DeletedProvider") public static byte[] getRawKey(byte[] seed) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance(AES); //for android SecureRandom sr = null; // 在4.2以上版本中,SecureRandom获取方式发生了改变 if (android.os.Build.VERSION.SDK_INT >= 17) { sr = SecureRandom.getInstance(SHA1PRNG, new CryptoProvider()); } else { sr = SecureRandom.getInstance(SHA1PRNG); } sr.setSeed(seed); kgen.init(128, sr); //128bits,192bits,256bits //AES中128位密钥,加密轮次为10轮;192位密钥,加密轮次为12轮;256位密钥,加密轮次为14轮。 SecretKey secretKey = kgen.generateKey(); return secretKey.getEncoded(); } /** * 将二进制转换成16进制 */ public static String parseByte2HexStr(byte[] buf) { StringBuilder sb = new StringBuilder(); 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进制转换为二进制 */ public 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; } }