license.ExtraTestWakeLicense.java Source code

Java tutorial

Introduction

Here is the source code for license.ExtraTestWakeLicense.java

Source

package license;

import java.net.NetworkInterface;
import java.security.MessageDigest;
import java.security.PublicKey;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.Map;

import license.blowfish.Blowfish;
import license.rsa.RSACoder;

import org.apache.commons.codec.binary.Base64;
import org.junit.Before;
import org.junit.Test;

/**
 * wake****
 * rsa???
 * 
 * ----------------------------------------------
 * TODO:TestWakeLicense
 * ----------------------------------------------
 * 
 * @author byht
 *
 */
public class ExtraTestWakeLicense {

    //1.?
    ProjectInfo pi = new ProjectInfo();

    /**
     * ??string
     */
    private String testData;

    /**
     *  **  *?*
     */
    private static String publicKey;
    private String privateKey;

    @Before
    public void setUp() throws Exception {
        Map<String, Object> keyMap = RSACoder.initKey();

        publicKey = RSACoder.getPublicKey(keyMap);
        privateKey = RSACoder.getPrivateKey(keyMap);
        System.err.println(": \n\r" + publicKey);
        System.err.println("? \n\r" + privateKey);
    }

    /**
     * wakelicense
     * @throws Exception
     */
    @Test
    public void testWake() throws Exception {

        pi.setClient("");
        pi.setName("");
        pi.setContact("");
        pi.setTel("15000100001");
        pi.setEmail("dcm19890101@163.com");

        String info = projectInfoToString(pi);
        System.out.println(
                "---===(*****?info.getBytes()?Register.data)"
                        + info);

        System.out.println();
        System.out.println();

        readProjectInfo(pi, info);

        System.out.println();
        System.out.println();

        setInput(pi);

        System.out.println();
        System.out.println();

        //??testData
        testData = toString(pi);
        System.out.println(testData);

        System.out.println(extraData());

        System.out.println();
        System.out.println();

        //
        //wake*?*license?
        //
        String[] ss = testData.split("\n");
        String blowfishInfo = new Blowfish(Date.class.getName()).decryptString(ss[0]);
        byte[] rmd5 = md5(blowfishInfo);
        //?base64?
        byte[] lmd5 = rsa(new Base64().decode(ss[1]));
        System.out.println("?? " + equalsBytes(rmd5, lmd5));

    }

    /**
     * ??? 
     * @param rmd5
     * @param lmd5
     * @return
     */
    private static boolean equalsBytes(byte[] rmd5, byte[] lmd5) {
        if (rmd5.length == lmd5.length) {
            for (int i = 0; i < rmd5.length; i++)
                if (rmd5[i] != lmd5[i])
                    return false;
            return true;
        }
        return false;
    }

    /**
     * ?
     * @return
     * @throws Exception
     */
    //TODO:??
    static PublicKey readPublicKeyFromFile() throws Exception {
        //       ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(
        //         KeyData.publicKey));
        //       try {
        //         BigInteger m = (BigInteger)oin.readObject();
        //         BigInteger e = (BigInteger)oin.readObject();
        //         RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e);
        //         KeyFactory fact = KeyFactory.getInstance("RSA");
        //         return fact.generatePublic(keySpec);
        //       } finally {
        //         oin.close();
        //       }
        return null;
    }

    /**
     * ?rsa?
     * @param key
     * @return
     * @throws Exception
     */
    //TODO:?
    private static byte[] rsa(byte[] key) throws Exception {
        return RSACoder.decryptByPublicKey(key, publicKey);
    }

