11package com .prenda .helper ;
22
3- import java .io .DataInputStream ;
4- import java .io .File ;
5- import java .io .FileInputStream ;
6- import java .security .KeyFactory ;
7- import java .security .interfaces .RSAPrivateKey ;
8- import java .security .spec .PKCS8EncodedKeySpec ;
9- import java .util .Base64 ;
3+ import java .io .FileReader ;
4+ import java .security .Key ;
5+ import java .security .KeyPair ;
106import java .util .Date ;
117import java .util .GregorianCalendar ;
128import java .util .Properties ;
139
10+ import org .apache .log4j .Logger ;
11+ import org .bouncycastle .openssl .PEMKeyPair ;
12+ import org .bouncycastle .openssl .PEMParser ;
13+ import org .bouncycastle .openssl .jcajce .JcaPEMKeyConverter ;
14+
1415import com .prenda .servlet .RegisterOwner ;
1516
1617import io .jsonwebtoken .Jwts ;
1718import io .jsonwebtoken .SignatureAlgorithm ;
18- import io .jsonwebtoken .impl .TextCodec ;
1919
2020public class KeyUtil {
21+
22+ private static Logger log = Logger .getLogger (KeyUtil .class );
2123
2224 public String getJws () {
2325 String jws ="" ;
2426 try {
2527 Properties props = new Properties ();
2628 props .load (RegisterOwner .class .getResourceAsStream ("/env.properties" ));
27- // String token = props.getProperty("github.token");
2829 String path = props .getProperty ("github.pem" );
2930 GregorianCalendar gc = new GregorianCalendar ();
3031 gc .add (GregorianCalendar .MINUTE , 10 );
3132 jws = Jwts .builder ().setIssuer ("10575" ).setIssuedAt (new Date ()).setExpiration (gc .getTime ())
32- .signWith (SignatureAlgorithm .RS256 , getPemPrivateKey (path , "RSA " )).compact ();
33-
33+ .signWith (SignatureAlgorithm .RS256 , getPemPrivateKey (path , "BC " )).compact ();
34+ log . info ( "jws: " + jws );
3435 } catch (Exception e ) {
3536 e .printStackTrace ();
3637 }
3738 return jws ;
3839 }
3940
40- public RSAPrivateKey getPemPrivateKey (String filename , String algorithm ) {
41- RSAPrivateKey rSAPrivateKey = null ;
41+ public Key getPemPrivateKey (String filename , String provider ) {
42+ Key key = null ;
4243 try {
44+ /* PKCS8
4345 File f = new File(filename);
4446 FileInputStream fis = new FileInputStream(f);
4547 DataInputStream dis = new DataInputStream(fis);
@@ -48,18 +50,24 @@ public RSAPrivateKey getPemPrivateKey(String filename, String algorithm) {
4850 dis.close();
4951
5052 String temp = new String(keyBytes);
51- String privKeyPEM = temp .replace ("-----BEGIN PRIVATE KEY-----\n " , "" );
52- privKeyPEM = privKeyPEM .replace ("-----END PRIVATE KEY-----" , "" );
53- // System.out.println ("Private key\n"+privKeyPEM);
53+ String privKeyPEM = temp.replace("-----BEGIN RSA PRIVATE KEY-----\n", ""); //PCKS1 is -----BEGIN PRIVATE KEY-----\n
54+ privKeyPEM = privKeyPEM.replace("-----END RSA PRIVATE KEY-----", ""); // //PCKS1 is -----END PRIVATE KEY-----
55+ log.info ("Private key\n"+privKeyPEM);
5456
5557 byte[] decoded = Base64.getDecoder().decode(privKeyPEM); // TextCodec.BASE64.decode(privKeyPEM); //
5658
57- PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec (decoded );
59+ /* PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decoded);
5860 KeyFactory kf = KeyFactory.getInstance(algorithm);
59- rSAPrivateKey = (RSAPrivateKey ) kf .generatePrivate (spec );
61+ rSAPrivateKey = (RSAPrivateKey) kf.generatePrivate(spec);*/
62+
63+ PEMParser pemParser = new PEMParser (new FileReader (filename ));
64+ JcaPEMKeyConverter converter = new JcaPEMKeyConverter ().setProvider (provider );
65+ Object object = pemParser .readObject ();
66+ KeyPair kp = converter .getKeyPair ((PEMKeyPair ) object );
67+ key = (Key ) kp .getPrivate ();
6068 } catch (Exception e ) {
6169 e .printStackTrace ();
6270 }
63- return rSAPrivateKey ;
71+ return key ;
6472 }
6573}
0 commit comments