@Test public void testDecryptAnotherWay() { String encryptedData = "CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZMQmRzooG2xrDcvSnxIMXFufNstNGTyaGS9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+3hVbJSRgv+4lGOETKUQz6OYStslQ142dNCuabNPGBzlooOmB231qMM85d2/fV6ChevvXvQP8Hkue1poOFtnEtpyxVLW1zAo6/1Xx1COxFvrc2d7UL/lmHInNlxuacJXwu0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn/Hz7saL8xz+W String ivStr = "r7BXXKkLb8qrSNn05n0qiA=="; String sessionKey = "tiihtNczf5v6AKRyjwEUhQ=="; assertThat(WxMaCryptUtils.decrypt(sessionKey, encryptedData, ivStr)) .isEqualTo(WxMaCryptUtils.decryptAnotherWay(sessionKey, encryptedData, ivStr)); }
public static String decryptAnotherWay(String sessionKey, String encryptedData, String ivStr) { byte[] keyBytes = Base64.decodeBase64(sessionKey.getBytes(UTF_8)); 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 key = new SecretKeySpec(keyBytes, "AES"); try { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(Base64.decodeBase64(ivStr.getBytes(UTF_8)))); return new String(cipher.doFinal(Base64.decodeBase64(encryptedData.getBytes(UTF_8))), UTF_8); } catch (Exception e) { throw new RuntimeException("AES解密失败！", e); } }
WxMaCryptUtils extends me.chanjar.weixin.common.util.crypto.WxCryptUtil { public static String decryptAnotherWay(String sessionKey, String encryptedData, String ivStr) { byte[] keyBytes = Base64.decodeBase64(sessionKey.getBytes(UTF_8)); 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 key = new SecretKeySpec(keyBytes, "AES"); try { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(Base64.decodeBase64(ivStr.getBytes(UTF_8)))); return new String(cipher.doFinal(Base64.decodeBase64(encryptedData.getBytes(UTF_8))), UTF_8); } catch (Exception e) { throw new RuntimeException("AES解密失败！", e); } } }
WxMaCryptUtils extends me.chanjar.weixin.common.util.crypto.WxCryptUtil { public static String decryptAnotherWay(String sessionKey, String encryptedData, String ivStr) { byte[] keyBytes = Base64.decodeBase64(sessionKey.getBytes(UTF_8)); 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 key = new SecretKeySpec(keyBytes, "AES"); try { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(Base64.decodeBase64(ivStr.getBytes(UTF_8)))); return new String(cipher.doFinal(Base64.decodeBase64(encryptedData.getBytes(UTF_8))), UTF_8); } catch (Exception e) { throw new RuntimeException("AES解密失败！", e); } } WxMaCryptUtils(WxMaConfig config); }
WxMaCryptUtils extends me.chanjar.weixin.common.util.crypto.WxCryptUtil { public static String decryptAnotherWay(String sessionKey, String encryptedData, String ivStr) { byte[] keyBytes = Base64.decodeBase64(sessionKey.getBytes(UTF_8)); 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 key = new SecretKeySpec(keyBytes, "AES"); try { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(Base64.decodeBase64(ivStr.getBytes(UTF_8)))); return new String(cipher.doFinal(Base64.decodeBase64(encryptedData.getBytes(UTF_8))), UTF_8); } catch (Exception e) { throw new RuntimeException("AES解密失败！", e); } } WxMaCryptUtils(WxMaConfig config); static String decrypt(String sessionKey, String encryptedData, String ivStr); static String decryptAnotherWay(String sessionKey, String encryptedData, String ivStr); }
WxMaCryptUtils extends me.chanjar.weixin.common.util.crypto.WxCryptUtil { public static String decryptAnotherWay(String sessionKey, String encryptedData, String ivStr) { byte[] keyBytes = Base64.decodeBase64(sessionKey.getBytes(UTF_8)); 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 key = new SecretKeySpec(keyBytes, "AES"); try { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(Base64.decodeBase64(ivStr.getBytes(UTF_8)))); return new String(cipher.doFinal(Base64.decodeBase64(encryptedData.getBytes(UTF_8))), UTF_8); } catch (Exception e) { throw new RuntimeException("AES解密失败！", e); } } WxMaCryptUtils(WxMaConfig config); static String decrypt(String sessionKey, String encryptedData, String ivStr); static String decryptAnotherWay(String sessionKey, String encryptedData, String ivStr);  }