verify data by Certificate - Java Security

Java examples for Security:Certificate

Description

verify data by Certificate

Demo Code


//package com.java2s;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;

import java.security.PublicKey;
import java.security.Signature;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] data = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        String sign = "java2s.com";
        String certificatePath = "java2s.com";
        System.out.println(verify(data, sign, certificatePath));
    }// w  w  w  .j a  v  a  2s  . com

    public static final String KEY_STORE = "JKS";
    public static final String X509 = "X.509";

    public static boolean verify(byte[] data, String sign,
            String certificatePath) throws Exception {
        // ?????
        X509Certificate x509Certificate = (X509Certificate) getCertificate(certificatePath);
        // ????
        PublicKey publicKey = x509Certificate.getPublicKey();
        // ??????
        Signature signature = Signature.getInstance(x509Certificate
                .getSigAlgName());
        signature.initVerify(publicKey);
        signature.update(data);

        return signature.verify(decryptBASE64(sign));

    }

    public static Certificate getCertificate(String certificatePath)
            throws Exception {
        CertificateFactory certificateFactory = CertificateFactory
                .getInstance(X509);
        FileInputStream in = new FileInputStream(certificatePath);

        Certificate certificate = certificateFactory
                .generateCertificate(in);
        in.close();

        return certificate;
    }

    public static Certificate getCertificate(String keyStorePath,
            String alias, String password) throws Exception {
        KeyStore ks = getKeyStore(keyStorePath, password);
        Certificate certificate = ks.getCertificate(alias);

        return certificate;
    }

    public static PublicKey getPublicKey(String certificatePath)
            throws Exception {
        Certificate certificate = getCertificate(certificatePath);
        PublicKey key = certificate.getPublicKey();
        return key;
    }

    private static byte[] decryptBASE64(String sign) {
        try {
            return new sun.misc.BASE64Decoder().decodeBuffer(sign);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            return null;
        }

    }

    public static KeyStore getKeyStore(String keyStorePath, String password)
            throws Exception {
        FileInputStream is = new FileInputStream(keyStorePath);
        KeyStore ks = KeyStore.getInstance(KEY_STORE);
        ks.load(is, password.toCharArray());
        is.close();
        return ks;
    }
}

Related Tutorials