微信小程序获取用户解密的Session_key 然后对 encryptedData进行解密 偶尔报错 时间长了之后会报内存溢出:
-
java.lang.OutOfMemoryError: GC overhead limit exceeded
-
-
at javax.crypto.JarVerifier.verifySingleJar(JarVerifier.java:426)
-
at javax.crypto.JarVerifier.verifyJars(JarVerifier.java:322)
-
at javax.crypto.JarVerifier.verify(JarVerifier.java:250)
-
at javax.crypto.JceSecurity.verifyProviderJar(JceSecurity.java:160)
-
at javax.crypto.JceSecurity.getVerificationResult(JceSecurity.java:186)
-
at javax.crypto.Cipher.getInstance(Cipher.java:653)
看代码是java解密的时候报错了,
然后将老代码
-
-
public byte[] aesCbcDecrypt(byte[] content, byte[] keyBytes, byte[] iv) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
-
//密钥
-
SecretKey k = new SecretKeySpec(keyBytes, "AES");
-
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
-
-
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding",
-
new BouncyCastleProvider()
-
);
-
-
cipher.init(Cipher.DECRYPT_MODE, k, new IvParameterSpec(iv));
-
//执行操作
-
return cipher.doFinal(content);
-
}
改成如下方式
-
private static Provider provider = new BouncyCastleProvider();
-
-
public byte[] aesCbcDecrypt(byte[] content, byte[] keyBytes, byte[] iv) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
-
//密钥
-
SecretKey k = new SecretKeySpec(keyBytes, "AES");
-
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
-
-
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", provider);
-
-
cipher.init(Cipher.DECRYPT_MODE, k, new IvParameterSpec(iv));
-
//执行操作
-
return cipher.doFinal(content);
-
}
用Jmeter 100并发压测 可以检测到老代码内存在上升然后短时间不会释放,新代码上升然后稳定(和知乎文章链接中的结果一致),而且新代码运行速度也更快(不用每次都new了)。
老代码cpu:
新代码cpu:
微信服务器返回的数据为什么解密失败,这个原因还是待查。同一个用户,前两次解密失败,Session_key不变,然后第三次可以成功。
实时分析java占用cpu的进程及线程,找到线程对应的java代码。
top -Hp pid
jstack pid下的线程pid
参考了微信中的文章
但是貌似并不是所有的问题代码都能在里面定位到。