一、加密解密
package com.bobonew.Unitl;
import java.security.Key;
import java.security.Security;
import java.util.Calendar;
import java.util.Date;
import javax.crypto.Cipher;
/**
* 注册码加密解密类
* @author czz 2017-1-5
*
*/
public class DesUtils {
/** 字符串默认键值 */
private static String strDefaultKey = "czz01314";
public static String TwoKey = "01470258";
private Cipher encryptCipher = null;
/** 解密工具 */
private Cipher decryptCipher = null;
/**
* 创建激活码
* @param cmpCode
* @param useTime 4位数 如:0365
*/
public static String createRegcode(String cmpCode, String useTime){
String code = createNum(cmpCode, useTime);
try {
DesUtils des = new DesUtils(DesUtils.strDefaultKey);
return des.encrypt(code);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 验证激活码是否有效
* @param cmpCode
* @param regcode
* @return 如果有效返回天数,无效返回0;
*/
public static int verifyActive(String cmpCode, String regcode) {
String num = getNum(regcode, DesUtils.strDefaultKey);
if (num == null || num.length() != BASE_LEN) {
return 0;
}
try {
String startTime = num.substring(0, 10);
String cmpc = num.substring(10, 18);
String useTime = num.substring(18, 22);
String endTime = num.substring(22, BASE_LEN);
if (!cmpc.equals(putZero(cmpCode))) {
return 0;
}
if (isTrueTime(startTime, endTime, useTime)) {
return Integer.parseInt(useTime);
}
} catch (Exception e) {
return 0;
}
return 0;
}
/*********************加密解密天数********************/
/**
* 加密天数(也可用于激活码二次加密保存数据库)
* @param time
* @return
*/
public static String timeEncrypt(String time){
try {
DesUtils des = new DesUtils(DesUtils.TwoKey);
return des.encrypt(time);
} catch (Exception e) {
return null;
}
}
/**
* 解密天数
* @param code
* @return
*/
public static int timeDecrypt(String code){
try {
DesUtils des = new DesUtils(DesUtils.TwoKey);
String time = des.decrypt(code);
return Integer.valueOf(time);
} catch (Exception e) {
return 0;
}
}
/**
* 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18