|
//Copyright 2017 - https://github.com/lbalmaceda |
|
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
|
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
|
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
|
|
package com.auth0.jwt; |
|
|
|
import org.bouncycastle.util.io.pem.PemObject; |
|
import org.bouncycastle.util.io.pem.PemReader; |
|
|
|
import java.io.File; |
|
import java.io.FileNotFoundException; |
|
import java.io.FileReader; |
|
import java.io.IOException; |
|
import java.security.KeyFactory; |
|
import java.security.NoSuchAlgorithmException; |
|
import java.security.PrivateKey; |
|
import java.security.PublicKey; |
|
import java.security.spec.EncodedKeySpec; |
|
import java.security.spec.InvalidKeySpecException; |
|
import java.security.spec.PKCS8EncodedKeySpec; |
|
import java.security.spec.X509EncodedKeySpec; |
|
|
|
public class PemUtils { |
|
|
|
private static byte[] parsePEMFile(File pemFile) throws IOException { |
|
if (!pemFile.isFile() || !pemFile.exists()) { |
|
throw new FileNotFoundException(String.format("The file '%s' doesn't exist.", pemFile.getAbsolutePath())); |
|
} |
|
PemReader reader = new PemReader(new FileReader(pemFile)); |
|
PemObject pemObject = reader.readPemObject(); |
|
byte[] content = pemObject.getContent(); |
|
reader.close(); |
|
return content; |
|
} |
|
|
|
private static PublicKey getPublicKey(byte[] keyBytes, String algorithm) { |
|
PublicKey publicKey = null; |
|
try { |
|
KeyFactory kf = KeyFactory.getInstance(algorithm); |
|
EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); |
|
publicKey = kf.generatePublic(keySpec); |
|
} catch (NoSuchAlgorithmException e) { |
|
System.out.println("Could not reconstruct the public key, the given algorithm could not be found."); |
|
} catch (InvalidKeySpecException e) { |
|
System.out.println("Could not reconstruct the public key"); |
|
} |
|
|
|
return publicKey; |
|
} |
|
|
|
private static PrivateKey getPrivateKey(byte[] keyBytes, String algorithm) { |
|
PrivateKey privateKey = null; |
|
try { |
|
KeyFactory kf = KeyFactory.getInstance(algorithm); |
|
EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); |
|
privateKey = kf.generatePrivate(keySpec); |
|
} catch (NoSuchAlgorithmException e) { |
|
System.out.println("Could not reconstruct the private key, the given algorithm could not be found."); |
|
} catch (InvalidKeySpecException e) { |
|
System.out.println("Could not reconstruct the private key"); |
|
} |
|
|
|
return privateKey; |
|
} |
|
|
|
public static PublicKey readPublicKeyFromFile(String filepath, String algorithm) throws IOException { |
|
byte[] bytes = PemUtils.parsePEMFile(new File(filepath)); |
|
return PemUtils.getPublicKey(bytes, algorithm); |
|
} |
|
|
|
public static PrivateKey readPrivateKeyFromFile(String filepath, String algorithm) throws IOException { |
|
byte[] bytes = PemUtils.parsePEMFile(new File(filepath)); |
|
return PemUtils.getPrivateKey(bytes, algorithm); |
|
} |
|
|
|
} |
I am trying this with OpenSSL generated RSA file. I am getting Exception (InvalidKeyException).
But as @lbalmaceda said, it is working with the private key file he has shared above in the link.
I have generated RSA private key using OpenSSL with the following command
openssl genrsa -out private.key 1024
-----BEGIN RSA PRIVATE KEY-----
MIICXwIBAAKBgQC1POE0N0juIEKW4drJWaJ0dNtvSdG/H12cGO4qJRFgaZFUOn1s
pJ/gAw0nYJbQI89EJaH9DQwiesDq0XFkfMqRg01PdDWkEZe2QRP5++Nfmu+CI18P
kNDzbTdbGAw5Xfq/jrkjgdu+fJDz+QNS9VE5KEYe/m9sD91F9+r151qTRwIDAQAB
AoGBAJnrDC92TD+/sg3F3jNmJmvU2o9XGATCtJNfMNUmCe3hegUYb3CXFxf+P2uT
wkEeSGZNt5bbP9UAf1ptaWm3+afQ1h83CPOQhLl8r4/6buTfIZL2eV+C9gPOwlBa
gRsznGh4qg8D/P/X8Mq6+Q4eHiIDdP6/HjDuVAfPY8KlEoDhAkEA3oAA6mqge+Xi
1Otj+F9TVSKA6jfMFbHmwOEHi3ACB93BMMqaCaxSV6T9MKLtttLJTP1wBx+CdQte
tcLlxrbTaQJBANCGeVYHfrKpO+O0U1R2nIEWJ7Pd8oTITulyI55W2PqC05rYai7u
6Q26YMsjIlMubqv6UzuVReV03RidmVPKSy8CQQC97ZhaghBiErdRN2oLzxtsVdqj
lGOitUybort0/HTPUC0kQB3DWhSj+hOi28F9SWtKTCDAA9axoLYFA8xulwvZAkEA
y4BQ7cpGtWk/T0tuf2F5/uh2Oq0BvuAVUvHXHPG4s1H13IoTplX2DzWyvMw+9Vq9
Gw0jKWTWX8Ya96jmN8WWdQJBALjiR19s7+PBc8iQE0WHsoU1rpZglyglifg2P7hz
yEmLuocXDc96Ftvnq8NvZhQpyZEnMtMmt99qki+DCDdwf20=
-----END RSA PRIVATE KEY-----
and is validated with OpenSSL without any issue.
The only difference between the example file and my file is, in example it says "-----BEGIN PRIVATE KEY-----" and in my one "-----BEGIN RSA PRIVATE KEY-----"