- 浏览: 54302 次
- 性别:
- 来自: 湖北
文章分类
- 全部博客 (102)
- ibatis (4)
- spring (12)
- 数据库 (3)
- java (26)
- css (2)
- linux (1)
- hibernate (4)
- Maven (3)
- CMS (1)
- spring mvc (1)
- MyBati (1)
- WEB (1)
- 分布式 (2)
- webservice (2)
- 网络协议 (1)
- TCP (1)
- UDP协议 (1)
- sql优化原则 (1)
- android (1)
- hadoop (10)
- solr (2)
- Scala学习笔记--Actor和并发 (0)
- Spark (4)
- Scala (1)
- hbase (1)
- kafka (1)
- ICE (2)
- 机器学习算法 (2)
- Apache Ignite (1)
- python (1)
- tensorflow (2)
- openstack (1)
- 系统监控 (2)
- 大数据 (1)
- ogg (2)
- Oracle GoldenGate DDL 详细说明 使用手册(较早资料) (0)
- oracle (1)
最新评论
package com.dameng.dmdp.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class License {
private static final String licensePath = "license.dat";
private static final String keyFilename = "key.data";
private static SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd"); //yyyy-MM-dd HH:mm:ss
//Product.title , AppServer
private String title = "AppServer";
//Product.sku, J2EE/CORBA
private String sku = "J2EE/CORBA";
//Serial.number, MAC Address
private byte[] serialNumber;
//Platform, all
private String platform = "";
//Trial.license
private boolean isTrial;
//License.expiry, 2010-05-12
private String expiry = "";
//Test
public static void main(String[] args) throws Exception{
//
InetAddress ia = InetAddress.getLocalHost();
System.out.println(ia);
String mac =getLocalMac(ia);
//为用户生成license,并email给他
License om = new License();
om.setTitle("AppServer");
om.setSku("J2EE/CORBA");
//String mac = getMACAddress();
om.setPlatform("Windows");
om.setTrial(false);
om.setExpiry("2010-05-12");
License.generateLicense(om,mac);
//在程序中校验license
License.validate();
}
//为用户生成license, 也就是将serialNumber字段的MAC加密
public static License generateLicense(License om,String mac){
try{
//密匙不存在,则生成密匙
if(! new File(keyFilename).exists()){
generateKey();
}
//用密匙加密数据
byte rawKeyData[] = readFile(keyFilename); //用某种方法获得密匙数据
DESKeySpec dks = new DESKeySpec( rawKeyData ); //从原始密匙数据创建DESKeySpec对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); //创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象
SecretKey key = keyFactory.generateSecret( dks );
Cipher cipher = Cipher.getInstance( "DES" ); //Cipher对象实际完成加密操作
SecureRandom sr = new SecureRandom(); //DES算法要求有一个可信任的随机数源
cipher.init(Cipher.ENCRYPT_MODE, key, sr ); //用密匙初始化Cipher对象
System.out.println("mac-----"+mac);
byte data[] = mac.getBytes(); //用某种方法获取数据
byte encryptedData[] = cipher.doFinal( data ); //正式执行加密操作
System.out.println("加密后的数据:" + encryptedData);
om.setSerialNumber(encryptedData);
//输出license
write(om);
}catch(Exception e){
e.printStackTrace();
}
return om;
}
//生成密匙
private static boolean generateKey(){
boolean result = false;
String algorithm = "DES";
try{
SecureRandom sr = new SecureRandom(); //DES算法要求有一个可信任的随机数源
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
kg.init( sr );
SecretKey key = kg.generateKey(); //[-68, -68, -57, -71, 42, -125, 32, 13]
result = writeFile(keyFilename, key.getEncoded()); //把密匙数据保存到文件
}catch(Exception e){
e.printStackTrace();
}
return result;
}
public static boolean validate(){
boolean result = false;
try{
License om = read();
String os = System.getProperty("os.name");
if (os.toLowerCase().startsWith(om.getPlatform().toLowerCase()) || "all".equals(om.getPlatform())) {
Date expireDate = sdf.parse(om.getExpiry());
if(expireDate.getTime() - new Date().getTime() < 0){
System.err.println("License过期了");
result = false;
}else{
if(om.isTrial()){
System.out.println("试用License有效");
result = true;
}else{
String mac = getMACAddress();
//用密匙解密数据
byte rawKeyData[] = readFile(keyFilename); //用某种方法获得密匙数据
DESKeySpec dks = new DESKeySpec( rawKeyData ); //从原始密匙数据创建DESKeySpec对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); //创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象
SecretKey key = keyFactory.generateSecret( dks );
Cipher cipher = Cipher.getInstance( "DES" ); //Cipher对象实际完成加密操作
SecureRandom sr = new SecureRandom(); //DES算法要求有一个可信任的随机数源
cipher.init(Cipher.DECRYPT_MODE, key, sr ); //用密匙初始化Cipher对象
byte data[] = om.getSerialNumber(); //用某种方法获取数据
byte encryptedData[] = cipher.doFinal(data); //正式执行加密操作
String tmp = new String(encryptedData);
System.out.println("解密后的数据:" + tmp);
if(mac.equals(tmp)){
System.out.println("License有效");
result = true;
}else{
System.err.println("License无效");
result = false;
}
}
}
}else{
System.err.println("License的平台无效");
result = false;
}
}catch(Exception e){
e.printStackTrace();
}
return result;
}
public static License read(){
License om = null;
try{
om = new License();
ObjectInputStream in = new ObjectInputStream(new FileInputStream(licensePath));
om.setTitle((String)in.readObject());
om.setSku((String)in.readObject());
om.setPlatform((String)in.readObject());
om.setTrial(in.readBoolean());
om.setExpiry((String)in.readObject());
om.setSerialNumber((byte[])in.readObject());
in.close();
}catch(Exception e){
e.printStackTrace();
}
return om;
}
public static boolean write(License om){
boolean result;
try{
File f = new File(licensePath);
if(f.exists())
f.delete();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
out.writeObject(om.getTitle());
out.writeObject(om.getSku());
out.writeObject(om.getPlatform());
out.writeBoolean(om.isTrial());
out.writeObject(om.getExpiry());
out.writeObject(om.getSerialNumber());
out.close();
result = true;
}catch(Exception e){
e.printStackTrace();
result = false;
}
return result;
}
public static boolean writeFile(String path, byte[] bytes){
boolean result = false;
try {
FileOutputStream fos = new FileOutputStream(path);
fos.write(bytes);
fos.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static byte[] readFile(String path){
byte[] bytes = null;
try {
FileInputStream fin = new FileInputStream(path);
bytes = new byte[fin.available()];
fin.read(bytes);
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
return bytes;
}
public static String getMACAddress() {
String address = "";
String os = System.getProperty("os.name");
if (os.startsWith("Windows")) {
try {
String command = "ipconfig /all";
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream(),"GB2312"));
String line;
while ((line = br.readLine()) != null) {
if (line.toLowerCase().indexOf("physical address") > 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index);
break;
}
}
br.close();
return address.trim();
} catch (IOException e) {
e.printStackTrace();
}
} else if (os.startsWith("Linux")) {
String command = "/bin/sh -c ifconfig -a";
Process p;
try {
p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("HWaddr") > 0) {
int index = line.indexOf("HWaddr") + "HWaddr".length();
address = line.substring(index);
break;
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
address = address.trim();
return address;
}
private static String getLocalMac(InetAddress ia) throws SocketException {
// TODO Auto-generated method stub
//获取网卡,获取地址
byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
System.out.println("mac数组长度:"+mac.length);
StringBuffer sb = new StringBuffer("");
for(int i=0; i<mac.length; i++) {
if(i!=0) {
sb.append("-");
}
//字节转换为整数
int temp = mac[i]&0xff;
String str = Integer.toHexString(temp);
System.out.println("每8位:"+str);
if(str.length()==1) {
sb.append("0"+str);
}else {
sb.append(str);
}
}
System.out.println("本机MAC地址:"+sb.toString().toUpperCase());
return sb.toString().toUpperCase();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSku() {
return sku;
}
public void setSku(String sku) {
this.sku = sku;
}
public byte[] getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(byte[] serialNumber) {
this.serialNumber = serialNumber;
}
public String getPlatform() {
return platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
public boolean isTrial() {
return isTrial;
}
public void setTrial(boolean isTrial) {
this.isTrial = isTrial;
}
public String getExpiry() {
return expiry;
}
public void setExpiry(String expiry) {
this.expiry = expiry;
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class License {
private static final String licensePath = "license.dat";
private static final String keyFilename = "key.data";
private static SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd"); //yyyy-MM-dd HH:mm:ss
//Product.title , AppServer
private String title = "AppServer";
//Product.sku, J2EE/CORBA
private String sku = "J2EE/CORBA";
//Serial.number, MAC Address
private byte[] serialNumber;
//Platform, all
private String platform = "";
//Trial.license
private boolean isTrial;
//License.expiry, 2010-05-12
private String expiry = "";
//Test
public static void main(String[] args) throws Exception{
//
InetAddress ia = InetAddress.getLocalHost();
System.out.println(ia);
String mac =getLocalMac(ia);
//为用户生成license,并email给他
License om = new License();
om.setTitle("AppServer");
om.setSku("J2EE/CORBA");
//String mac = getMACAddress();
om.setPlatform("Windows");
om.setTrial(false);
om.setExpiry("2010-05-12");
License.generateLicense(om,mac);
//在程序中校验license
License.validate();
}
//为用户生成license, 也就是将serialNumber字段的MAC加密
public static License generateLicense(License om,String mac){
try{
//密匙不存在,则生成密匙
if(! new File(keyFilename).exists()){
generateKey();
}
//用密匙加密数据
byte rawKeyData[] = readFile(keyFilename); //用某种方法获得密匙数据
DESKeySpec dks = new DESKeySpec( rawKeyData ); //从原始密匙数据创建DESKeySpec对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); //创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象
SecretKey key = keyFactory.generateSecret( dks );
Cipher cipher = Cipher.getInstance( "DES" ); //Cipher对象实际完成加密操作
SecureRandom sr = new SecureRandom(); //DES算法要求有一个可信任的随机数源
cipher.init(Cipher.ENCRYPT_MODE, key, sr ); //用密匙初始化Cipher对象
System.out.println("mac-----"+mac);
byte data[] = mac.getBytes(); //用某种方法获取数据
byte encryptedData[] = cipher.doFinal( data ); //正式执行加密操作
System.out.println("加密后的数据:" + encryptedData);
om.setSerialNumber(encryptedData);
//输出license
write(om);
}catch(Exception e){
e.printStackTrace();
}
return om;
}
//生成密匙
private static boolean generateKey(){
boolean result = false;
String algorithm = "DES";
try{
SecureRandom sr = new SecureRandom(); //DES算法要求有一个可信任的随机数源
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
kg.init( sr );
SecretKey key = kg.generateKey(); //[-68, -68, -57, -71, 42, -125, 32, 13]
result = writeFile(keyFilename, key.getEncoded()); //把密匙数据保存到文件
}catch(Exception e){
e.printStackTrace();
}
return result;
}
public static boolean validate(){
boolean result = false;
try{
License om = read();
String os = System.getProperty("os.name");
if (os.toLowerCase().startsWith(om.getPlatform().toLowerCase()) || "all".equals(om.getPlatform())) {
Date expireDate = sdf.parse(om.getExpiry());
if(expireDate.getTime() - new Date().getTime() < 0){
System.err.println("License过期了");
result = false;
}else{
if(om.isTrial()){
System.out.println("试用License有效");
result = true;
}else{
String mac = getMACAddress();
//用密匙解密数据
byte rawKeyData[] = readFile(keyFilename); //用某种方法获得密匙数据
DESKeySpec dks = new DESKeySpec( rawKeyData ); //从原始密匙数据创建DESKeySpec对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); //创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象
SecretKey key = keyFactory.generateSecret( dks );
Cipher cipher = Cipher.getInstance( "DES" ); //Cipher对象实际完成加密操作
SecureRandom sr = new SecureRandom(); //DES算法要求有一个可信任的随机数源
cipher.init(Cipher.DECRYPT_MODE, key, sr ); //用密匙初始化Cipher对象
byte data[] = om.getSerialNumber(); //用某种方法获取数据
byte encryptedData[] = cipher.doFinal(data); //正式执行加密操作
String tmp = new String(encryptedData);
System.out.println("解密后的数据:" + tmp);
if(mac.equals(tmp)){
System.out.println("License有效");
result = true;
}else{
System.err.println("License无效");
result = false;
}
}
}
}else{
System.err.println("License的平台无效");
result = false;
}
}catch(Exception e){
e.printStackTrace();
}
return result;
}
public static License read(){
License om = null;
try{
om = new License();
ObjectInputStream in = new ObjectInputStream(new FileInputStream(licensePath));
om.setTitle((String)in.readObject());
om.setSku((String)in.readObject());
om.setPlatform((String)in.readObject());
om.setTrial(in.readBoolean());
om.setExpiry((String)in.readObject());
om.setSerialNumber((byte[])in.readObject());
in.close();
}catch(Exception e){
e.printStackTrace();
}
return om;
}
public static boolean write(License om){
boolean result;
try{
File f = new File(licensePath);
if(f.exists())
f.delete();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
out.writeObject(om.getTitle());
out.writeObject(om.getSku());
out.writeObject(om.getPlatform());
out.writeBoolean(om.isTrial());
out.writeObject(om.getExpiry());
out.writeObject(om.getSerialNumber());
out.close();
result = true;
}catch(Exception e){
e.printStackTrace();
result = false;
}
return result;
}
public static boolean writeFile(String path, byte[] bytes){
boolean result = false;
try {
FileOutputStream fos = new FileOutputStream(path);
fos.write(bytes);
fos.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static byte[] readFile(String path){
byte[] bytes = null;
try {
FileInputStream fin = new FileInputStream(path);
bytes = new byte[fin.available()];
fin.read(bytes);
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
return bytes;
}
public static String getMACAddress() {
String address = "";
String os = System.getProperty("os.name");
if (os.startsWith("Windows")) {
try {
String command = "ipconfig /all";
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream(),"GB2312"));
String line;
while ((line = br.readLine()) != null) {
if (line.toLowerCase().indexOf("physical address") > 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index);
break;
}
}
br.close();
return address.trim();
} catch (IOException e) {
e.printStackTrace();
}
} else if (os.startsWith("Linux")) {
String command = "/bin/sh -c ifconfig -a";
Process p;
try {
p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("HWaddr") > 0) {
int index = line.indexOf("HWaddr") + "HWaddr".length();
address = line.substring(index);
break;
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
address = address.trim();
return address;
}
private static String getLocalMac(InetAddress ia) throws SocketException {
// TODO Auto-generated method stub
//获取网卡,获取地址
byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
System.out.println("mac数组长度:"+mac.length);
StringBuffer sb = new StringBuffer("");
for(int i=0; i<mac.length; i++) {
if(i!=0) {
sb.append("-");
}
//字节转换为整数
int temp = mac[i]&0xff;
String str = Integer.toHexString(temp);
System.out.println("每8位:"+str);
if(str.length()==1) {
sb.append("0"+str);
}else {
sb.append(str);
}
}
System.out.println("本机MAC地址:"+sb.toString().toUpperCase());
return sb.toString().toUpperCase();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSku() {
return sku;
}
public void setSku(String sku) {
this.sku = sku;
}
public byte[] getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(byte[] serialNumber) {
this.serialNumber = serialNumber;
}
public String getPlatform() {
return platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
public boolean isTrial() {
return isTrial;
}
public void setTrial(boolean isTrial) {
this.isTrial = isTrial;
}
public String getExpiry() {
return expiry;
}
public void setExpiry(String expiry) {
this.expiry = expiry;
}
}
发表评论
-
jvm
2018-03-26 09:47 396http://www.cnblogs.com/moonands ... -
多线程
2015-11-11 16:05 351public class ThreadDemo3 { ... -
java之装饰设计模式和继承的简单区别
2015-10-29 16:24 808http://jiangnanlove.iteye.com/b ... -
java注解
2015-10-26 11:18 393Java自定义注解小结 作者:谢伟伦 学习java有两年之余了 ... -
字符串补零除0
2015-10-21 11:55 701//去零操作 String str = "0050 ... -
代理模式与装饰模式差别,适配器模式
2015-10-07 19:29 727http://blog.csdn.net/hitprince/ ... -
jsoup解析html
2015-03-31 11:17 830jsoup:解析HTML用法小结 原文 http://my ... -
java 堆和栈
2014-11-27 15:18 5201.栈(stack)与堆(heap)都是J ... -
java内部类、静态内部类 小结
2014-11-26 14:12 5461)首先,用内部类是因 ... -
java 23种设计模式
2014-11-19 14:56 599http://zz563143188.iteye.com/bl ... -
Java调用webservice接口方法
2014-11-19 14:36 5201. Java调用webservice接口方法 webserv ... -
java基本类型
2014-11-05 14:05 704基本类型比较 -
java修饰符权限
2014-11-05 13:58 601(1)public:可以被所有其他类所访问。 (2)priv ... -
类型转换
2014-11-05 13:44 495short s1 = 1; s1 = s1 + 1;有错,s1 ... -
Sring x = new String("xyz")
2014-11-05 13:32 513只要是new,都是重新分配堆空间,如果不区分栈和堆,这里创建了 ... -
java内部类和静态内部类调用
2014-06-23 14:06 549内部类 public class Test { clas ... -
匿名内部类
2014-06-18 15:00 442匿名内部类也就是没有 ... -
类的加载周期
2014-06-16 12:47 371类什么时候被加载/类加载时机: 第一:生成该类对象的时候,会 ... -
内部类
2014-05-05 14:43 497http://www.cnblogs.com/mengdd/a ... -
工厂模式
2014-03-24 15:17 526举两个例子以快速明白Java中的简单工厂模式: 女娲抟土造人 ...
相关推荐
本文将深入探讨如何使用Java语言来实现一个自定义的License生成器,并结合图形化用户界面(GUI)来提升用户体验。以下是对该主题的详细阐述: 一、Java源码实现License生成器 1. 数据结构:首先,我们需要定义一个...
【HP iLO license 生成器】是针对HP(Hewlett Packard)服务器远程管理功能——集成Lights-Out(Integrated Lights-Out)的一个工具。HP iLO是一个内置的硬件管理控制器,它允许管理员远程监控、配置和管理服务器,...
1. JAVA 编写的 License生成器 2. 采用rsa非对称密钥算法。 3. 打包成jar直接运行。 4. 自动创建明文txt文件,修改后进行一键加密,生成License文件。 5. 优秀的界面操作。 6. 具有较好的不可复制性。
本文将详细介绍SAP License生成软件SapLicGen及其注册码生成过程。 SapLicGen是一款专门用于生成SAP许可证的工具。在企业部署SAP系统时,通常需要根据自身的业务需求和用户数量购买相应的许可证。然而,由于SAP的...
保证java web ,spirngboot,tomcate web安全,可以现在IP,mac,自定义参数,License生成器 (JAVA源码+界面) 其中包括license授权机制的原理和制作license的具体步骤 增加了mac 地址验证
1. JAVA 编写的 License生成器 2. 采用rsa非对称密钥算法。 3. 打包成jar直接运行。 4. 自动创建明文txt文件,修改后进行一键加密,生成License文件。 5. 优秀的界面操作。 6. 具有较好的不可复制性。
自己开发的用于软件license授权的通用解决方案,可以用于自己的软件产品授权保护。源码部署到服务器,客户端用于生成license,生成的license结果服务器验证通过后才视为合法有效的license。
dbeaver--ee dbeaver-ue cloudbeaver-ee LICENSE生成器
"license 生成license文件demo"这个项目主要涉及了如何创建和管理软件授权文件,这对于开发者来说是一个关键的实践过程。在这个过程中,通常会用到公钥和私钥加密技术,以及相关的编程示例(demo)。 首先,我们来...
标题"license生成步骤"中提到的,是关于如何生成这种自定义的许可证证书。通常,这个过程涉及到以下几个关键步骤: 1. **选择许可协议**:根据你的需求,选择合适的开源许可协议或者创建自定义的许可条款。常见的...
License授权组件,包括Java源码以及客户端界面,可生成授权文件。
java license生成验证的实现