Untitled
unknown
java
a year ago
5.7 kB
16
Indexable
package com.goodwe.utils; import java.util.Arrays; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; /* loaded from: classes3.dex */ public class AESCrypt { private static String aesKey = "1234123412341234"; private static byte _h2b(byte b) { int i; if (b < 48 || b > 57) { byte b2 = 97; if (b < 97 || b > 102) { b2 = 65; if (b < 65 || b > 70) { return (byte) -1; } } i = (b - b2) + 10; } else { i = b - 48; } return (byte) i; } public static void setAESKey(String key) { aesKey = key; } public static byte[] GdwEncrypt(byte[] bSrc) { if (bSrc == null) { return null; } try { return EncryptCBC(padding(bSrc, 16), aesKey); } catch (Exception e) { e.printStackTrace(); return null; } } private static byte[] padding(byte[] bSrc, int blockNum) { int length = bSrc.length; if (length % blockNum == 0) { return bSrc; } byte[] bArr = new byte[((length / blockNum) + 1) * blockNum]; Arrays.fill(bArr, (byte) 0); System.arraycopy(bSrc, 0, bArr, 0, bSrc.length); return bArr; } public static byte[] GdwDecrypt(byte[] encrypted) { if (encrypted == null) { return null; } try { return DecryptCBC(encrypted, aesKey); } catch (Exception e) { e.printStackTrace(); return null; } } public static byte[] unPadding(byte[] src) { if (src == null) { return null; } int length = src.length - 1; while (length >= 0 && src[length] == 0) { length--; } int i = length + 1; byte[] bArr = new byte[i]; System.arraycopy(src, 0, bArr, 0, i); return bArr; } private static byte[] EncryptCBC(byte[] bSrc, String sKey) { if (sKey == null) { System.out.print("Key cann't be null"); return null; } else if (sKey.length() != 16) { System.out.print("Key must be 16-byte"); return null; } else { try { IvParameterSpec ivParameterSpec = new IvParameterSpec(sKey.getBytes()); SecretKeySpec secretKeySpec = new SecretKeySpec(sKey.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); cipher.init(1, secretKeySpec, ivParameterSpec); return cipher.doFinal(bSrc); } catch (Exception e) { System.out.println(e.toString()); return null; } } } private static byte[] DecryptCBC(byte[] encrypted, String sKey) { if (sKey == null) { System.out.print("Key cann't be null"); return null; } else if (sKey.length() != 16) { System.out.print("Key must be 16-byte"); return null; } else if (encrypted.length % 16 != 0) { System.out.print("encrypted string should be multiple of 16-byte"); return null; } else { try { IvParameterSpec ivParameterSpec = new IvParameterSpec(sKey.getBytes()); SecretKeySpec secretKeySpec = new SecretKeySpec(sKey.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); cipher.init(2, secretKeySpec, ivParameterSpec); try { return cipher.doFinal(encrypted); } catch (Exception e) { System.out.println(e.toString()); return null; } } catch (Exception e2) { System.out.println(e2.toString()); return null; } } } public static String byteToHexString(byte[] bytes) { if (bytes == null) { return "byteToHexString::input is null"; } String str = ""; for (int i = 0; i < bytes.length; i++) { str = str + String.format("%02X", Byte.valueOf(bytes[i])); } return str; } public static byte[] hexStringToByte(String str) { byte _hexToByte; int i; int length = str.length(); int i2 = length / 2; if (i2 * 2 != length) { i2++; _hexToByte = _hexToByte((byte) 48, (byte) str.charAt(0)); i = 1; } else { _hexToByte = _hexToByte((byte) str.charAt(0), (byte) str.charAt(1)); i = 2; } if (_hexToByte == -1) { return null; } byte[] bArr = new byte[i2]; bArr[0] = _hexToByte; for (int i3 = 1; i3 < i2; i3++) { byte _hexToByte2 = _hexToByte((byte) str.charAt(i), (byte) str.charAt(i + 1)); if (_hexToByte2 == -1) { return null; } bArr[i3] = _hexToByte2; i += 2; } return bArr; } private static byte _hexToByte(byte b1, byte b2) { byte _h2b = _h2b(b1); if (_h2b == -1) { return (byte) -1; } byte _h2b2 = _h2b(b2); if (_h2b == -1) { return (byte) -1; } return (byte) ((_h2b << 4) + _h2b2); } }
Editor is loading...
Leave a Comment