All Products
Search
Document Center

FAQ

Last Updated: Jan 09, 2020

This topic provides solutions for the common issues in the current Java SDK version. The solutions are for reference only.

Issue 1: Failed to load user.key due to illegal key length.

Error prompt:

  1. 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'}}
  2. 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
  3. 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

error1

Solution:

This error occurs because the default configurations of some JDK versions include key limits. In this case, the SDK fails to decrypt the key file when the file is loaded.

To rectify this problem, add the following code to the beginning of the main function in DemoSample.java.

  1. String errorString = "Failed to modify key-length permissions";
  2. int newMaxKeyLength;
  3. try {
  4. if ((newMaxKeyLength = Cipher.getMaxAllowedKeyLength("AES")) < 256) {
  5. System.out.println("will modify aes length");
  6. Class c = Class.forName("javax.crypto.CryptoAllPermissionCollection");
  7. Constructor con = c.getDeclaredConstructor();
  8. con.setAccessible(true);
  9. Object allPermissionCollection = con.newInstance();
  10. Field f = c.getDeclaredField("all_allowed");
  11. f.setAccessible(true);
  12. f.setBoolean(allPermissionCollection, true);
  13. c = Class.forName("javax.crypto.CryptoPermissions");
  14. con = c.getDeclaredConstructor();
  15. con.setAccessible(true);
  16. Object allPermissions = con.newInstance();
  17. f = c.getDeclaredField("perms");
  18. f.setAccessible(true);
  19. ((Map) f.get(allPermissions)).put("*", allPermissionCollection);
  20. c = Class.forName("javax.crypto.JceSecurityManager");
  21. f = c.getDeclaredField("defaultPolicy");
  22. f.setAccessible(true);
  23. Field mf = Field.class.getDeclaredField("modifiers");
  24. mf.setAccessible(true);
  25. mf.setInt(f, f.getModifiers() & ~Modifier.FINAL);
  26. f.set(null, allPermissions);
  27. newMaxKeyLength = Cipher.getMaxAllowedKeyLength("AES");
  28. }
  29. } catch (Exception e) {
  30. throw new RuntimeException(errorString, e);
  31. }
  32. if (newMaxKeyLength < 256) {
  33. throw new RuntimeException(errorString);
  34. }

Issue 2: Failed to decrypt user.key.

Error prompt:

error2

Solution:

This error occurs because the password of the account in user.key is incorrect. We recommend that you check whether the password specified for the userPassword variable in DemoSample.java is correct. If you forget the password, create another account on the BaaS platform. Then, configure a password for the account, download the latest user.key file, and retry the operation.

Issue 3: Failed to import the SDK by using the Maven file.

Error prompt:

If the system displays a message that indicates that no SDK or Netty package is found, as shown in the following figure, enter the absolute path of the package that you want to import.error

Solution:

Add the following information in the pom.xml file.

  1. <dependencies>
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>4.12</version>
  6. <scope>test</scope>
  7. </dependency>
  8. <dependency>
  9. <groupId>io.netty</groupId>
  10. <artifactId>netty-tcnative-openssl-static</artifactId>
  11. <version>2.0.17</version>
  12. <scope>system</scope>
  13. <systemPath>
  14. {libdir}/netty-tcnative-openssl-static-2.0.17-Final-mychain-{platform}-x86_64.jar
  15. </systemPath>
  16. </dependency>
  17. <dependency>
  18. <groupId>com.alipay.mychainx</groupId>
  19. <artifactId>mychainx-sdk</artifactId>
  20. <version>0.10.2.6</version>
  21. <scope>system</scope>
  22. <systemPath>{libdir}/mychainx-sdk-0.10.2.6.jar
  23. </systemPath>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.slf4j</groupId>
  27. <artifactId>slf4j-api</artifactId>
  28. <version>1.7.25</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>com.google.guava</groupId>
  32. <artifactId>guava</artifactId>
  33. <version>20.0</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.bouncycastle</groupId>
  37. <artifactId>bcpkix-jdk15on</artifactId>
  38. <version>1.60</version>
  39. </dependency>
  40. <dependency>
  41. <groupId>io.netty</groupId>
  42. <artifactId>netty-all</artifactId>
  43. <version>4.1.29.Final</version>
  44. </dependency>
  45. <dependency>
  46. <groupId>commons-io</groupId>
  47. <artifactId>commons-io</artifactId>
  48. <version>2.4</version>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.apache.commons</groupId>
  52. <artifactId>commons-lang3</artifactId>
  53. <version>3.0</version>
  54. </dependency>
  55. </dependencies>

Note: Change {libdir} to the actual directory of your SDK and {platform} to the platform type, such as windows, linux, or osx.