Java XML Data Type Converter writeKey(Writer writer, PublicKey publicKey)

Here you can find the source of writeKey(Writer writer, PublicKey publicKey)

Description

Write Public Key in PEM format to the writer.

License

Apache License

Parameter

Parameter Description
writer Output writer
publicKey Public key to store

Exception

Parameter Description
IOException an exception

Declaration

public static void writeKey(Writer writer, PublicKey publicKey) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;

import java.io.Writer;

import java.security.PublicKey;

import javax.xml.bind.DatatypeConverter;

public class Main {
    private final static String PUBLIC_KEY_PREFIX = "-----BEGIN PUBLIC KEY-----";
    private final static String PUBLIC_KEY_SUFFIX = "-----END PUBLIC KEY-----";

    /**/*from w ww.ja v  a 2s .  co m*/
     * Write Public Key in PEM format to the writer.
     * 
     * @param writer
     *          Output writer
     * @param publicKey
     *            Public key to store
     * @throws IOException
     */
    public static void writeKey(Writer writer, PublicKey publicKey) throws IOException {
        writer.write(PUBLIC_KEY_PREFIX);
        writer.write('\n');
        String base64 = generateBase64(publicKey);
        char[] charArray = base64.toCharArray();

        for (int i = 0; i < charArray.length; i += 64) {
            int length = Math.min(charArray.length - i, 64);
            writer.write(charArray, i, length);
            writer.write('\n');
        }
        writer.write(PUBLIC_KEY_SUFFIX);
        writer.write("\n");
    }

    private static String generateBase64(PublicKey publicKey) {
        return DatatypeConverter.printBase64Binary(publicKey.getEncoded());
    }
}

Related

  1. toString(byte[] edid)
  2. toString(final Serializable o)
  3. toString(Serializable o)
  4. toTime(String st)
  5. unserializeURLSafe(String string)
  6. writeOpaqueValue(final String opaque)