Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. //takes a string and converts it to a cert object
  2. //string has to have line breaks!
  3. public static X509Certificate decodeCert(String certString) throws Exception {
  4. byte[] der = Base64.getDecoder().decode(certString.getBytes());
  5.  
  6. String LINE_SEPARATOR = System.getProperty("line.separator");
  7. String BEGIN_CERT = "-----BEGIN CERTIFICATE-----\n";
  8. String END_CERT = "\n-----END CERTIFICATE-----";
  9.  
  10. Base64.Encoder encoder = Base64.getMimeEncoder(64, LINE_SEPARATOR.getBytes());
  11.  
  12. String encodedCertText = new String(encoder.encode(der));
  13.  
  14. String fullCertString = BEGIN_CERT + encodedCertText + END_CERT;
  15.  
  16. CertificateFactory fact = CertificateFactory.getInstance("X.509");
  17. InputStream is = new ByteArrayInputStream(fullCertString.getBytes());
  18. return (X509Certificate) fact.generateCertificate(is);
  19. }
  20.  
  21. //takes a cert object and converts it to a string
  22. public static String encodeCert(X509Certificate cert) throws Exception {
  23. return new String(Base64.getEncoder().encode(cert.getEncoded()));
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement