Android:InvalidKeySpecException:无法识别编码的密钥规范
Android: InvalidKeySpecException: encoded key spec not recognised
当我尝试从字符串生成 RSA public 密钥时。我得到下面提到的异常。我正在尝试验证 Android 中生成的密钥(字符串,使用 Swift)。如何将字符串转换为 private/public 键?
static PublicKey getRSAPublicKeyFromString (String publicKeyPEM){
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA", "SC");
byte[] publicKeyBytes = Base64.decode(publicKeyPEM.getBytes("UTF-8"), Base64.DEFAULT);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes);
return keyFactory.generatePublic(x509KeySpec);
}catch (Exception e){
e.printStackTrace ();
}
return null;
}
异常
java.security.spec.InvalidKeySpecException: encoded key spec not recognised
at org.spongycastle.jcajce.provider.asymmetric.util.BaseKeyFactorySpi.engineGeneratePublic(BaseKeyFactorySpi.java:52)
at org.spongycastle.jcajce.provider.asymmetric.rsa.KeyFactorySpi.engineGeneratePublic(KeyFactorySpi.java:130)
at java.security.KeyFactory.generatePublic(KeyFactory.java:172)
public 输入 ios:
MIGJAoGBAOibWF15OhLJQAWpQN++UFqyG+2uLEG8PPvCajwwh6FWi6jLiZ9VN+l3JpiJTQwF/yxlSJCZCoYnljKeRBnNxpzBnuTJsyuZ4hI8ehLzDvh9nYzX/TlcZYVshJzyIVRZAbuFOCLNIltjB+Qmw8YGlxzeDSFaB6eEC1OP49hDakURAgMBAAE=
不幸的是,您拥有的是 base64 编码 PKCS#1 RSAPublicKey, which is something Java does not produce or consume. Java produces and consumes a slightly more generic and slightly more complicated X.509 SubjectPublicKeyInfo。
使用 Bouncycastle 库,下面是一些生成 RSAPublicKey 的示例代码:
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Primitive;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.RSAPublicKeySpec;
import java.util.Base64;
public class Main {
public static void main(String[] args) throws Exception {
String pubkey64 = "MIGJAoGBAOibWF15OhLJQAWpQN++UFqyG+2uLEG8PPvCajwwh6FWi6jLiZ9VN+l3JpiJTQwF/yxlSJCZCoYnljKeRBnNxpzBnuTJsyuZ4hI8ehLzDvh9nYzX/TlcZYVshJzyIVRZAbuFOCLNIltjB+Qmw8YGlxzeDSFaB6eEC1OP49hDakURAgMBAAE=";
PublicKey rsaPub = readRSAPublicKey(pubkey64);
System.out.println(rsaPub);
}
private static PublicKey readRSAPublicKey(String pubkey64) throws Exception {
byte [] derRsaPubBytes = Base64.getDecoder().decode(pubkey64);
ASN1Primitive asn1Prime = new ASN1InputStream(derRsaPubBytes).readObject();
org.bouncycastle.asn1.pkcs.RSAPublicKey rsaPub = org.bouncycastle.asn1.pkcs.RSAPublicKey.getInstance(asn1Prime);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePublic(new RSAPublicKeySpec(rsaPub.getModulus(), rsaPub.getPublicExponent()));
}
}
当我尝试从字符串生成 RSA public 密钥时。我得到下面提到的异常。我正在尝试验证 Android 中生成的密钥(字符串,使用 Swift)。如何将字符串转换为 private/public 键?
static PublicKey getRSAPublicKeyFromString (String publicKeyPEM){
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA", "SC");
byte[] publicKeyBytes = Base64.decode(publicKeyPEM.getBytes("UTF-8"), Base64.DEFAULT);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes);
return keyFactory.generatePublic(x509KeySpec);
}catch (Exception e){
e.printStackTrace ();
}
return null;
}
异常
java.security.spec.InvalidKeySpecException: encoded key spec not recognised
at org.spongycastle.jcajce.provider.asymmetric.util.BaseKeyFactorySpi.engineGeneratePublic(BaseKeyFactorySpi.java:52)
at org.spongycastle.jcajce.provider.asymmetric.rsa.KeyFactorySpi.engineGeneratePublic(KeyFactorySpi.java:130)
at java.security.KeyFactory.generatePublic(KeyFactory.java:172)
public 输入 ios:
MIGJAoGBAOibWF15OhLJQAWpQN++UFqyG+2uLEG8PPvCajwwh6FWi6jLiZ9VN+l3JpiJTQwF/yxlSJCZCoYnljKeRBnNxpzBnuTJsyuZ4hI8ehLzDvh9nYzX/TlcZYVshJzyIVRZAbuFOCLNIltjB+Qmw8YGlxzeDSFaB6eEC1OP49hDakURAgMBAAE=
不幸的是,您拥有的是 base64 编码 PKCS#1 RSAPublicKey, which is something Java does not produce or consume. Java produces and consumes a slightly more generic and slightly more complicated X.509 SubjectPublicKeyInfo。
使用 Bouncycastle 库,下面是一些生成 RSAPublicKey 的示例代码:
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Primitive;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.RSAPublicKeySpec;
import java.util.Base64;
public class Main {
public static void main(String[] args) throws Exception {
String pubkey64 = "MIGJAoGBAOibWF15OhLJQAWpQN++UFqyG+2uLEG8PPvCajwwh6FWi6jLiZ9VN+l3JpiJTQwF/yxlSJCZCoYnljKeRBnNxpzBnuTJsyuZ4hI8ehLzDvh9nYzX/TlcZYVshJzyIVRZAbuFOCLNIltjB+Qmw8YGlxzeDSFaB6eEC1OP49hDakURAgMBAAE=";
PublicKey rsaPub = readRSAPublicKey(pubkey64);
System.out.println(rsaPub);
}
private static PublicKey readRSAPublicKey(String pubkey64) throws Exception {
byte [] derRsaPubBytes = Base64.getDecoder().decode(pubkey64);
ASN1Primitive asn1Prime = new ASN1InputStream(derRsaPubBytes).readObject();
org.bouncycastle.asn1.pkcs.RSAPublicKey rsaPub = org.bouncycastle.asn1.pkcs.RSAPublicKey.getInstance(asn1Prime);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePublic(new RSAPublicKeySpec(rsaPub.getModulus(), rsaPub.getPublicExponent()));
}
}