Java基础、中级、高级、架构面试资料

Java生成MyEclipse注册码的两种方法

JAVA herman 3544浏览 0评论
公告:“业余草”微信公众号提供免费CSDN下载服务(只下Java资源),关注业余草微信公众号,添加作者微信:xttblog2,发送下载链接帮助你免费下载!
本博客日IP超过2000,PV 3000 左右,急需赞助商。
极客时间所有课程通过我的二维码购买后返现24元微信红包,请加博主新的微信号:xttblog2,之前的微信号好友位已满,备注:返现
受密码保护的文章请关注“业余草”公众号,回复关键字“0”获得密码
所有面试题(java、前端、数据库、springboot等)一网打尽,请关注文末小程序
视频教程免费领
腾讯云】1核2G5M轻量应用服务器50元首年,高性价比,助您轻松上云

最近安装了一个MyEclipse,需要用到注册码。就在网上找了两种用java代码的实现方法。两种方法产生的注册码不一致,不过都可以使用。
方法一的代码如下:

package com.xttblog.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * java生成MyEclipse注册码
 * @author 业余草 www.xttblog.com
 * @date 2016年6月12日16:09:20
 */
public class GenKeyUtil {
	private static final String LL = "业余草:www.xttblog.com";  
    public String getSerial(String userId, String type) {  
        NumberFormat nf = new DecimalFormat("000");  
        Calendar cal = Calendar.getInstance();  
        cal.add(Calendar.YEAR, 3);  
        cal.add(Calendar.DAY_OF_YEAR, -1);  
        String licenseNum = nf.format((int) (Math.random() * 1000));  
        String expTime = new StringBuilder("-").append(  
                new SimpleDateFormat("yyMMdd").format(cal.getTime())).append(  
                "0").toString();  
        String need = new StringBuilder(userId.substring(0, 1)).append("Y")  
                .append(type).append("-100").append(licenseNum).append(expTime)  
                .toString();  
        String dx = new StringBuilder(need).append(LL).append(userId)  
                .toString();  
        int suf = this.decode(dx);  
        String code = new StringBuilder(need).append(String.valueOf(suf))  
                .toString();  
        return this.change(code);  
    }  
  
    private int decode(String s) {  
        int i;  
        char[] ac;  
        int j;  
        int k;  
        i = 0;  
        ac = s.toCharArray();  
        j = 0;  
        k = ac.length;  
        while (j < k) {  
            i = (31 * i) + ac[j];  
            j++;  
        }  
        return Math.abs(i);  
    }  
  
    private String change(String s) {  
        byte[] abyte0;  
        char[] ac;  
        int i;  
        int k;  
        int j;  
        abyte0 = s.getBytes();  
        ac = new char[s.length()];  
        i = 0;  
        k = abyte0.length;  
        while (i < k) {  
            j = abyte0[i];  
            if ((j >= 48) && (j <= 57)) {  
                j = (((j - 48) + 5) % 10) + 48;  
            } else if ((j >= 65) && (j <= 90)) {  
                j = (((j - 65) + 13) % 26) + 65;  
            } else if ((j >= 97) && (j <= 122)) {  
                j = (((j - 97) + 13) % 26) + 97;  
            }  
            ac[i] = (char) j;  
            i++;  
        }  
        return String.valueOf(ac);  
    }  
  
    public static void main(String[] args) {  
        try {  
            System.out.println("please input register name:");  
            BufferedReader reader = new BufferedReader(new InputStreamReader(  
                    System.in));  
            String userId = null;  
            userId = reader.readLine();  
            if (userId == null || "".equals(userId)) {  
                System.out.println("name is null");  
                System.exit(0);  
            }  
            GenKeyUtil myeclipsegen = new GenKeyUtil();  
            String res = myeclipsegen.getSerial(userId, "E3MS");  
            System.out.println("Serial:" + res);  
            reader.readLine();  
        } catch (IOException ex) {  
            ex.printStackTrace();  
        }  
    }  
}

方法二的代码如下:

package com.xttblog.util;

import java.math.BigInteger;
import java.util.Date;
import java.util.Random;
import java.util.zip.CRC32;

