How to convert Base64 encoded pkcs12 content to java.security.PrivateKey? -


i'm consuming google directory api via service account , received pkcs12 key @ service account creation.

google support 2 different ways use taking key java.io.file or java.security.privatekey , poc, have used first way creating googlecredential object using java.io.file,

        googlecredential credential = new googlecredential.builder()                 .settransport(httptransport)                 .setjsonfactory(jsonfactory)                 .setserviceaccountid(serviceaccountid)                 .setserviceaccountscopes(arrays.aslist(directoryscopes.admin_directory_user))                 .setserviceaccountuser(serviceaccountuser)                 .setserviceaccountprivatekeyfromp12file(serviceaccountprivatekeyfile)                 .build(); 

it's working expected in actual use case cannot rely on filesystem cannot use first method. wanted implement actual use case using second way using java.security.privatekey , following when done.

    googlecredential credential = new googlecredential.builder()             .settransport(httptransport)             .setjsonfactory(jsonfactory)             .setserviceaccountid(serviceaccountid)             .setserviceaccountscopes(arrays.aslist(directoryscopes.admin_directory_user))             .setserviceaccountuser(serviceaccountuser)             .setserviceaccountprivatekey(serviceaccountprivatekey)             .build(); 

im use case, need make possible upload private key , store in database base64 encoded. need pass content of pkcs12 key , create googlecredential object. think second option suited way, couldn't find example create java.security.privatekey base64 encoded content of uploaded key.

is possible create java.security.privatekey object base64 encoded content of pkcs12 key?

or there other way around achieve usecase?

thanks in advance

darray

java.securty.keystore takes inputstream load() method, create using whatever method have of obtaining .p12 bytes. here's alternate method used. while still using file .p12 bytes, bring in bytearrayinputstream or inputstream subclass:

    private privatekey getprivatekeyfromp12() {         // google p12 files have "notasecret" pass , "privatekey" pk name         string p12p = "notasecret";    // not cool!  change before exporting .p12         try {             keystore keystore = keystore.getinstance("pkcs12");             // you'd adjust bring in .p12 bytes or chars             // input stream.  passwords char array!             keystore.load(                 this.getclass().getclassloader()                         .getresourceasstream("the.p12"),                 p12p.tochararray());             // key name fixed google, changed             privatekey key = (privatekey) keystore.getkey("privatekey",                 p12p.tochararray());             return key;         } catch (exception e) {             log.error("exception while trying obtain private key",e);             return null;         }       } 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -