get Serial Number from X509Certificate - Java Security

Java examples for Security:Certificate

Description

get Serial Number from X509Certificate

Demo Code


//package com.java2s;

import java.security.cert.X509Certificate;

public class Main {

    public static String getSerialNumber(X509Certificate cert) {
        if (null == cert)
            return null;
        byte[] serial = cert.getSerialNumber().toByteArray();
        if (serial.length > 0) {
            String serialNumberString = new String();
            for (int i = 0; i < serial.length; i++) {
                String s = Integer.toHexString(Byte.valueOf(serial[i])
                        .intValue());//  www  .  j  a  v  a 2 s.c  o m
                if (s.length() == 8)
                    s = s.substring(6);
                else if (1 == s.length())
                    s = "0" + s;
                serialNumberString += s;
            }
            return serialNumberString;
        }
        return null;
    }
}

Related Tutorials