Java XML Data Type Converter convertToPEM(PublicKey key)

Here you can find the source of convertToPEM(PublicKey key)

Description

convert To PEM

License

Open Source License

Declaration

static public String convertToPEM(PublicKey key) 

Method Source Code

//package com.java2s;
/*// w  w w. j a v  a 2s. c  o m
 * Copyright 2014-2017.
 * Distributed under the terms of the GPLv3 License.
 *
 * Authors:
 *      Clemens Zeidler <czei002@aucklanduni.ac.nz>
 */

import javax.xml.bind.DatatypeConverter;
import java.security.*;

import java.util.ArrayList;
import java.util.List;

public class Main {
    static private String convertToPEM(String type, Key key) {
        String pemKey = "-----BEGIN " + type + "-----\n";
        List<String> parts = splitIntoEqualParts(DatatypeConverter.printBase64Binary(key.getEncoded()), 64);
        for (String part : parts)
            pemKey += part + "\n";
        pemKey += "-----END " + type + "-----";
        return pemKey;
    }

    static public String convertToPEM(PublicKey key) {
        String algo = key.getAlgorithm();
        return convertToPEM(algo + " PUBLIC KEY", key);
    }

    static public String convertToPEM(PrivateKey key) {
        String algo = key.getAlgorithm();
        return convertToPEM(algo + " PRIVATE KEY", key);
    }

    private static List<String> splitIntoEqualParts(String string, int partitionSize) {
        List<String> parts = new ArrayList<>();
        int length = string.length();
        for (int i = 0; i < length; i += partitionSize)
            parts.add(string.substring(i, Math.min(length, i + partitionSize)));
        return parts;
    }
}

Related

  1. calculateHMAC(final String secretKey, final String feedId)
  2. compress(String string)
  3. compress(String string)
  4. convertBpmnVariableValueToXslParam(final Object bpmnVariableValue)
  5. convertToFourBytes(int numberToConvert)
  6. convertToTwoBytes(int numberToConvert)
  7. createBasicAuthenticationProperty(final String username, final String password)
  8. decode(final CharSequence _text)
  9. decode(String auth)