Je suis en train de créer une instance PrivateKey dans une application Android à partir d'un fichier pem pour décrypter certaines données mais je reçois l'erreur suivante:(Android/Java) Création d'une instance clé privée RSA
java.lang.RuntimeException: error:0c0890ba:ASN.1 encoding routines:asn1_check_tlen:WRONG_TAG
Le code:
// Read private key.
InputStream is = context.getResources().openRawResource(R.raw.private_key);
br = new BufferedReader(new InputStreamReader(is));
List<String> lines = new ArrayList<String>();
line = null;
while ((line = br.readLine()) != null)
lines.add(line);
// Removes the first and last lines of the file (comments).
if (lines.size() > 1 && lines.get(0).startsWith("-----") &&
lines.get(lines.size()-1).startsWith("-----")) {
lines.remove(0);
lines.remove(lines.size()-1);
}
// Concats the remaining lines to a single String.
StringBuilder sb = new StringBuilder();
for (String aLine: lines)
sb.append(aLine);
String keyString = sb.toString();
// Converts the String to a PublicKey instance
byte[] keyBytes = Base64.decode(keyString, Base64.DEFAULT);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
mKey = keyFactory.generatePrivate(spec);
Une aide?
est votre clé au format PKCS # 8 ou au format PKCS # 1? cela commence par '---- BEGIN PRIVATE KEY -----' ou '' ---- BEGIN RSA PRIVATE KEY ----- '? Dans le second cas, vous devez le convertir en pcks8 – pedrofb
Vous avez raison pedrofb, mettez votre commentaire comme réponse si vous voulez que je le mette à la bonne réponse. – svprdga