2008年4月27日 星期日

X.509 With JAVA SE 1.6 半成品版本(Ver0.1)


Java + X.509
用java寫的..分析X.509的檔案用(像是.cer .crt之類(應該))

import java.io.*;
import java.security.cert.*;

public class x509{
  public static void main(String[] args){
    try{
      InputStream inStream = new FileInputStream("124.cer");
      CertificateFactory cf = CertificateFactory.getInstance("X.509");
      X509Certificate cert = (X509Certificate)cf.generateCertificate(inStream);
      System.out.println("Certificate:");
      System.out.println("    Data:");
      System.out.println("        Version: "+cert.getVersion());
      System.out.println("        Serial Number: "+cert.getSerialNumber());
      System.out.println("        Signature Algorithm: "+cert.getSigAlgName());
      System.out.println("        Issuer: "+cert.getIssuerDN());
      System.out.println("- - - - -");
      System.out.println("        Validity");
      System.out.println("            Not Before: "+cert.getNotBefore());
      System.out.println("            Not After : "+cert.getNotAfter());
      System.out.println("        Subject: "+cert.getSubjectDN());
      System.out.println("- - - - -");
      System.out.println("        Subject Public Key Info:");
      System.out.println("            ******* not finish *********");
      System.out.println("        X509v3 extensions:");
      System.out.println("            ******* not finish *********");
      System.out.println("    Signature Algorithm: "+cert.getSigAlgName());
      System.out.println("- - - - -");
      System.out.println(byteArrayToHexString(cert.getSignature()));
      System.out.println("- - - - -");
      inStream.close();
    }catch(IOException ioe){}
      catch(CertificateException ce){}
    }

    static String byteArrayToHexString(byte in[]) {
    byte ch = 0x00;
    int i = 0;

    if (in == null || in.length <= 0)
      return null;
    String pseudo[] = {"0", "1", "2","3", "4", "5", "6", "7", "8","9", "A", "B", "C", "D", "E","F"};
    StringBuffer out = new StringBuffer(in.length * 2);
    while (i < ch =" (byte)" ch =" (byte)">>> 4){
      ch = (byte) (ch & 0x0F);
      out.append(pseudo[ (int) ch]);
      ch = (byte) (in[i] & 0x0F);
      out.append(pseudo[ (int) ch]);
      i++;
      out.append(":");
    }
    String rslt = new String(out);
    return rslt;
  }
}


結論
以上...有部分沒做完

有需要請自行補完 就這樣(逃)