public RSA gen_key(int keyLength, int exponent, Object callback) throws NoSuchProviderException, NoSuchAlgorithmException { RSA rsa = new RSA(); keyPair = KeyUtil.generateKeyPair(); return rsa; } public void save_key(String fullPath, String cipher, Object callback) throws IOException {
@Override protected KeyPair createNew() { try { return KeyUtil.generateKeyPair(); } catch (Exception e) { throw new GeneralException("Error generating keypair", e); } } }
protected JSONWebKey createJWK(String algorithm) throws NoSuchProviderException, NoSuchAlgorithmException { byte[] byteArray = new byte[16]; random.nextBytes(byteArray); String id = DatatypeConverter.printHexBinary(byteArray); KeyPair keyPair = KeyUtil.generateKeyPair(); JSONWebKey webKey = new JSONWebKey(); webKey.publicKey = keyPair.getPublic(); webKey.privateKey = keyPair.getPrivate(); webKey.use = "sig"; webKey.id = id; webKey.algorithm = algorithm; webKey.type = "RSA"; //only one supported return webKey; } }
/** * Command line utility to generate a keypair with a given DN. This is intended for low level * debugging, not public consumption. This is a very stupid utility but much more convenient * than using openSSL or some other such command line utility. * @param args */ public static void main(String[] args){ if(args.length != 1){ System.out.println("Usage: This accepts a single argument that is the DN for a cert request. It returns the pem encoded " + "cert request (but not the private key)"); return; } try{ KeyPair keyPair = KeyUtil.generateKeyPair(); MyPKCS10CertRequest cr = CertUtil.createCertRequest(keyPair, args[0]); System.out.println(CertUtil.fromCertReqToString(cr)); }catch(Throwable t){ t.printStackTrace(); } } }
protected KeyPair getNextKeyPair() { if (keyPair == null || (getEnvironment().getKeypairLifetime() <= 0) || (keypairExpiration < System.currentTimeMillis())) { try { keyPair = KeyUtil.generateKeyPair(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } keypairExpiration = System.currentTimeMillis() + getEnvironment().getKeypairLifetime(); } return keyPair; }
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)); }
@Override public KeyPair pop() { KeyPair x = super.pop(); if (x != null) { return x; } try { KeyPair keyPair = KeyUtil.generateKeyPair(); setSpare(keyPair); return keyPair; } catch (Exception e) { throw new GeneralException("Error generating key pair", e); } } }
public static void generateAndSign() throws Exception { 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_JWT; key.id = keyID; key.use = "sig"; key.type = "RSA"; keys.put(key); System.out.println("Generating keys and signing."); signAndVerify(keys, keyID); JSONObject jsonKeys = JSONWebKeyUtil.toJSON(keys); JSONWebKeys keys2 = JSONWebKeyUtil.fromJSON(jsonKeys.toString(2)); JSONWebKey webKey = keys2.get(keyID); System.out.println("Serializing, deserializing then signing."); signAndVerify(keys2, keyID); }
public static void firstTest() throws Exception { JSONObject header = new JSONObject(); header.put(TYPE, "JWT"); header.put(ALGORITHM, "RS256"); KeyPair keyPair = KeyUtil.generateKeyPair(); JSONWebKey webKey = new JSONWebKey(); webKey.algorithm = "RS256"; webKey.privateKey = keyPair.getPrivate(); webKey.publicKey = keyPair.getPublic(); webKey.id = "qwert"; webKey.type = "sig"; 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"); String tokken = createJWT(payload, webKey); System.out.println("JWT=" + tokken); JSONWebKeys keys = new JSONWebKeys(null); keys.put(webKey.id, webKey); 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. }
keyPair = KeyUtil.generateKeyPair(); } catch (Throwable e) { String msg = "Unable to generate a new keypair.";