java certificate转化成base64编码

 将证书转换成base64编码的形式,下一篇日志会介绍反转换。

public static void main(String[] args) {
  // TODO Auto-generated method stub

  secUtil sec = new secUtil();
    
      try {
          // Get the encoded form which is suitable for exporting


          Certificate cert = sec.getCert();    //获取证书
                   
          java.io.FileWriter wr = new java.io.FileWriter(new java.io.File("c://tomcat.cert"));


          wr.write(new sun.misc.BASE64Encoder().encode(cert.getEncoded()));  //给证书编码
          wr.flush();
         
      }  catch (Exception e) {
      }

   try {
    java.io.BufferedReader br = new java.io.BufferedReader(new java.io.FileReader(new java.io.File("c://rp.cert")));


     String s, strCert = new String();


          while((s = br.readLine())!= null)
            strCert += s + "\n";

  System.out.println(strCert);


   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }   

 }

输出结果为

MIICPTCCAaagAwIBAgIETYHOUDANBgkqhkiG9w0BAQUFADBjMQswCQYDVQQGEwJjaDEQMA4GA1UE
CBMHYmVpamluZzEQMA4GA1UEBxMHYmVpamluZzENMAsGA1UEChMEYnVwdDENMAsGA1UECxMEYnVw
dDESMBAGA1UEAxMJbWluYXpoYW5nMB4XDTExMDMxNzA5MDMxMloXDTExMDYxNTA5MDMxMlowYzEL
MAkGA1UEBhMCY2gxEDAOBgNVBAgTB2JlaWppbmcxEDAOBgNVBAcTB2JlaWppbmcxDTALBgNVBAoT
BGJ1cHQxDTALBgNVBAsTBGJ1cHQxEjAQBgNVBAMTCW1pbmF6aGFuZzCBnzANBgkqhkiG9w0BAQEF
AAOBjQAwgYkCgYEAojGYAsSDgqkcZGjDTPWF3KVTzZ1x9eHfcpl4YbMxlsM/dOYjKiX72Ocgmnbz
sZfjAFwtvgW+xaL6ctjEjuM2/qcq0co0D4qRn9zwc+S/3Cgf9hgknvyDzm3rD21TQXJANuGHabaa
2jEnmf4yKDWDdbnBUX2euSCrcfRF+oEXjG8CAwEAATANBgkqhkiG9w0BAQUFAAOBgQAfPJ6HgnXQ
1WpaFWlMT6FxjLzSOFLvqo3UlOAqxXGw3zqbV/35IZY/Z+3UW6cJYcMTeyqRxu9APiy+ZgHOaf9s
TsU8Of7gJ4K6rW1wCkcMx3VhiLXk7SsZ0HZh0gwS/oLOw8PJNEirgmXGRGtABen6UyiLLflJn57V
466jtSQnhA==

你可能感兴趣的:(java,java,exception,string,null,c)