1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| weiXinCommon.decryptOfDiyIV(userLoginVo.getEncryptedData(), sessionKey, userLoginVo.getIv());
private static final String KEY_ALGORITHM = "AES"; private static final String ALGORITHM_STR = "AES/CBC/PKCS7Padding"; private static Key key; private static Cipher cipher;
public byte[] decryptOfDiyIV(String encryptedData, String sessionKeyB64, String ivs) { byte[] encryptedDataByte = Base64.decode(encryptedData); byte[] sessionKeyB64Byte = Base64.decode(sessionKeyB64); byte[] ivsByte = Base64.decode(ivs); byte[] encryptedText = null; init(sessionKeyB64Byte); try { cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(ivsByte)); encryptedText = cipher.doFinal(encryptedDataByte); } catch (Exception e) { e.printStackTrace(); } return encryptedText; }
private void init(byte[] keyBytes) { int base = 16; if (keyBytes.length % base != 0) { int groups = keyBytes.length / base + (keyBytes.length % base != 0 ? 1 : 0); byte[] temp = new byte[groups * base]; Arrays.fill(temp, (byte) 0); System.arraycopy(keyBytes, 0, temp, 0, keyBytes.length); keyBytes = temp; } Security.addProvider(new BouncyCastleProvider()); key = new SecretKeySpec(keyBytes, KEY_ALGORITHM); try { cipher = Cipher.getInstance(ALGORITHM_STR, "BC"); } catch (Exception e) { e.printStackTrace(); } }
|