package com.huawei.secure.android.common.encrypt.aes; import android.text.TextUtils; import com.huawei.secure.android.common.encrypt.utils.EncryptUtil; import com.huawei.secure.android.common.encrypt.utils.HexUtil; import com.huawei.secure.android.common.encrypt.utils.b; import java.io.UnsupportedEncodingException; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public final class AesCbc { private static final String a = "security:"; private static final String b = "AES/CBC/PKCS5Padding"; private static final String c = "AES"; private static final String d = "AesCbc"; private static final String e = ""; private static final int f = 16; private static final int g = 16; private AesCbc() { } private static byte[] a(String str, byte[] bArr, byte[] bArr2) { if (TextUtils.isEmpty(str)) { b.b(d, "cbc encrypt param is not right"); return new byte[0]; } try { return encrypt(str.getBytes("UTF-8"), bArr, bArr2); } catch (UnsupportedEncodingException e2) { String str2 = d; b.b(str2, " cbc encrypt data error" + e2.getMessage()); return new byte[0]; } } private static byte[] b(byte[] bArr) { byte[] bArr2 = new byte[(bArr.length - 16)]; System.arraycopy(bArr, 16, bArr2, 0, bArr.length - 16); return bArr2; } static byte[] d(byte[] bArr) { String str; try { str = new String(bArr, "UTF-8"); } catch (UnsupportedEncodingException e2) { String str2 = d; b.b(str2, "stripCryptHead: exception : " + e2.getMessage()); str = ""; } if (!str.startsWith(a)) { return new byte[0]; } if (bArr.length <= 9) { return new byte[0]; } int length = bArr.length - 9; byte[] bArr2 = new byte[length]; System.arraycopy(bArr, 9, bArr2, 0, length); return bArr2; } public static String decrypt(String str, String str2) { if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) { b.b(d, "content or key is null"); return ""; } byte[] hexStr2ByteArray = HexUtil.hexStr2ByteArray(str2); if (hexStr2ByteArray.length >= 16) { return decrypt(str, hexStr2ByteArray); } b.b(d, "key length is not right"); return ""; } public static String decryptWithCryptHead(String str, byte[] bArr) { if (!TextUtils.isEmpty(str) && bArr != null && bArr.length >= 16) { String c2 = c(str); if ("".equals(c2)) { return ""; } int indexOf = c2.indexOf(58); if (indexOf >= 0) { return decrypt(HexUtil.byteArray2HexStr(HexUtil.hexStr2ByteArray(c2.substring(indexOf + 1))), bArr, HexUtil.hexStr2ByteArray(c2.substring(0, indexOf))); } b.b(d, " cbc cipherText data missing colon"); } return ""; } public static byte[] decryptWithCryptHeadReturnByte(byte[] bArr, byte[] bArr2) { if (bArr == null || bArr2 == null || bArr2.length < 16) { return new byte[0]; } byte[] d2 = d(bArr); if (d2.length == 0) { return new byte[0]; } int a2 = a(d2); if (a2 >= 0) { byte[] copyOf = Arrays.copyOf(d2, a2); int length = (d2.length - copyOf.length) - 1; byte[] bArr3 = new byte[length]; System.arraycopy(d2, a2 + 1, bArr3, 0, length); return decrypt(bArr3, bArr2, copyOf); } b.b(d, " cbc cipherText data missing colon"); return new byte[0]; } public static String encrypt(String str, String str2) { if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) { b.b(d, "cbc encrypt param is not right"); return ""; } byte[] hexStr2ByteArray = HexUtil.hexStr2ByteArray(str2); if (hexStr2ByteArray.length >= 16) { return encrypt(str, hexStr2ByteArray); } b.b(d, "key length is not right"); return ""; } private static byte[] c(byte[] bArr) { byte[] bArr2 = new byte[16]; System.arraycopy(bArr, 0, bArr2, 0, 16); return bArr2; } private static String b(String str) { if (TextUtils.isEmpty(str)) { return ""; } try { return str.substring(6, 12) + str.substring(16, 26) + str.substring(32, 48); } catch (Exception e2) { String str2 = d; b.b(str2, "getIv exception : " + e2.getMessage()); return ""; } } static String c(String str) { if (!TextUtils.isEmpty(str) && str.indexOf(a) != -1) { return str.substring(9); } return ""; } private static String a(String str, String str2) { if (!TextUtils.isEmpty(str) && !TextUtils.isEmpty(str2)) { try { return str2.substring(0, 6) + str.substring(0, 6) + str2.substring(6, 10) + str.substring(6, 16) + str2.substring(10, 16) + str.substring(16) + str2.substring(16); } catch (Exception e2) { String str3 = d; b.b(str3, "mix exception: " + e2.getMessage()); } } return ""; } public static String decrypt(String str, byte[] bArr) { if (!TextUtils.isEmpty(str) && bArr != null && bArr.length >= 16) { String b2 = b(str); String a2 = a(str); if (!TextUtils.isEmpty(b2) && !TextUtils.isEmpty(a2)) { return decrypt(a2, bArr, HexUtil.hexStr2ByteArray(b2)); } b.b(d, "ivParameter or encrypedWord is null"); } return ""; } public static String encrypt(String str, byte[] bArr) { if (TextUtils.isEmpty(str) || bArr == null || bArr.length < 16) { b.b(d, "cbc encrypt param is not right"); return ""; } byte[] generateSecureRandom = EncryptUtil.generateSecureRandom(16); byte[] a2 = a(str, bArr, generateSecureRandom); if (a2 == null || a2.length == 0) { return ""; } return a(HexUtil.byteArray2HexStr(generateSecureRandom), HexUtil.byteArray2HexStr(a2)); } public static String decryptWithCryptHead(byte[] bArr, byte[] bArr2) { try { return new String(decryptWithCryptHeadReturnByte(bArr, bArr2), "UTF-8"); } catch (UnsupportedEncodingException unused) { b.b(d, "decryptWithCryptHead UnsupportedEncodingException "); return ""; } } public static String decrypt(String str, String str2, String str3) { if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2) || TextUtils.isEmpty(str3)) { b.b(d, "cbc decrypt param is not right"); return ""; } byte[] hexStr2ByteArray = HexUtil.hexStr2ByteArray(str2); byte[] hexStr2ByteArray2 = HexUtil.hexStr2ByteArray(str3); if (hexStr2ByteArray.length >= 16 && hexStr2ByteArray2.length >= 16) { return decrypt(str, hexStr2ByteArray, hexStr2ByteArray2); } b.b(d, "key length or ivParameter is not right"); return ""; } public static String encrypt(String str, String str2, String str3) { if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2) || TextUtils.isEmpty(str3)) { b.b(d, "cbc encrypt param is not right"); return ""; } byte[] hexStr2ByteArray = HexUtil.hexStr2ByteArray(str2); byte[] hexStr2ByteArray2 = HexUtil.hexStr2ByteArray(str3); if (hexStr2ByteArray.length >= 16 && hexStr2ByteArray2.length >= 16) { return encrypt(str, hexStr2ByteArray, hexStr2ByteArray2); } b.b(d, "key length or ivParameter length is not right"); return ""; } private static byte[] a(byte[] bArr, byte[] bArr2) { byte[] bArr3 = new byte[(bArr.length + bArr2.length)]; System.arraycopy(bArr, 0, bArr3, 0, bArr.length); System.arraycopy(bArr2, 0, bArr3, bArr.length, bArr2.length); return bArr3; } private static String a(String str) { if (TextUtils.isEmpty(str)) { return ""; } try { return str.substring(0, 6) + str.substring(12, 16) + str.substring(26, 32) + str.substring(48); } catch (Exception e2) { String str2 = d; b.b(str2, "get encryptword exception : " + e2.getMessage()); return ""; } } public static String encrypt(String str, byte[] bArr, byte[] bArr2) { if (!TextUtils.isEmpty(str) && bArr != null && bArr.length >= 16 && bArr2 != null && bArr2.length >= 16) { return HexUtil.byteArray2HexStr(a(str, bArr, bArr2)); } b.b(d, "cbc encrypt param is not right"); return ""; } public static String decrypt(String str, byte[] bArr, byte[] bArr2) { if (TextUtils.isEmpty(str) || bArr == null || bArr.length < 16 || bArr2 == null || bArr2.length < 16) { b.b(d, "cbc decrypt param is not right"); return ""; } try { return new String(decrypt(HexUtil.hexStr2ByteArray(str), bArr, bArr2), "UTF-8"); } catch (UnsupportedEncodingException e2) { String str2 = d; b.b(str2, " cbc decrypt data error" + e2.getMessage()); return ""; } } private static int a(byte[] bArr) { return bArr[16] == 58 ? 16 : -1; } public static byte[] encrypt(byte[] bArr, byte[] bArr2, byte[] bArr3) { if (bArr == null || bArr.length == 0 || bArr2 == null || bArr2.length < 16 || bArr3 == null || bArr3.length < 16) { b.b(d, "cbc encrypt param is not right"); return new byte[0]; } SecretKeySpec secretKeySpec = new SecretKeySpec(bArr2, c); try { Cipher instance = Cipher.getInstance(b); instance.init(1, secretKeySpec, new IvParameterSpec(bArr3)); return instance.doFinal(bArr); } catch (NoSuchAlgorithmException e2) { String str = d; b.b(str, "NoSuchAlgorithmException: " + e2.getMessage()); return new byte[0]; } catch (NoSuchPaddingException e3) { String str2 = d; b.b(str2, "NoSuchPaddingException: " + e3.getMessage()); return new byte[0]; } catch (InvalidKeyException e4) { String str3 = d; b.b(str3, "InvalidKeyException: " + e4.getMessage()); return new byte[0]; } catch (InvalidAlgorithmParameterException e5) { String str4 = d; b.b(str4, "InvalidAlgorithmParameterException: " + e5.getMessage()); return new byte[0]; } catch (IllegalBlockSizeException e6) { String str5 = d; b.b(str5, "IllegalBlockSizeException: " + e6.getMessage()); return new byte[0]; } catch (BadPaddingException e7) { String str6 = d; b.b(str6, "BadPaddingException: " + e7.getMessage()); return new byte[0]; } } public static byte[] decrypt(byte[] bArr, byte[] bArr2, byte[] bArr3) { if (bArr == null || bArr.length == 0 || bArr2 == null || bArr2.length < 16 || bArr3 == null || bArr3.length < 16) { b.b(d, "cbc decrypt param is not right"); return new byte[0]; } SecretKeySpec secretKeySpec = new SecretKeySpec(bArr2, c); try { Cipher instance = Cipher.getInstance(b); instance.init(2, secretKeySpec, new IvParameterSpec(bArr3)); return instance.doFinal(bArr); } catch (NoSuchAlgorithmException e2) { String str = d; b.b(str, "NoSuchAlgorithmException: " + e2.getMessage()); return new byte[0]; } catch (NoSuchPaddingException e3) { String str2 = d; b.b(str2, "NoSuchPaddingException: " + e3.getMessage()); return new byte[0]; } catch (InvalidKeyException e4) { String str3 = d; b.b(str3, "InvalidKeyException: " + e4.getMessage()); return new byte[0]; } catch (InvalidAlgorithmParameterException e5) { String str4 = d; b.b(str4, "InvalidAlgorithmParameterException: " + e5.getMessage()); return new byte[0]; } catch (IllegalBlockSizeException e6) { String str5 = d; b.b(str5, "IllegalBlockSizeException: " + e6.getMessage()); return new byte[0]; } catch (BadPaddingException e7) { String str6 = d; b.b(str6, "BadPaddingException: " + e7.getMessage()); return new byte[0]; } } public static byte[] encrypt(byte[] bArr, byte[] bArr2) { byte[] generateSecureRandom = EncryptUtil.generateSecureRandom(16); return a(generateSecureRandom, encrypt(bArr, bArr2, generateSecureRandom)); } public static byte[] decrypt(byte[] bArr, byte[] bArr2) { return decrypt(b(bArr), bArr2, c(bArr)); } }