我尝试从给定的私钥创建RSA私钥。
这就是我想要做的事情:
String prK = "MIIBOQIBAAJAVJhUS0gLqXLOmVv2xG23oFPwim9+rVxGhLUXqKShQCvB3iRMOHn7\n" +
"/GNJumpwmnglcsNXuqAhN0OxqKGGJdtYdwIDAQABAkBP0VrXnSbDvvuIX+k59Xvo\n" +
"3sp7FDAmSoaO+H9WM9+ht5H/f/geIrSEXSIkFLnzniMwtOJ422GmkDkL1F67HuDh\n" +
"AiEAlNauDiq3RqoXufbauyPEOG9fMS2pvB+auT2XCHJhhKsCIQCRgIo7WIRZYnNp\n" +
"NRWaoppUQK3g+aM8sdeBYpbs2nwDZQIgZXIxrmxFAUAb7d+oVFdbfc/DRSTHhPbR\n" +
"oaKuF87GUwMCIFmzaATsLjO42TPMETSS+BfnBAtFe5hIf3Z5pFgC3h9tAiEAgYju\n" +
"g92fmVvE+CcRSg6at7meSEbK/Kxg7Ar4mlkXMlI=";
byte[] privateKeyBytes = prK.getBytes(StandardCharsets.UTF_8);
PrivateKey privateKey = null;
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
KeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
privateKey = keyFactory.generatePrivate(privateKeySpec);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
然而,抛出系统错误:
06-13 13:38:22.965 23257-23257/com.example.michael.dather W/System.err: java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0c0890ba:ASN.1 encoding routines:asn1_check_tlen:WRONG_TAG
06-13 13:38:22.965 23257-23257/com.example.michael.dather W/System.err: at com.android.org.conscrypt.OpenSSLKey.getPrivateKey(OpenSSLKey.java:283)
06-13 13:38:22.965 23257-23257/com.example.michael.dather W/System.err: at com.android.org.conscrypt.OpenSSLRSAKeyFactory.engineGeneratePrivate(OpenSSLRSAKeyFactory.java:64)
06-13 13:38:22.965 23257-23257/com.example.michael.dather W/System.err: at java.security.KeyFactory.generatePrivate(KeyFactory.java:187)
06-13 13:38:22.965 23257-23257/com.example.michael.dather W/System.err: at com.example.michael.dather.SECURITY.Encrypt$override.rsaDecrypt(Encrypt.java:145)
06-13 13:38:22.965 23257-23257/com.example.michael.dather W/System.err: at com.example.michael.dather.SECURITY.Encrypt$override.rsaEncrypt(Encrypt.java:117)
06-13 13:38:22.965 23257-23257/com.example.michael.dather W/System.err: at com.example.michael.dather.SECURITY.Encrypt$override.init$body(Encrypt.java:45)
06-13 13:38:22.965 23257-23257/com.example.michael.dather W/System.err: at com.example.michael.dather.SECURITY.Encrypt$override.access$dispatch(Encrypt.java)
06-13 13:38:22.965 23257-23257/com.example.michael.dather W/System.err: at com.example.michael.dather.SECURITY.Encrypt.<init>(Encrypt.java:0)
privateKey = keyFactory.generatePrivate(privateKeySpec);
有谁知道我的代码可能出错?