/** * Write a PEM format PKCS1 private using a writer. * * @param privateKey * @param writer * @throws IOException */ public static void toPKCS1PEM(PrivateKey privateKey, Writer writer) throws IOException { String pem = toPKCS1PEM(privateKey); writer.write(pem); writer.flush(); }
public void save_key(String fullPath, String cipher, Object callback) throws IOException { KeyUtil.toPKCS1PEM(keyPair.getPrivate(), new FileOutputStream(fullPath)); } public void save_pub_key(String fullPath) throws IOException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
public static void toPKCS1PEM(PrivateKey privateKey, OutputStream out) throws IOException { PrintStream printStream = new PrintStream(out); printStream.print(toPKCS1PEM(privateKey)); printStream.flush(); }
/** * Writes the retrieved credentials to the specified output stream. * * @param os OutputStream to write to * @throws IOException * @throws GeneralSecurityException */ public void saveCredentials(OutputStream os) throws IOException, GeneralSecurityException { CertUtil.toPEM(certificateChain, os); // since we are putting the private key in the same file as the certs, we have to // add a new line. os.write('\n'); KeyUtil.toPKCS1PEM(keypair.getPrivate(), os); }
public static void printKeys() throws Exception { String text = "eyJ0eXAiOiJKV1QiLCJraWQiOiI5azBIUEczbW9YRU5uZSIsImFsZyI6IlJTMjU2In0.eyJpc3MiOiJodHRwczovL2FzaGlnYXJ1Lm5jc2EudWl1Yy5lZHU6OTQ0MyIsInN1YiI6ImpnYXlub3IiLCJleHAiOjE0ODQ3NjQ3NDQsImF1ZCI6Im15cHJveHk6b2E0bXAsMjAxMjovY2xpZW50X2lkLzE0NjQ5ZTJmNDY4NDUwZGFjMGMxODM0ODExZGJkNGM3IiwiaWF0IjoxNDg0NzYzODQ0LCJub25jZSI6IjBaSWktRXV4ZUNfWDhBZ0IzVmlmT29xS2lYV3N6X05sWFN6SXU3aDhyelUiLCJhdXRoX3RpbWUiOiIxNDg0NzYzODQzIn0"; String keyID = "aQEiCy2fJcVgkOft"; KeyPair keyPair = KeyUtil.generateKeyPair(); JSONWebKeys keys = new JSONWebKeys(keyID); JSONWebKey key = new JSONWebKey(); key.privateKey = keyPair.getPrivate(); key.publicKey = keyPair.getPublic(); key.algorithm = "RS256"; key.id = keyID; key.use = "sig"; key.type = "RSA"; keys.put(key); System.out.println("----- START keys"); System.out.println(KeyUtil.toX509PEM(keyPair.getPublic())); System.out.println(KeyUtil.toPKCS1PEM(keyPair.getPrivate())); System.out.println(KeyUtil.toPKCS8PEM(keyPair.getPrivate())); System.out.println("----- END keys\n"); JSONObject jsonKeys = JSONWebKeyUtil.toJSON(keys); JSONWebKeys keys2 = JSONWebKeyUtil.fromJSON(jsonKeys.toString(2)); JSONWebKey webKey = keys2.get(keyID); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(webKey.privateKey.getEncoded()); RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(keySpec); System.out.println(KeyUtil.toX509PEM(webKey.publicKey)); System.out.println(KeyUtil.toPKCS1PEM(privateKey)); System.out.println(KeyUtil.toPKCS8PEM(privateKey)); }
public static void firstTestB() throws Exception { String keyID = "9k0HPG3moXENne"; JSONWebKeys keys = JSONWebKeyUtil.fromJSON(new File("/home/ncsa/dev/csd/config/keys.jwk")); JSONObject payload = new JSONObject(); payload.put("name", "jeff"); payload.put("id", "sukjfhusdfsdjkfh"); payload.put("other_claim", "skjdf93489ghiovs 98sd89wehi ws"); payload.put("another_claim", "l;kfg8934789dfio9v 92w89 98wer"); JSONWebKey webKey = keys.get(keyID); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(webKey.privateKey.getEncoded()); RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(keySpec); System.out.println(KeyUtil.toX509PEM(webKey.publicKey)); System.out.println(KeyUtil.toPKCS1PEM(privateKey)); System.out.println(KeyUtil.toPKCS8PEM(privateKey)); String tokken = createJWT(payload, keys.get(keyID)); System.out.println("JWT=" + tokken); System.out.println("claims=" + verifyAndReadJWT(tokken, keys)); System.out.println("-----"); // note that if the this last call // works it is because the verification works too. }
request.setAttribute("privateKey", KeyUtil.toPKCS1PEM(asset.getPrivateKey())); }else{ request.setAttribute("privateKey", "(none)");