java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0c0000b9:ASN.1 encodin

支付宝踩坑:java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0c0000b9:ASN.1 encoding routines:OPENSSL_internal:WRONG_TAG

这个问题是:Android4.1的问题,4.1之后就是加KeyFactory keyf = KeyFactory.getInstance(ALGORITHM, "BC");

PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decode(privateKey));
KeyFactory keyf = KeyFactory.getInstance(ALGORITHM, "BC");//
PrivateKey priKey = keyf.generatePrivate(priPKCS8);

java.security.Signature signature = java.security.Signature.getInstance(getAlgorithms(rsa2));

signature.initSign(priKey);
signature.update(content.getBytes(DEFAULT_CHARSET));

byte[] signed = signature.sign();
//LogUtils.logd("AliPaySignTool.sign:加密返回");
return Base64.encode(signed);
### Java解压缩失败解决方案 当遇到`java.lang.RuntimeException: Unzip failed`错误时,通常意味着在尝试读取或处理ZIP文件的过程中出现了问题。此异常可能由多种原因引起,包括但不限于文件损坏、权限不足以及路径配置不正确。 #### 文件验证与修复 确保用于解压操作的目标ZIP文件未被破坏。可以通过其他工具(如WinRAR或7-Zip)打开该文件来初步判断其完整性。如果发现文件确实存在问题,则应重新获取一份完整的副本[^1]。 #### 权限设置检查 确认应用程序具有足够的访问权限去执行涉及磁盘I/O的操作。特别是在Linux系统上运行程序时,需注意当前用户的目录写入许可情况;而在Windows环境下则要留意UAC(User Account Control)的影响范围[^4]。 #### 路径字符串规范化 对于通过网络加载资源的情况,务必保证URL地址拼接无误,并且遵循标准格式。例如,在构建`jar:`协议开头的URI实例之前,应该先调用`toExternalForm()`方法转换原始链接对象。 ```java // 正确做法示范 String jarUrlStr = "file:/path/to/my.jar"; URL url = new URL(jarUrlStr); JarURLConnection connection = (JarURLConnection)new URL("jar", "", url + "!/"); connection.setUseCaches(false); // 关闭缓存机制以减少潜在冲突 InputStream inputStream = connection.getInputStream(); ``` #### 使用第三方库简化流程 考虑到内置API可能存在局限性,引入成熟的开源项目往往能带来更稳定的表现。像Apache Commons Compress这样的组件就提供了丰富的功能接口供开发者选用,从而降低开发难度并提高代码可维护性[^2]。 ```xml <!-- Maven POM片段 --> <dependencies> <!-- ... --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.21</version> </dependency> <!-- ... --> </dependencies> ``` ```java import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipFile; public class ZipUtil { public static void extractZip(String zipFilePath, String outputDirPath){ try(ZipFile zf=new ZipFile(zipFilePath)){ Enumeration<? extends ZipArchiveEntry> entries=zf.getEntries(); while(entries.hasMoreElements()){ final var entry=(ZipArchiveEntry)entries.nextElement(); File outputFile=new File(outputDirPath,entry.getName()); if(entry.isDirectory()){ Files.createDirectories(outputFile.toPath()); }else{ Files.copy(zf.getInputStream(entry),outputFile.toPath(),StandardCopyOption.REPLACE_EXISTING); } } }catch(IOException e){ throw new RuntimeException(e.getMessage(),e); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值