    /**
     * blowfish??md5
     * @param info
     * @return
     * @throws Exception
     */
    private static byte[] md5(String info) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < info.length(); i++)
            sb.append(Integer.valueOf(info.charAt(i)));
        return md.digest(sb.toString().getBytes());
    }

    /**
     * jersey.data??
     * @return
     */
    public String extraData() {

        StringBuffer sb = new StringBuffer();
        sb.append("\n:");
        sb.append(pi.getClient());
        sb.append("\n:");
        sb.append(pi.getName());
        sb.append("\n:");
        switch (pi.getType().intValue()) {
        case 0:
            sb.append("?");
            break;
        case 1:
            sb.append("");
            break;
        case 2:
            sb.append("?");
            break;
        }

        sb.append("\n");
        sb.append(new SimpleDateFormat("yyyy-MM-dd").format(new Date(pi.getPeriod().longValue())));

        return sb.toString();
    }

    public String encryptProjectInfo(ProjectInfo pi) {
        return new Blowfish(Date.class.getName()).encryptString(projectInfo(pi));
    }

    private String projectInfo(ProjectInfo pi) {
        StringBuilder sb = new StringBuilder();
        sb.append("1=").append(pi.getName()).append("\n");
        sb.append("2=").append(pi.getClient()).append("\n");
        sb.append("3=").append(pi.getMaxUsers()).append("\n");
        sb.append("4=").append(pi.getMaxOnlineUsers()).append("\n");
        sb.append("5=").append(pi.getType()).append("\n");
        sb.append("6=").append(pi.getPeriod()).append("\n");
        sb.append("7=").append(pi.getEmail()).append("\n");
        sb.append("8=").append(pi.getTel()).append("\n");
        sb.append("9=").append(pi.getContact()).append("\n");
        sb.append("10=").append(pi.getPath()).append("\n");
        sb.append("11=").append(pi.getCpuNumber()).append("\n");
        sb.append("12=").append(pi.getMacName1()).append("\n");
        sb.append("13=").append(pi.getMac1()).append("\n");
        sb.append("14=").append(pi.getMacName2()).append("\n");
        sb.append("15=").append(pi.getMac2()).append("\n");

        System.out.println("----- : " + sb.toString());

        return sb.toString();
    }

    /**
     * ?
     * ;blowfish??
     * ;md5--rsa--base64??
     * 
     */
    public String toString(ProjectInfo pi) {
        System.out.println("?");

        try {
            return encryptProjectInfo(pi) + "\n" + new Base64().encodeAsString(rsa());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //TODO: ?
    byte[] rsa() throws Exception {
        return RSACoder.encryptByPrivateKey(md5(), privateKey);
    }

    byte[] md5() throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        StringBuilder sb = new StringBuilder();
        String info = projectInfo(pi);
        System.out.println("----?md5");
        System.out.println(info);
        System.out.println();
        for (int i = 0; i < info.length(); i++)
            sb.append(Integer.valueOf(info.charAt(i)));
        return md.digest(sb.toString().getBytes());
    }

    /**
     * ?
     * @param pi
     */
    private void setInput(ProjectInfo pi) throws Exception {
        System.out.println("?=====");
        System.out.println();
        System.out.println("License (0: ?, 1: , 2: ?) :------?1 ");
        pi.setType(1);
        System.out.println(" (yyyy-MM-dd) :-----?2013-06-01 ");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        pi.setPeriod(Long.valueOf(sdf.parse("2013-06-01").getTime()));
        System.out.println(" : -----?100");
        pi.setMaxUsers(100);
        System.out.println(" : -----?100");
        pi.setMaxOnlineUsers(100);
    }

    /**
     * ?
     * @param info
     * @throws Exception
     */
    public void readProjectInfo(ProjectInfo pi, String info) throws Exception {
        String[] arrayOfString1;
        System.out.println("???---");
        int j = (arrayOfString1 = new Blowfish(Date.class.getName()).decryptString(info).split("\n")).length;
        for (int i = 0; i < j; i++) {
            String str = arrayOfString1[i];
            String[] s = str.split("=");
            System.out.println(s[0] + "=" + s[1]);
            switch (Integer.valueOf(s[0]).intValue()) {
            case 1:
                pi.setName(s[1]);
                break;
            case 2:
                pi.setClient(s[1]);
                break;
            case 7:
                pi.setEmail(s[1]);
                break;
            case 8:
                pi.setTel(s[1]);
                break;
            case 9:
                pi.setContact(s[1]);
                break;
            case 10:
                pi.setPath(s[1]);
                break;
            case 11:
                pi.setCpuNumber(Integer.valueOf(s[1]));
                break;
            case 12:
                pi.setMacName1(s[1]);
                break;
            case 13:
                pi.setMac1(s[1]);
                break;
            case 14:
                pi.setMacName2(s[1]);
                break;
            case 15:
                pi.setMac2(s[1]);
            case 3:
            case 4:
            case 5:
            case 6:
            }
        }
    }

    private String projectInfoToString(ProjectInfo pi) {
        StringBuilder sb = new StringBuilder();
        sb.append("1=").append(pi.getName()).append("\n");
        sb.append("2=").append(pi.getClient()).append("\n");
        sb.append("3=").append(pi.getMaxUsers()).append("\n");
        sb.append("4=").append(pi.getMaxOnlineUsers()).append("\n");
        sb.append("5=").append(pi.getType()).append("\n");
        sb.append("6=").append(pi.getPeriod()).append("\n");
        sb.append("7=").append(pi.getEmail()).append("\n");
        sb.append("8=").append(pi.getTel()).append("\n");
        sb.append("9=").append(pi.getContact()).append("\n");
        sb.append("10=").append(path()).append("\n");
        sb.append("11=").append(Runtime.getRuntime().availableProcessors()).append("\n");
        try {
            mac(sb);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("----?---====\n" + sb.toString());
        return new Blowfish(Date.class.getName()).encryptString(sb.toString());
    }

    /**
     * ?mac?
     * @param sb
     * @throws Exception
     */
    private static void mac(StringBuilder sb) throws Exception {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        int i = 12;
        Base64 base64 = new Base64();
        for (NetworkInterface ni : Collections.list(interfaces)) {
            if (ni.getName().substring(0, 1).equalsIgnoreCase("e")) {
                sb.append(String.valueOf(i)).append('=').append(ni.getName()).append('\n');
                i++;
                byte[] mac = ni.getHardwareAddress();
                sb.append(String.valueOf(i)).append('=').append(base64.encodeAsString(mac)).append('\n');
                i++;
            }
        }
    }

    /**
     * ?
     * @return
     */
    private static String path() {
        return ProjectInfo.class.getProtectionDomain().getCodeSource().getLocation().getPath().split("lib")[0];
    }

}