Des加密解密,创建激活码,验证等--笔记

本文介绍了使用DES算法进行加密解密的步骤,并详细阐述了如何运用此技术创建激活码以及验证激活码的有效性,同时提及了结合定时器进行相关操作的流程。
摘要由CSDN通过智能技术生成

一、加密解密

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值