Ausnahme beim Entschlüsseln einer mit OpenSSL generierten "der" -Datei: Die Eingabelänge muss ein Vielfaches von 8 sein, wenn mit gepolsterter Verschlüsselung entschlüsselt wird

Zunächst erstelle ich mit OpenSSL eine private RSA-Schlüsseldatei und konvertiere sie dann in eine verschlüsselte "der" -Datei:

$ openssl pkcs8 -topk8 -inform PEM -outform DER -in private_key.pem -out private_key.der

Nächste Ich versuche, diese Datei mit dem folgenden Code aus Java zu entschlüsseln (in diesem Stadium habe ich die Datei bereits in dasbyte[] key Array mit Code, der sich am Ende dieses Beitrags befindet):

public static byte[] decryptPrivateKey(byte[] key) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    PBEKeySpec passKeySpec = new PBEKeySpec("p".toCharArray()); //my password

    EncryptedPrivateKeyInfo encryptedKey = new EncryptedPrivateKeyInfo(key);
    System.out.println(encryptedKey.getAlgName());
    //PBEWithMD5AndDES
    System.out.println("key length: " + key.length);
    //key length: 677
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance(encryptedKey.getAlgName());
    SecretKey passKey = keyFac.generateSecret(passKeySpec);

     // Create PBE Cipher
    Cipher pbeCipher = Cipher.getInstance(encryptedKey.getAlgName());
    // Initialize PBE Cipher with key and parameters
    pbeCipher.init(Cipher.DECRYPT_MODE, passKey, encryptedKey.getAlgParameters());

    // Decrypt the private key(throws the exception)
    return pbeCipher.doFinal(key);
}

Ich erhalte den folgenden Stack-Trace in der obigen return-Anweisung:

Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:750)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
    at com.sun.crypto.provider.PBECipherCore.doFinal(PBECipherCore.java:422)
    at com.sun.crypto.provider.PBEWithMD5AndDESCipher.engineDoFinal(PBEWithMD5AndDESCipher.java:316)
    at javax.crypto.Cipher.doFinal(Cipher.java:2087)
    at roland.test.crypto.Test.decryptPrivateKey(Test.java:96)
    at roland.test.crypto.Test.getPrivateKey(Test.java:74)
    at roland.test.crypto.Test.test(Test.java:58)
    at roland.test.crypto.Test.main(Test.java:30)

Der Schlüssel wird als Byte-Array aus der "der" -Datei gelesen:

public static PrivateKey getPrivateKey() throws Exception {
    byte[] key = null;
    try(final InputStream resourceStream = getMyClass().getResourceAsStream("private_key.der")) { //$NON-NLS-1$\r
        key = ByteStreams.toByteArray(resourceStream);
    } catch (IOException e) {
        e.printStackTrace();
    }

    key = decryptPrivateKey(key);
}

Antworten auf die Frage(1)

Ihre Antwort auf die Frage