Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //takes a string and converts it to a cert object
- //string has to have line breaks!
- public static X509Certificate decodeCert(String certString) throws Exception {
- byte[] der = Base64.getDecoder().decode(certString.getBytes());
- String LINE_SEPARATOR = System.getProperty("line.separator");
- String BEGIN_CERT = "-----BEGIN CERTIFICATE-----\n";
- String END_CERT = "\n-----END CERTIFICATE-----";
- Base64.Encoder encoder = Base64.getMimeEncoder(64, LINE_SEPARATOR.getBytes());
- String encodedCertText = new String(encoder.encode(der));
- String fullCertString = BEGIN_CERT + encodedCertText + END_CERT;
- CertificateFactory fact = CertificateFactory.getInstance("X.509");
- InputStream is = new ByteArrayInputStream(fullCertString.getBytes());
- return (X509Certificate) fact.generateCertificate(is);
- }
- //takes a cert object and converts it to a string
- public static String encodeCert(X509Certificate cert) throws Exception {
- return new String(Base64.getEncoder().encode(cert.getEncoded()));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement