我有一个PEM RSA私钥,并从硬线字符串中读取它。 它适用于低于4.3的Android,但不适用于Android 4.4和5.
在上面的Android 4.4中,我收到错误。
01-08 11:49:31.004: W/System.err(31454): at com.android.org.bouncycastle.jcajce.provider.asymmetric.rsa.KeyFactorySpi.engineGeneratePrivate(KeyFactorySpi.java:105)
01-08 11:49:31.004: W/System.err(31454): at java.security.KeyFactory.generatePrivate(KeyFactory.java:186)
01-08 11:49:31.004: W/System.err(31454): at org.strongswan.android.logic.CharonVpnService.getFakePrivateKey(CharonVpnService.java:1984)
01-08 11:49:31.004: W/System.err(31454): at org.strongswan.android.logic.CharonVpnService.getUserKey(CharonVpnService.java:1346)
01-08 11:49:31.004: W/System.err(31454): at dalvik.system.NativeStart.run(Native Method)
01-08 11:49:31.004: W/System.err(31454): Caused by: java.lang.ArithmeticException: error:0407006A:rsa routines:RSA_padding_check_PKCS1_type_1:block type is not 01
01-08 11:49:31.004: W/System.err(31454): at java.math.NativeBN.BN_new(Native Method)
01-08 11:49:31.004: W/System.err(31454): at java.math.BigInt.makeValid(BigInt.java:51)
01-08 11:49:31.004: W/System.err(31454): at java.math.BigInt.putBigEndianTwosComplement(BigInt.java:181)
01-08 11:49:31.004: W/System.err(31454): at java.math.BigInteger.<init>(BigInteger.java:304)
01-08 11:49:31.004: W/System.err(31454): at com.android.org.bouncycastle.asn1.DERInteger.getValue(DERInteger.java:90)
01-08 11:49:31.004: W/System.err(31454): at com.android.org.bouncycastle.asn1.pkcs.PrivateKeyInfo.<init>(PrivateKeyInfo.java:79)
01-08 11:49:31.004: W/System.err(31454): at com.android.org.bouncycastle.asn1.pkcs.PrivateKeyInfo.getInstance(PrivateKeyInfo.java:45)
01-08 11:49:31.004: W/System.err(31454): at com.android.org.bouncycastle.jcajce.provider.asymmetric.rsa.KeyFactorySpi.engineGeneratePrivate(KeyFactorySpi.java:91)
-08 11:49:31.004: W/System.err(31454): ... 4 more
01-08 11:49:31.004: E/CharonVpnService(31454): ##########################################################
01-08 11:49:31.004: W/System.err(31454): java.lang.NullPointerException
01-08 11:49:31.004: W/System.err(31454): at org.strongswan.android.logic.CharonVpnService.getFakePrivateKey(CharonVpnService.java:1998)
01-08 11:49:31.004: W/System.err(31454): at org.strongswan.android.logic.CharonVpnService.getUserKey(CharonVpnService.java:1346)
01-08 11:49:31.004: W/System.err(31454): at dalvik.system.NativeStart.run(Native Method)
这是我生成私钥的剪切代码。
private static String privateKey =
"-----BEGIN RSA PRIVATE KEY-----\n"+
"MIIEowIBAAKCAQEAl+rYuFTZkztxypSrmvUaq0haVYtiadD2XOpBdGn5RW5eQ8yn\n"+
"cTBynw7TXTH4OF31fkdqjvuDQ8rom4M5O7i6KvQkcCzlnnPgJJeN6VrBU3JfLHVb\n"+
"poFqnyzY/WhB6kkfHwFADiaFscjZuLJPIR1VR2RzCMjehlkx5rDYMle3/wYu0sSS\n"+
"SpZsERZ4wzh2TweCJfM2mskBnRA+5RADaOGnMlALRI+OFEj8Wo+ZodjUOQdoeOIe\n"+
"XiceytCE4goSrdTc/IPhT+C3G2dctOni3zbc3Dpi1Rm9bcxezOMCQb0c8Jrssf6B\n"+
"aO/CfleZVe4X8SDZD20jRvW4oTAZu1fnw9kPywIDAQABAoIBACxDUefLWSWL4WjM\n"+
"Dn/0wFN9nkxkc0UvjEigPHgF1IpFURPiEjtpiUwMzsrNn2wU/E1R05tbbvCWs1V0\n"+
"U32yIhQX1HIWssSLMJiYy9FdDw0YOuDCRPTrqjw8nfCfquzvZzDqdVZqJPQa+EzX\n"+
"LzUPzeQiQV4i4Ks2W9LCXgu1ATg9iwEguRbePKAZ4wmiHScYp9LkwHXpkJYdPAFh\n"+
"8J96aGFVs8nwEJBFMXdrhgCqlbKNJjXNxriLtqf/nBsiqzDxk8UNx5+EGfB99b1U\n"+
"v7UlcGKHwTdxz9bKrNE8S3R28dGdskG8lZinSBkNI4koA01Os41VH7nMjxFHuARr\n"+
"VixKcdkCgYEAyLAJa4+83c8H3q4Mqmepxvtvbix+YHelU3G6kLvC5Fgf3WV/+PPE\n"+
"TiDyzAfBFELGQGmi7+Nb/Y+4+j7h2rfafaS/JeBVtSABQydW5CW0KWm6KgJn98NT\n"+
"z0gDMVRSVJDrwpRjeUYThnz2M4FnOAGbc08EZi+qiTJv8jIifAH7eL0CgYEAwcm2\n"+
"7N9MUsuFn9BTbe8JjUSK82Pmqa1WMQJlmldG5w2/w0Q3TLoDqJKXs/PUOqT1aX+A\n"+
"OT2qrtlhduSLWz0rod8gyVx7gOiCnNcoIU1DZqr0U7NgSF9mdBcZYAGkdE56AkPp\n"+
"GCCK5krONjFA2H2az008qCMue8JDdIdM2Qo2hycCgYBqpCiNUJcS4rWS3bf7QRSV\n"+
"mPxwdhI9nrZqbe+ugyegATgFhK8JEjfJMHb0dlZiNs10Yu3jPVSfm6S2zX1kpPfk\n"+
"dgvgy6Gfmr8w+RGSMHrY2sbDFf47ST8a0XBo4R7SsyOkhQeK5PLxLywA2I2J1EjP\n"+
"cDitOD0NpJCaCYwLi14enQKBgGYGlVBQoBZXIJrrS4FtgFHn7GAIEMD0iZRz9poz\n"+
"qRisnHFp0NOMe7CcvVH45ZuajbplblS0qtAzkqTN3rDCm941Y0Wgc0WDJID+phoi\n"+
"W4IKL2iB4Ac6cu5b2vg45SR2zLW0m1wghgCJEdhmRgW023SY7X54R1jRBkKrmUIL\n"+
"pnmxAoGBAJ2JKeBs2sigZJB4q673orZq1OdPh4EpjO3nViq/SMF2RvOc4VY694SC\n"+
"LbLbIppDu1+cp+kUL2bf+rgH1fZZ3+L1hpxayr20VGXym+icZIDBImcgIlHWsNVF\n"+
"ti05osMkCYF9vByldjLtCQUtxowUdsf1ZpF5Zyy7tsn5Mlr8Xdxz" ;
"-----END RSA PRIVATE KEY-----";
private PrivateKey getFakePrivateKey(){
PrivateKey fakePriKey =null;
byte[] encoded;
KeyFactory kf;
String privKeyPEM = privateKey.replace("-----BEGIN RSA PRIVATE KEY-----\n", "");
privKeyPEM = privKeyPEM.replace("-----END RSA PRIVATE KEY-----", "");
try {
encoded = Base64.decode(privKeyPEM.getBytes(Charset.forName("UTF-8")), Base64.DEFAULT);
EncodedKeySpec keySpec = new PKCS8EncodedKeySpec( encoded );
kf = KeyFactory.getInstance("RSA", "BC");
fakePriKey = (PrivateKey)kf.generatePrivate ( keySpec );
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fakePriKey;
}
更强大的东西是在Android 4.3下面工作。我不知道发生了什么,也不知道上面的Android 4.4。
任何帮助将非常感谢。提前谢谢。
答案 0 :(得分:0)
使用此代码。现在,我对其进行了测试并运行良好。我在Android Studio中使用它
public static PrivateKey stringtoprivatekey(String privateKeyString)
{
try {
if (privateKeyString.contains("-----BEGIN PRIVATE KEY-----") || privateKeyString.contains("-----END PRIVATE KEY-----"))
privateKeyString = privateKeyString.replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "");
if (privateKeyString.contains("-----BEGIN RSA PRIVATE KEY-----") || privateKeyString.contains("-----END RSA PRIVATE KEY-----"))
privateKeyString = privateKeyString.replace("-----BEGIN RSA PRIVATE KEY-----", "").replace("-----END RSA PRIVATE KEY-----", "");
privateKeyString = privateKeyString.replaceAll("\\r|\\n", "");
byte[] privateKeyDER = Base64.decode(privateKeyString, Base64.DEFAULT);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyDER));
return privateKey;
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
e.printStackTrace();
return null;
}
}