javalicense验证⽅案_truelicense实现JAVA的license机制
(包。。。
⼀、⾸先介绍下license授权机制的原理:
1、 ⽣成密钥对,⽅法有很多。
2、 授权者保留私钥,使⽤私钥对包含授权信息(如使⽤截⽌⽇期,MAC地址等)的license进⾏数字签名。
3、 公钥给使⽤者(放在验证的代码中使⽤),⽤于验证license是否符合使⽤条件。
接下来是本例制作license的具体步骤:
⼆、第⼀步:使⽤keytool⽣成密钥对
以下命令在dos命令⾏执⾏,注意当前执⾏⽬录,最后⽣成的密钥对即在该⽬录下:
1、⾸先要⽤KeyTool⼯具来⽣成私匙库:(-alias别名 –validity 3650表⽰10年有效)
keytool -genkey -alias privatekey -keystore privateKeys.store -validity 3650
2、然后把私匙库内的公匙导出到⼀个⽂件当中:
keytool -export -alias privatekey - -keystore privateKeys.store
3、然后再把这个证书⽂件导⼊到公匙库:
keytool -import -alias publiccert - -keystore publicCerts.store
最后⽣成⽂件privateKeys.store、publicCerts.store拷贝出来备⽤。
三、第⼆步:⽣成证书(该部分代码由授权者独⽴保管执⾏)
1、 ⾸先LicenseManagerHolder.java类:
lina.license;
import de.schlichtherle.license.LicenseManager;
import de.schlichtherle.license.LicenseParam;
public class LicenseManagerHolder {
private static LicenseManager licenseManager;
public static synchronized LicenseManager getLicenseManager(LicenseParam licenseParams) {
if (licenseManager == null) {
licenseManager = new LicenseManager(licenseParams);
}
return licenseManager;
}
} ···
2、 然后是主要⽣成license的代码CreateLicense.java:
lina.license;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
DateFormat;
ParseException;
SimpleDateFormat;
import java.util.Properties;
import java.util.prefs.Preferences;
import javax.security.auth.x500.X500Principal;
import de.schlichtherle.license.CipherParam;
import de.schlichtherle.license.DefaultCipherParam; import de.schlichtherle.license.DefaultKeyStoreParam; import de.schlichtherle.license.DefaultLicenseParam; import de.schlichtherle.license.KeyStoreParam;
import de.schlichtherle.license.LicenseContent;
import de.schlichtherle.license.LicenseParam;
import de.schlichtherle.license.LicenseManager;java源代码加密
/**
* CreateLicense
* @author melina
*/
public class CreateLicense {
//common param
private static String PRIVATEALIAS = "";
private static String KEYPWD = "";
private static String STOREPWD = "";
private static String SUBJECT = "";
private static String licPath = "";
private static String priPath = "";
//license content
private static String issuedTime = "";
private static String notBefore = "";
private static String notAfter = "";
private static String consumerType = "";
private static int consumerAmount = 0;
private static String info = "";
/
/ 为了⽅便直接⽤的API⾥的例⼦
// X500Princal是⼀个证书⽂件的固有格式,详见API
private final static X500Principal DEFAULTHOLDERANDISSUER = new X500Principal( "CN=Duke、OU=JavaSoft、O=Sun Microsystems、C=US");
public void setParam(String propertiesPath) {
// 获取参数
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream(propertiesPath);
try {
prop.load(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PRIVATEALIAS = Property("PRIVATEALIAS");
KEYPWD = Property("KEYPWD");
STOREPWD = Property("STOREPWD");
SUBJECT = Property("SUBJECT");
KEYPWD = Property("KEYPWD");
licPath = Property("licPath");
priPath = Property("priPath");
//license content
issuedTime = Property("issuedTime");
notBefore = Property("notBefore");
notAfter = Property("notAfter");
consumerType = Property("consumerType");
consumerAmount = Integer.Property("consumerAmount"));
info = Property("info");
}
public boolean create() {
try {
/************** 证书发布者端执⾏ ******************/
LicenseManager licenseManager = LicenseManagerHolder
.getLicenseManager(initLicenseParams0());
licenseManager.store((createLicenseContent()), new File(licPath));
} catch (Exception e) {
e.printStackTrace();
System.out.println("客户端证书⽣成失败!");
return false;
}
System.out.println("服务器端⽣成证书成功!");
return true;
}
// 返回⽣成证书时需要的参数
private static LicenseParam initLicenseParams0() {
Preferences preference = Preferences
.userNodeForPackage(CreateLicense.class);
// 设置对证书内容加密的对称密码
CipherParam cipherParam = new DefaultCipherParam(STOREPWD);
// 参数1,2从哪个Resource()获得密钥库;参数3密钥库的别名;参数4密钥库存储密码;参数5密钥库密码KeyStoreParam privateStoreParam = new DefaultKeyStoreParam(
CreateLicense.class, priPath, PRIVATEALIAS, STOREPWD, KEYPWD);
LicenseParam licenseParams = new DefaultLicenseParam(SUBJECT,
preference, privateStoreParam, cipherParam);
return licenseParams;
}
// 从外部表单拿到证书的内容
public final static LicenseContent createLicenseContent() {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
LicenseContent content = null;
content = new LicenseContent();
content.setSubject(SUBJECT);
content.setHolder(DEFAULTHOLDERANDISSUER);
content.setIssuer(DEFAULTHOLDERANDISSUER);
try {
content.setIssued(format.parse(issuedTime));
content.setNotBefore(format.parse(notBefore));
content.setNotAfter(format.parse(notAfter));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
content.setConsumerType(consumerType);
content.setConsumerAmount(consumerAmount);
content.setInfo(info);
// 扩展
content.setExtra(new Object());
return content;
}
}
3、 测试程序licenseCreateTest.java:
```lina.license;
lina.license.CreateLicense;
public class licenseCreateTest {
public static void main(String[] args){
CreateLicense cLicense = new CreateLicense();
//获取参数
cLicense.setParam("./param.properties");
//⽣成证书
}
}
4、 ⽣成时使⽤到的param.properties⽂件如下:
```>>common parameters>>#
#alias
PRIVATEALIAS=privatekey
#key(该密码⽣成密钥对的密码,需要妥善保管,不能让使⽤者知道) KEYPWD=bigdata123456
#STOREPWD(该密码是在使⽤keytool⽣成密钥对时设置的密钥库的访问密码) STOREPWD=abc123456
#SUBJECT

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论