【企业微信】修复 sun.security.util 在高版本 java 中无法访问的问题,改为通过 bouncycastle 库解析私钥 by Demonese · Pull Request #3216 · binarywang/WxJava
注意:该解决方法会额外为 cp 模块引入新的依赖库,如果有更好的写法欢迎提出。
相关 issue:#3211
borisbao pushed a commit to borisbao/WxJava that referenced this pull request
目前版本是4.6.0
为什么跟pr的不一致呢
public static String decryptPriKeyByPKCS1(String encryptRandomKey, String msgAuditPriKey) throws Exception { String privateKey = msgAuditPriKey.replaceAll("(\r\n|\r|\n|\n\r)", "").replace("-----BEGIN RSA PRIVATE KEY-----", "").replace("-----END RSA PRIVATE KEY-----", "").replace(" ", ""); byte[] keyBytes = Base64.getDecoder().decode(privateKey); DerValue[] seq = (new DerInputStream(keyBytes)).getSequence(0); RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(seq[1].getBigInteger(), seq[2].getBigInteger(), seq[3].getBigInteger(), seq[4].getBigInteger(), seq[5].getBigInteger(), seq[6].getBigInteger(), seq[7].getBigInteger(), seq[8].getBigInteger()); PrivateKey priKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(2, priKey); byte[] utf8 = cipher.doFinal(Base64.getDecoder().decode(encryptRandomKey)); return new String(utf8, StandardCharsets.UTF_8); }
目前版本是4.6.0
为什么跟pr的不一致呢
public static String decryptPriKeyByPKCS1(String encryptRandomKey, String msgAuditPriKey) throws Exception { String privateKey = msgAuditPriKey.replaceAll("(\r\n|\r|\n|\n\r)", "").replace("-----BEGIN RSA PRIVATE KEY-----", "").replace("-----END RSA PRIVATE KEY-----", "").replace(" ", ""); byte[] keyBytes = Base64.getDecoder().decode(privateKey); DerValue[] seq = (new DerInputStream(keyBytes)).getSequence(0); RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(seq[1].getBigInteger(), seq[2].getBigInteger(), seq[3].getBigInteger(), seq[4].getBigInteger(), seq[5].getBigInteger(), seq[6].getBigInteger(), seq[7].getBigInteger(), seq[8].getBigInteger()); PrivateKey priKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(2, priKey); byte[] utf8 = cipher.doFinal(Base64.getDecoder().decode(encryptRandomKey)); return new String(utf8, StandardCharsets.UTF_8); }
sorry 之前用的是4.6.0