📄 licensekeypair.java.svn-base
字号:
package com.richeninfo.license;
import java.io.*;
import com.richeninfo.crypt.*;
import java.util.*;
import java.util.zip.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author xiaolie
* @version 1.0
*/
public class LicenseKeyPair implements java.io.Serializable{
//private Crypt crypt;
private byte[] pubKey;
private byte[] sn;
private long modifiedTime;
String path;
int p1, p2, p3;
static Properties properties = System.getProperties();
private static Hashtable pairs;
private String name;
public LicenseKeyPair(String name, String pubKey, String sn, String path) throws Exception {
this.path = path;
this.name = name;
//System.err.println("LicenseKeyPair.LicenseKeyPair(String pubKey, String sn)");
Crypt crypt = Crypt.getCrypt(path + "read.dat");
//System.err.println("LicenseKeyPair.LicenseKeyPair(String pubKey, String sn) 1");
this.pubKey = crypt.encrypt(pubKey.getBytes());
this.sn = crypt.encrypt(sn.getBytes());
//System.err.println("LicenseKeyPair.LicenseKeyPair(String pubKey, String sn) 2");
}
int getP1() {
return p1;
}
int getP2() {
return p2;
}
int getP3() {
return p3;
}
public String getSn() throws Exception {
Crypt crypt = Crypt.getCrypt(path + "read.dat");
return new String(crypt.decrypt(sn));
}
public String getPubKey() throws Exception {
Crypt crypt = Crypt.getCrypt(path + "read.dat");
return new String(crypt.decrypt(pubKey));
}
public long getModifiedTime() throws Exception {
return modifiedTime;
}
private String getP(String sn2, String sn1, int n) {
StringBuffer s = new StringBuffer(4);
for (int i = 0; i < 4; i++) {
int index = (4*n) + i;
char value = (char)(sn2.charAt(index) - sn1.charAt(index) + '0');
s.append(value);
}
return s.toString();
}
private long getC(String sn2, String sn1) throws NumberFormatException {
StringBuffer s = new StringBuffer(12);
for (int i = 12; i < 24; i++) {
char value = (char)(sn2.charAt(i) - sn1.charAt(i) + '0');
s.append(value);
}
return Long.parseLong(s.toString());
}
public boolean isRightSn() throws Exception {
String sn2 = getSn();
String sn1 = getSn(getPubKey(), path);
//System.out.println("sn1=" + sn1);
String params;
long c;
try {
String p = getP(sn2, sn1, 0);
params = p;
p1 = Integer.parseInt(p);
p = getP(sn2, sn1, 1);
params += p;
p2 = Integer.parseInt(p);
p = getP(sn2, sn1, 2);
params += p;
p3 = Integer.parseInt(p);
c = getC(sn2, sn1);
} catch (NumberFormatException e) {
return false;
}
CRC32 crc = new CRC32();
crc.update(params.getBytes());
long crcValue = crc.getValue();
if ((c % crcValue) == 0) {
return true;
} else {
return false;
}
}
public static LicenseKeyPair loadFromeFile(String name) throws Exception {
//System.out.println("LicenseKeyPair.loadFromFile :name=" + name);
if (pairs == null) {
try {
FileInputStream istream = new FileInputStream(properties.getProperty("java.home") + "/" + "License.data");
ObjectInputStream p = null;
try {
p = new ObjectInputStream(istream);
pairs = (Hashtable)p.readObject();
} finally {
istream.close();
}
if (!pairs.containsKey(name)) throw new NotRegisterException ("No Rigister yet now");
LicenseKeyPair keyPair = (LicenseKeyPair)pairs.get(name);
istream = new FileInputStream(properties.getProperty("java.home") + "/" + "serial.data");
p = new ObjectInputStream(istream);
byte[] t = (byte[])p.readObject();
istream.close();
Crypt cr = Crypt.getCrypt(keyPair.path + "read.dat");
String s = new String(cr.decrypt(t));
keyPair.modifiedTime = Long.parseLong(s);
return keyPair;
} catch (InvalidClassException ie) {
System.out.println("InvalidClassException:" + ie.toString());
File f= new File(properties.getProperty("java.home") + "/" + "License.data");
if (f.exists()) {
f.delete();
}
f = new File(properties.getProperty("java.home") + "/" + "serial.data");
if (f.exists()) {
f.delete();
}
//throw new NotRegisterException ("No Rigister yet now");
} catch (FileNotFoundException e) {
//System.out.println("FileNotFoundException:" + e.toString());
//throw new NotRegisterException ("No Rigister yet now");
}
}
if (pairs != null && pairs.containsKey(name)) {
return (LicenseKeyPair)pairs.get(name);
}
throw new NotRegisterException ("No Rigister yet now");
}
public void saveToFile() throws Exception {
Crypt crypt = Crypt.getCrypt(path + "read.dat");
FileOutputStream ostream = new FileOutputStream(properties.getProperty("java.home") + "/" + "License.data");
ObjectOutputStream p = new ObjectOutputStream(ostream);
if (pairs == null) {
pairs = new Hashtable();
}
//if (!pairs.containsKey(name)) {
pairs.put(name, this);
//}
Enumeration enu = pairs.keys();
while (enu.hasMoreElements()) {
String n = (String)enu.nextElement();
LicenseKeyPair kp = (LicenseKeyPair)pairs.get(n);
kp.modifiedTime = 0;
}
p.writeObject(pairs);
p.flush();
ostream.close();
File f = new File(properties.getProperty("java.home") + "/" + "License.data");
modifiedTime = f.lastModified();
enu = pairs.keys();
while (enu.hasMoreElements()) {
String n = (String)enu.nextElement();
LicenseKeyPair kp = (LicenseKeyPair)pairs.get(n);
kp.modifiedTime = modifiedTime;
}
String s = Long.toString(modifiedTime);
byte[] t = crypt.encrypt(s.getBytes());
ostream = new FileOutputStream(properties.getProperty("java.home") + "/" + "serial.data");
p = new ObjectOutputStream(ostream);
p.writeObject(t);
ostream.close();
}
public static String getSn(String pubKey, String path) throws Exception {
Crypt crypt = Crypt.getCrypt(path + "SecretKey.dat");
byte[] cryptedKey = crypt.encrypt(pubKey.getBytes());
StringBuffer snBuf = new StringBuffer(cryptedKey.length);
for (int i = 0; i < cryptedKey.length; i++) {
int k = Math.abs(cryptedKey[i]);
byte b = (byte)(k % 15 + 65);
char c = (char)b;
snBuf.append(c);
}
return snBuf.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -