Parser une clé RSA PKCS#8

Résolu
talbsbast Messages postés 53 Date d'inscription   Statut Membre Dernière intervention   -  
talbsbast Messages postés 53 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour,
J'ai une clé privée key sous forme d'un byte array : et j'aimerai la convertir en RSAPrivateCrtKeyImpl pour que j'ai puisse récupérer les éléments de la clé (P, Q, dP , ...).
Y a t'il un moyen simple de parser une clé PKCS8 ?
Merci:)

byte[] key = toByteArray("308204bd020100300d06092a864886f7...");

RSAPrivateCrtKeyImpl privateKey = ...?
A voir également:

1 réponse

KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Bonjour,

Tu peux faire comme ceci :
RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey) KeyFactory.getInstance("RSA")
    .generatePrivate(new PKCS8EncodedKeySpec(key));
1
talbsbast Messages postés 53 Date d'inscription   Statut Membre Dernière intervention   1
 
Merci pour votre réponse.
J'ai essayé de faire comme ceci, mais il n'arrive pas à caster un "PrivateKey" to "RSAPrivateCrtKey", j'obtiens l'erreur suivante :

java.lang.ClassCastException: class sun.security.rsa.RSAPrivateCrtKeyImpl cannot be cast to class javacard.security.RSAPrivateCrtKey (sun.security.rsa.RSAPrivateCrtKeyImpl is in module java.base of loader 'bootstrap'; javacard.security.RSAPrivateCrtKey is in unnamed module of loader 'app')
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020 > talbsbast Messages postés 53 Date d'inscription   Statut Membre Dernière intervention  
 
Tu as un problème avec l'import de RSAPrivateCrtKey.
Ce n'est pas avec le package javacard.security qu'il faut travailler mais avec java.security

Un exemple complet :
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;

public class TestRsa {
    public static void main(String[] args) throws Exception {
        String base64 = // https://en.wikipedia.org/wiki/PKCS_8
                "MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEAq7BFUpkGp3+LQmlQ" +
                "Yx2eqzDV+xeG8kx/sQFV18S5JhzGeIJNA72wSeukEPojtqUyX2J0CciPBh7eqclQ" +
                "2zpAswIDAQABAkAgisq4+zRdrzkwH1ITV1vpytnkO/NiHcnePQiOW0VUybPyHoGM" +
                "/jf75C5xET7ZQpBe5kx5VHsPZj0CBb3b+wSRAiEA2mPWCBytosIU/ODRfq6EiV04" +
                "lt6waE7I2uSPqIC20LcCIQDJQYIHQII+3YaPqyhGgqMexuuuGx+lDKD6/Fu/JwPb" +
                "5QIhAKthiYcYKlL9h8bjDsQhZDUACPasjzdsDEdq8inDyLOFAiEAmCr/tZwA3qeA" +
                "ZoBzI10DGPIuoKXBd3nk/eBxPkaxlEECIQCNymjsoI7GldtujVnr1qT+3yedLfHK" +
                "srDVjIT3LsvTqw==";
        byte[] key = Base64.getDecoder().decode(base64);
        RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey) KeyFactory.getInstance("RSA")
                .generatePrivate(new PKCS8EncodedKeySpec(key));
        System.out.println("n=" + privateKey.getModulus());        // n=89920594...
        System.out.println("p=" + privateKey.getPrimeP());         // p=98780596...
        System.out.println("q=" + privateKey.getPrimeQ());         // q=91030625...
        System.out.println("d=" + privateKey.getPrivateExponent());// d=17043709...
        System.out.println("e=" + privateKey.getPublicExponent()); // e=65537
    }
}
0
talbsbast Messages postés 53 Date d'inscription   Statut Membre Dernière intervention   1
 
Effectivement !! Merci beaucoup pour votre aide!!
0