更新时间:2019-11-19 19:29
本文针对当前版本 Java SDK 的常见问题提供解决方法以供参考。
运行错误提示:
09:49:40.623 [main] ERROR com.github.lyd.msg.provider.DemoSample - unable to read private key,wrong password? errorCode :{MychainSdkErrorCodeEnum{errorCode='30001', errorDesc='sdk invalid private key'}}
09:49:40.626 [main] ERROR com.github.lyd.msg.provider.DemoSample - unable to read encrypted data: 1.2.840.113549.1.12.1.3 not available: Illegal key size or default parameters
Exception in thread "main" com.alipay.mychain.sdk.exceptions.MychainSdkException: unable to read encrypted data: 1.2.840.113549.1.12.1.3 not available: Illegal key size or default parameters
解决方法:
出现此错误的原因是个别 JDK 的默认配置限制了 key 的长度,因此 SDK 在加载 key 文件时解密失败。
可在 DemoSample.java
中 main
函数最开始位置,增加一段代码来进行处理,如下所示。
String errorString = "Failed to modify key-length permissions";
int newMaxKeyLength;
try {
if ((newMaxKeyLength = Cipher.getMaxAllowedKeyLength("AES")) < 256) {
System.out.println("will modify aes length");
Class c = Class.forName("javax.crypto.CryptoAllPermissionCollection");
Constructor con = c.getDeclaredConstructor();
con.setAccessible(true);
Object allPermissionCollection = con.newInstance();
Field f = c.getDeclaredField("all_allowed");
f.setAccessible(true);
f.setBoolean(allPermissionCollection, true);
c = Class.forName("javax.crypto.CryptoPermissions");
con = c.getDeclaredConstructor();
con.setAccessible(true);
Object allPermissions = con.newInstance();
f = c.getDeclaredField("perms");
f.setAccessible(true);
((Map) f.get(allPermissions)).put("*", allPermissionCollection);
c = Class.forName("javax.crypto.JceSecurityManager");
f = c.getDeclaredField("defaultPolicy");
f.setAccessible(true);
Field mf = Field.class.getDeclaredField("modifiers");
mf.setAccessible(true);
mf.setInt(f, f.getModifiers() & ~Modifier.FINAL);
f.set(null, allPermissions);
newMaxKeyLength = Cipher.getMaxAllowedKeyLength("AES");
}
} catch (Exception e) {
throw new RuntimeException(errorString, e);
}
if (newMaxKeyLength < 256) {
throw new RuntimeException(errorString);
}
运行错误提示:
解决方法:
此错误通常是因账户 user.key
的密码错误导致,建议检查 DemoSample.java
中是否准确配置了 userPassword
变量,设定正确密码。如果忘记密码,可前往 BaaS 平台重新创建一个新的账户,并配置密码、下载新的 user.key
文件,然后重试。
运行错误提示:
如果提示找不到sdk或netty包,例如下图所示,请尝试手动引入包的绝对路径。
解决方法:
在pom.xml
文件中添加:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-tcnative-openssl-static</artifactId>
<version>2.0.17</version>
<scope>system</scope>
<systemPath>
{libdir}/netty-tcnative-openssl-static-2.0.17-Final-mychain-{platform}-x86_64.jar
</systemPath>
</dependency>
<dependency>
<groupId>com.alipay.mychainx</groupId>
<artifactId>mychainx-sdk</artifactId>
<version>0.10.2.6</version>
<scope>system</scope>
<systemPath>{libdir}/mychainx-sdk-0.10.2.6.jar
</systemPath>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.60</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.29.Final</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
注意:上述 {libdir} 请修改为您sdk文件的真实路径;{platform} 请修改为您的平台类型,如windows、linux或osx。
在文档使用中是否遇到以下问题
更多建议
匿名提交