Je suis les étapes suivantes à partir du lien: How to read .pem file to get private and public key. J'ai exécuté les trois commandes suivantes:Seules les clés RSAPrivate (Crt) KeySpec et PKCS8EncodedKeySpec sont prises en charge pour les clés privées RSA
1. $openssl genrsa -out mykey.pem 2048
2. $openssl pkcs8 -topk8 -inform PEM -outform PEM -in mykey.pem -out private_key.pem -nocrypt
3. $ openssl rsa -in mykey.pem -pubout -outform DER -out public_key.der
Cela a créé trois fichiers, mais quand je tentais de les lire dans le code Java je commencé à faire face ci-dessous erreur:
PUBLIC KEY EXPO : 65537
Only RSAPrivate(Crt)KeySpec and PKCS8EncodedKeySpec supported for RSA private keys
Mon code de référence:
public class PublicPrivateKeyDemo {
private static File privateKeyFile = null;
private static File publicKeyFile = null;
public static void main(String[] args) {
String path = "C:/Users/test898/keys";
privateKeyFile = new File(path + "/" + "private.pem");
publicKeyFile = new File(path + "/" + "public.der");
try {
loadkeys();
} catch (IOException | GeneralSecurityException e) {
System.out.println(e.getMessage());
}
}
private static void loadkeys() throws IOException, GeneralSecurityException {
byte[] publicKeyBytes = new byte[(int) publicKeyFile.length()];
FileInputStream publicFis = null;
publicFis = new FileInputStream(publicKeyFile);
if (publicFis.read(publicKeyBytes) > 0) {
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
KeyFactory factory = KeyFactory.getInstance("RSA");
RSAPublicKey pubKey = (RSAPublicKey) factory.generatePublic(publicKeySpec);
BigInteger pKeyModulus = pubKey.getModulus();
BigInteger pKeyExponent = pubKey.getPublicExponent();
System.out.println("PUBLIC KEY EXPO : "+pKeyExponent);
}
byte[] privateKeyBytes = new byte[(int) privateKeyFile.length()];
FileInputStream privateFis = null;
privateFis = new FileInputStream(privateKeyFile);
if (privateFis.read(privateKeyBytes) > 0) {
//PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKeyBytes);
X509EncodedKeySpec spec = new X509EncodedKeySpec(privateKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateKey privKey = (RSAPrivateKey) keyFactory.generatePrivate(spec);
BigInteger pKeyModulus = privKey.getModulus();
BigInteger pKeyExponent = privKey.getPrivateExponent();
System.out.println("PRIVATE KEY : "+pKeyExponent);
}
}
}
S'il vous plaît aidez-moi ce qui ne va pas ici? Je suppose que la clé privée doit donner une valeur quelque chose simillar comme ci-dessous ":
21150549370950609585296765828149303178265715265804890679831411170495636016527926323370428466362501818569080118374307191403222367256185274427528812911191842330928112748042350818573390540259857225467392220170770506599589136056049534085562156615813126185240565396115577449461468695709719589257257375788267753694280485882595576829517086782992300102288858453543505912425724874212273830247789870669315715724390578125469483751830964757980799543436412647956770560679365767737577100276745456138533646455249170660612983618544127693626739283128573829732806886889896396374650462743743148634276240986662548043510018342217214342529
Votre privé La clé doit être au format DER, pas PEM. Et vous devez utiliser le PKCS8EncodedKeySpec pour la clé privée. Et même si cela fonctionne généralement, ce n'est pas la bonne façon de lire dans un fichier. Voir [ma réponse] (http://stackoverflow.com/questions/7250229/reading-a-binary-input-stream-into-a-single-byte-array-in-java/7250275#7250275) et d'autres à un question différente –