Here you can find the source of writeKey(Writer writer, PublicKey publicKey)
Parameter | Description |
---|---|
writer | Output writer |
publicKey | Public key to store |
Parameter | Description |
---|---|
IOException | an exception |
public static void writeKey(Writer writer, PublicKey publicKey) throws IOException
//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()); } }