Helper FunctionsΒΆ

The following Java functions use the Spongy Castle library.

Helper Functions:

public static PublicKey getRSAPublicKeyFromString(String apiKey) throws Exception{
    KeyFactory keyFactory = KeyFactory.getInstance("RSA", "SC");
    byte[] publicKeyBytes = Base64.decode(apiKey.getBytes("UTF-8"), Base64.DEFAULT);
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes);
    return keyFactory.generatePublic(x509KeySpec);
}

public static PrivateKey getRSAPrivateKeyFromString(String key) throws Exception {
    byte [] clear = Base64.decode(key, Base64.DEFAULT);
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(clear);
    KeyFactory fact = KeyFactory.getInstance("RSA", "SC");
    PrivateKey priv = fact.generatePrivate(keySpec);
    Arrays.fill(clear, (byte) 0);
    return priv;
}

public static String stripPublicKeyHeaders(String key) {
        //strip the headers from the key string
        StringBuilder strippedKey = new StringBuilder();
        String lines[] = key.split("\n");
        for(String line : lines) {
            if(!line.contains("BEGIN PUBLIC KEY") && !line.contains("END PUBLIC KEY")) {
                strippedKey.append(line.trim());
            }
        }
        return strippedKey.toString().trim();
}

User Contributed

LaunchKey links to user contributed code as a resource to its community. LaunchKey does not in any way guarantee or warrant the quality and security of these code bases. User contributed code is supported by the creators. If you do find a link from the site to user contributed code that is malicious or inappropriate in any way, please report that link to LaunchKey immediately and we will investigate the claim. Submit any issue to LaunchKey support at https://launchkey.com./support. ×