Java tutorial
/** * Copyright(c) Foresee Science & Technology Ltd. */ package com.icanft.common.startup; import java.io.FileInputStream; import java.security.PublicKey; import java.security.Signature; import java.util.Date; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.icanft.common.util.DateTimeUtil; import com.icanft.common.util.InternationalizationUtil; /** * <pre> * ?License * </pre> * * @author luxiaocheng luxiaocheng@foresee.cn * @version 1.00.00 * * <pre> * * ?: : : * </pre> */ public class ValidateLicense { private static final Log log = LogFactory.getLog(ValidateLicense.class); /** * License * * @param licenseColl LicenseColl * @param path String * @return boolean */ public static boolean validateLicense(LicenseColl licenseColl, String path) { // ?? boolean payedUser = true; List<String> macAddressList = CollectMacAddress.getMacAddress(); for (LicenseInfo licenseInfo : licenseColl.getLicenseInfoList()) { String productName = licenseInfo.getProductName(); String expirationDate = licenseInfo.getExpiration(); String signature = licenseInfo.getSignature(); boolean flag = false; String data = new StringBuffer().append(productName).append(expirationDate).toString(); java.security.cert.CertificateFactory cf; try { cf = java.security.cert.CertificateFactory.getInstance("X.509"); java.security.cert.Certificate cert = cf .generateCertificate(new FileInputStream(path + "/WEB-INF/fbrp.cer")); // PublicKey pubKey = cert.getPublicKey(); // ??Signature Signature sig = Signature.getInstance("SHA1withDSA"); sig.initVerify(pubKey); sig.update(InternationalizationUtil.getBytes(data)); // ?License boolean verifies = true; // sig.verify(Base64.decodeBase64(InternationalizationUtil // .getBytes(signature))); if (verifies) { Date evalDate = DateTimeUtil.parseDate(expirationDate); if (evalDate.before(new Date())) { // ? payedUser = false; } else { flag = true; payedUser = true; System.out.println( "" + productName + " License" + expirationDate + "?"); } } } catch (Exception e) { log.error("", e); } if (!flag) { payedUser = false; System.out.println( "" + productName + " License??License?"); } } return payedUser; } }