com.litt.core.security.license.LicenseService.java Source code

Java tutorial

Introduction

Here is the source code for com.litt.core.security.license.LicenseService.java

Source

package com.litt.core.security.license;

import java.io.File;

import org.dom4j.Document;
import org.dom4j.Element;

import com.litt.core.license.License;
import com.litt.core.license.LicenseManager;
import com.litt.core.security.Algorithm;
import com.litt.core.security.ISecurity;
import com.litt.core.security.algorithm.DESTool;
import com.litt.core.security.license.gui.Gui;
import com.litt.core.util.ResourceUtils;
import com.litt.core.util.XmlUtils;

public class LicenseService {

    /**
     * ?License
     * @param homePath
     * @param license
     * @param priKeyFile
     * @param licenseFile
     * @throws Exception
     */
    public void save(String homePath, String productCode, String customerCode, License license, File priKeyFile,
            File licenseFile) throws Exception {
        //????
        DigitalSignatureTool utils = new DigitalSignatureTool("DSA");
        utils.readPriKey(priKeyFile.getAbsolutePath()); //??
        String signedData = utils.sign(license.toString()); //??????
        license.setSignature(signedData);

        LicenseManager.writeLicense(license, licenseFile);

        String licenseContent = XmlUtils.readXml(licenseFile).asXML();
        //DES
        ISecurity security = new DESTool(license.getLicenseId(), Algorithm.BLOWFISH);
        licenseContent = security.encrypt(licenseContent);
        //config.xml
        File configFile = ResourceUtils.getFile(homePath + File.separator + "config.xml");
        Document document = XmlUtils.readXml(configFile);
        Element rootE = document.getRootElement();
        Element produceNode = (Element) document.selectSingleNode("//product[@code='" + productCode + "']");

        Element customerElement = produceNode.addElement("customer");
        customerElement.addAttribute("code", customerCode);
        customerElement.addElement("licenseId").setText(license.getLicenseId());
        customerElement.addElement("encryptedLicense").setText(licenseContent);

        XmlUtils.writeXml(configFile, document);
        XmlUtils.formatXml(configFile, XmlUtils.FORMAT_PREETY);
    }

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

    }

}