public void save_pub_key(String fullPath) throws IOException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { KeyUtil.toX509PEM(keyPair.getPublic(), new FileWriter(fullPath)); } }
public static void toX509PEM(PublicKey publicKey, Writer writer) throws IOException { writer.write(toX509PEM(publicKey)); writer.flush(); }
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 testSigning() throws Exception { String h = "{\"typ\":\"JWT\",\"kid\":\"9k0HPG3moXENne\",\"alg\":\"RS256\"}"; String p = "{\"iss\":\"https://ashigaru.ncsa.uiuc.edu:9443\",\"sub\":\"jgaynor\",\"exp\":1484764744,\"aud\":\"myproxy:oa4mp,2012:/client_id/14649e2f468450dac0c1834811dbd4c7\",\"iat\":1484763844,\"nonce\":\"0ZIi-EuxeC_X8AgB3VifOoqKiXWsz_NlXSzIu7h8rzU\",\"auth_time\":\"1484763843\"}\n"; JSONObject header = JSONObject.fromObject(h); System.out.println("header=" + header); JSONObject payload = JSONObject.fromObject(p); System.out.println("payload=" + payload); System.out.println("base 64=" + concat(header, payload)); //String keyID = "9k0HPG3moXENne"; String keyID = "244B235F6B28E34108D101EAC7362C4E"; JSONWebKeys keys = JSONWebKeyUtil.fromJSON(new File("/home/ncsa/dev/csd/config/polo-keys.jwk")); String idTokken = createJWT(payload, keys.get(keyID)); System.out.println(idTokken); JSONObject claims = verifyAndReadJWT(idTokken, keys); System.out.println("claims = " + claims); JSONWebKey webKey = keys.get(keyID); System.out.println(KeyUtil.toX509PEM(webKey.publicKey)); }
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. }
public static void signAndVerify(JSONWebKeys keys, String keyID) throws Exception { String h = "{" + " \"typ\": \"JWT\"," + " \"kid\": \"9k0HPG3moXENne\"," + " \"alg\": \"RS256\"" + "}"; String p = "{\n" + " \"iss\": \"https://ashigaru.ncsa.uiuc.edu:9443\"," + " \"sub\": \"jgaynor\"," + " \"exp\": 1484764744," + " \"aud\": \"myproxy:oa4mp,2012:/client_id/14649e2f468450dac0c1834811dbd4c7\"," + " \"iat\": 1484763844," + " \"nonce\": \"0ZIi-EuxeC_X8AgB3VifOoqKiXWsz_NlXSzIu7h8rzU\"," + " \"auth_time\": \"1484763843\"" + "}"; JSONObject header = JSONObject.fromObject(h); JSONObject payload = JSONObject.fromObject(p); JSONWebKey key = keys.get(keyID); String signature = sign(header, payload, key); System.out.println(concat(header, payload) + "." + signature); System.out.println(KeyUtil.toX509PEM(key.publicKey)); System.out.println("verified?" + verify(header, payload, signature, key)); }
say("key id=" + key); say(KeyUtil.toX509PEM(localKeys.get(key).publicKey));