1 package org.cacert.gigi.util;
3 import java.util.Base64;
7 public static String encode(String type, byte[] data) {
8 return "-----BEGIN " + type + "-----\n" + //
9 Base64.getEncoder().encodeToString(data).replaceAll("(.{64})(?=.)", "$1\n") + //
10 "\n-----END " + type + "-----";
13 public static byte[] decode(String type, String data) {
14 data = data.replaceAll("-----BEGIN " + type + "-----", "").replace("\n", "").replace("\r", "");
15 // Remove the first and last lines
16 data = data.replaceAll("-----END " + type + "-----", "");
17 // Base64 decode the data
18 return Base64.getDecoder().decode(data);