kr.co.exsoft.eframework.util.LicenseUtil.java Source code

Java tutorial

Introduction

Here is the source code for kr.co.exsoft.eframework.util.LicenseUtil.java

Source

package kr.co.exsoft.eframework.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.net.URL;
import java.io.File;
import java.security.Key;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.util.StringTokenizer;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import org.apache.commons.lang.StringUtils;
import java.net.URLDecoder;

/***
 * ??  ?
 * @author  
 * @since 2014.07.21
 * @version 3.0
 *
 */
public class LicenseUtil {

    public static void main(String[] args) {

        String ret = "";

        if (args.length == 1) {

            // 
            ret = licenseDecrpt(args[0]);
            System.out.println("?? =" + ret);

        } else if (args.length == 2) {

            // 
            // LICENSE_TYPE = NAMED -  ??  / CONCURRENT -  ?? 
            // USER_COUNT = 0 -  /  ? ?
            ret = licenseEncrypt(args[0], Integer.parseInt(args[1]));
            System.out.println("?? =" + ret);

        } else {
            System.out.println("Usage: LicenseUtil licenseType userCount");
        }

    }

    /**
     * EDMS ?? 
     * @param licenseType
     * @param userCount
     * @return String
     */
    public static String licenseEncrypt(String licenseType, int userCount) {

        String licenseKey = "";

        try {
            licenseKey = generateLicenseKey(licenseType, userCount);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return licenseKey;
    }

    /***
     * EDMS ?? 
     * @param licenseKey
     * @return String
     */
    public static String licenseDecrpt(String licenseKey) {

        String license = "";

        try {
            license = LicenseUtil.decipherLicenseKey(licenseKey);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return license;
    }

    /**
     * ??  ?.
     * 
     * @param licenseType
     * @param userCount
     * @return String
     */
    public static String generateLicenseKey(String licenseType, int userCount) {

        String ksPass = "loveboat";
        String keyPass = "loveboat";
        String alias = "ab942e0f-9e4a-44b9-9f82-0a5f5d48ba12";
        String ret = null;

        try {
            // ??   
            URL url = ClassLoader.getSystemResource("kr/co/exsoft/eframework/cert/exsoft.pfx");
            FileInputStream certfis = new FileInputStream(new File(url.getFile()));

            // Private Key ?.
            BufferedInputStream ksbufin = new BufferedInputStream(certfis);

            KeyStore ks = KeyStore.getInstance("PKCS12");
            ks.load(ksbufin, ksPass.toCharArray());

            PrivateKey key = (PrivateKey) ks.getKey(alias, keyPass.toCharArray());

            // ??  ?.
            ret = spell("EDMsl|" + licenseType + "|" + userCount + "|", key);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return ret;
    }

    /**
     * 
     * <pre>
     * 1.  : ??   APPLICATION
     * 2.  :
     * </pre>
     * @Method Name : decipherLicenseKey
     * @param licenseKey
     * @return String
     * @throws Exception
     */
    public static String decipherLicenseKey(String licenseKey) throws Exception {

        String ret = null;

        if (StringUtils.isNotBlank(licenseKey)) {

            // ??   ? public key ?
            URL url = ClassLoader.getSystemResource("kr/co/exsoft/eframework/cert/exsoft.cer");
            FileInputStream certfis = new FileInputStream(new File(url.getFile()));

            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            Certificate cert = cf.generateCertificate(certfis);

            PublicKey key = cert.getPublicKey();

            // ??  .
            ret = unspell(licenseKey, key);
        }

        return ret;
    }

    /**
     * 
     * <pre> 
     * 1.  :  ??   WAS
     * 2.  :
     * </pre>
     * @Method Name : decipherLicenseKeyWeb
     * @param licenseKey
     * @return String
     * @throws Exception
     */
    public static String decipherLicenseKeyWeb(String licenseKey) throws Exception {

        String ret = null;

        if (StringUtils.isNotBlank(licenseKey)) {

            // URLDecoder - ? ?   
            String certName = URLDecoder.decode(
                    LicenseUtil.class.getResource("/").getPath() + "kr/co/exsoft/eframework/cert/exsoft.cer",
                    "utf-8");

            //  WAS  
            if (certName.indexOf("WEB-INF/") != -1) {
                certName = certName.replace("WEB-INF", "WEB-INF/");
            }

            FileInputStream certfis = null;

            try {
                certfis = new FileInputStream(certName);
            } catch (Exception e) {
                // JBOSS?  Jboss system property?   .
                // Jboss system  ? standalone.xml ? 
                /*<system-properties>
                   <property name="org.apache.catalina.connector.URI_ENCODING" value="UTF-8" />
                   <property name="org.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING" value="true" />
                   <property name="exsoft.license.cert.path" value="D:/jboss-eap-6.3/standalone/deployments/EDMS3.0.war/WEB-INF/classes" /> 
                </system-properties>*/
                // Jboss exception ?  WAS System property    

                certName = URLDecoder.decode(
                        System.getProperty("exsoft.license.cert.path") + "/kr/co/exsoft/eframework/cert/exsoft.cer",
                        "utf-8");

                try {
                    certfis = new FileInputStream(certName);
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }

            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            Certificate cert = cf.generateCertificate(certfis);

            PublicKey key = cert.getPublicKey();

            // ??  .
            ret = unspell(licenseKey, key);
        }

        return ret;
    }

    /**
     * .
     * 
     * @param word
     * @param key
     * @return String
     * @throws NoSuchPaddingException 
     * @throws NoSuchAlgorithmException 
     */
    private static String spell(String word, Key key) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] cleartext = word.getBytes();
        byte[] ciphertext = cipher.doFinal(cleartext);

        return getString(ciphertext);
    }

    /**
     * .
     * 
     * @param word
     * @param key
     * @return String
     */
    private static String unspell(String word, Key key) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, key);

        byte[] ciphertext = getBytes(word);
        byte[] cleartext = cipher.doFinal(ciphertext);

        return new String(cleartext);
    }

    private static String getString(byte[] bytes) {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < bytes.length; i++) {
            byte b = bytes[i];
            sb.append((int) (0x00FF & b));
            if (i + 1 < bytes.length) {
                sb.append("-");
            }
        }

        return sb.toString();
    }

    private static byte[] getBytes(String str) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        StringTokenizer st = new StringTokenizer(str, "-", false);

        while (st.hasMoreTokens()) {
            int i = Integer.parseInt(st.nextToken());
            bos.write((byte) i);
        }

        return bos.toByteArray();
    }

}