avere una stringa di essere inviato dal nel formato di seguito:Come si fa a convertire una stringa da PEM Der formato
-----BEGIN RSA PUBLIC KEY-----
MIGHAoGBANAahj75ZIz9nXqW2H83nGcUao4wNyYZ9Z1kiNTUYQl7ob/RBmDzs5rY
mUahXAg0qyS7+a55eU/csShf5ATGzAXv+DDPcz8HrSTcHMEFpuyYooX6PrIZ07Ma
XtsJ2J4mhlySI5uOZVRDoaFY53MPQx5gud2quDz759IN/0gnDEEVAgED
-----END RSA PUBLIC KEY-----
Come faccio a costruire un oggetto PublicKey da questa stringa? hanno provato il sotto Rimuovere l'intestazione e piè di pagina e base64 decodificare il buffer
public static PublicKey getFromString(String keystr) throws Exception
{
//String S1= asciiToHex(keystr);
byte[] keyBytes = new sun.misc.BASE64Decoder().decodeBuffer(keystr);
X509EncodedKeySpec spec =
new X509EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePublic(spec);
}
Questo fallisce sia come formato di chiave valida o otterrà sotto l'errore
java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: algid parse error, not a sequence
at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:188)
at java.security.KeyFactory.generatePublic(KeyFactory.java:304)
at PublicKeyReader.getFromString(PublicKeyReader.java:30)
at Tst.main(Tst.java:36)
il tasto viene generato thro il API di openSSL PEM_write_bio_RSAPublicKey(bio, rsa);
hanno usato il link .Ma non farà il converstion a der format – MSSV
Nota che quello che stai cercando di fare non è realmente una "conversione in DER". La conversione in DER è solo la decodifica di ciò che è base64 qui e l'output come una sequenza di byte. Stai cercando di decodificare la struttura ASN.1. – Bruno