]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/PEM.java
[DB]: Add orgAdmin management code
[gigi.git] / src / org / cacert / gigi / util / PEM.java
1 package org.cacert.gigi.util;
2
3 import java.util.Base64;
4
5 public class PEM {
6
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 + "-----";
11     }
12
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);
19
20     }
21 }