/**
 * java生成MyEclipse注册码
 * @author 业余草 www.xttblog.com
 * @date 2016年6月12日16:09:20
 */
public class KeyGenUtil {
	
	/**
	 * 获取循环冗余码
	 * @author 业余草 www.xttblog.com
	 */
    private static short getCRC(String str, int i, byte bytes[])  {  
        CRC32 crc32 = new CRC32();  
        if (str != null){  
            for (int j = 0; j < str.length(); j++){  
                crc32.update(str.charAt(j));  
            }  
        }  
        crc32.update(i);  
        crc32.update(i >> 8);  
        crc32.update(i >> 16);  
        crc32.update(i >> 24);  
        for (int k = 0; k < bytes.length - 2; k++) {  
            crc32.update(bytes[k]);  
        }  
        return (short) (int) crc32.getValue();  
    }  
    
    /**
	 * @author 业余草 www.xttblog.com
	 */  
    private static String encodeGroups(BigInteger biginteger) {  
        BigInteger beginner1 = BigInteger.valueOf(0x39aa400L);  
        StringBuilder sb = new StringBuilder();  
        for (int i = 0; biginteger.compareTo(BigInteger.ZERO) != 0; i++){  
            int j = biginteger.mod(beginner1).intValue();  
            if (i > 0){  
                sb.append("-");  
            }  
            sb.append(encodeGroup(j));  
            biginteger = biginteger.divide(beginner1);  
        }  
        return sb.toString();  
    }
    
    /**
	 * @author 业余草 www.xttblog.com
	 */    
    public static String encodeGroup(int i){  
        StringBuilder sb = new StringBuilder();  
        for (int j = 0; j < 5; j++){  
            int k = i % 36;  
            char c;  
            if (k < 10){  
                c = (char) (48 + k);  
            } else {  
                c = (char) ((65 + k) - 10);  
            }  
            sb.append(c);  
            i /= 36;  
        }  
        return sb.toString();  
    }  
    
    /**
	 * @author 业余草 www.xttblog.com
	 */  
    public static String MakeKey(String name, int days, int id){  
        id %= 100000;  
        byte bkey[] = new byte[12];  
        bkey[0] = (byte) 1; 
        bkey[1] = 13; 
        Date d = new Date();  
        long ld = (d.getTime() >> 16);  
        bkey[2] = (byte) (ld & 255);  
        bkey[3] = (byte) ((ld >> 8) & 255);  
        bkey[4] = (byte) ((ld >> 16) & 255);  
        bkey[5] = (byte) ((ld >> 24) & 255);  
        days &= 0xffff;  
        bkey[6] = (byte) (days & 255);  
        bkey[7] = (byte) ((days >> 8) & 255);  
        bkey[8] = 105;  
        bkey[9] = -59;  
        bkey[10] = 0;  
        bkey[11] = 0;  
        int w = getCRC(name, id % 100000, bkey);  
        bkey[10] = (byte) (w & 255);  
        bkey[11] = (byte) ((w >> 8) & 255);  
        BigInteger pow = new BigInteger("89126272330128007543578052027888001981", 10),
        mod = new BigInteger("86f71688cdd2612ca117d1f54bdae029", 16);  
        BigInteger k0 = new BigInteger(bkey),k1 = k0.modPow(pow, mod);  
        String s0 = Integer.toString(id),sz = "0";  
        while (s0.length() != 5){  
            s0 = sz.concat(s0);  
        }  
        s0 = s0.concat("-");  
        s0 = s0.concat(encodeGroups(k1));  
        return s0;  
    }  
  
    public static void main(String[] args){  
        String name = "xttblog.com";  
        Random r = new Random();  
        System.out.println("regName:"+name);  
        System.out.println("regCode:"+MakeKey(name, 0, r.nextInt(100000)));  
    }  
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

业余草公众号

最后,欢迎关注我的个人微信公众号:业余草(yyucao)!可加作者微信号:xttblog2。备注:“1”,添加博主微信拉你进微信群。备注错误不会同意好友申请。再次感谢您的关注!后续有精彩内容会第一时间发给您!原创文章投稿请发送至532009913@qq.com邮箱。商务合作也可添加作者微信进行联系!

本文原文出处:业余草: » Java生成MyEclipse注册码的两种方法