java 绑定mac地址_如何JAVA实现使用TrueLicense在证书中绑定PC的MAC地址,防止止拷贝应用...

/*** 一、首先介绍下license授权机制的原理:1、 生成密钥对,方法有很多。2、 授权者保留私钥,使用私钥对包含授权信息(如使用截止日期,MAC地址等)的license进行数字签名。3、 公钥给使用者(放在验证的代码中使用),用于验证license是否符合使用条件。***/package cn.melina.license;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.text.DateFormat;import java.text.ParseException;import java.text.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;/** * CreateLicense * @author melina */public class CreateLicense {//common paramprivate static String PRIVATEALIAS = "";private static String KEYPWD = "";private static String STOREPWD = "";private static String SUBJECT = "";private static String licPath = "";private static String priPath = "";//license contentprivate 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是一个证书文件的固有格式,详见APIprivate 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 blocke.printStackTrace();}PRIVATEALIAS = prop.getProperty("PRIVATEALIAS");KEYPWD = prop.getProperty("KEYPWD");STOREPWD = prop.getProperty("STOREPWD");SUBJECT = prop.getProperty("SUBJECT");KEYPWD = prop.getProperty("KEYPWD");licPath = prop.getProperty("licPath");priPath = prop.getProperty("priPath");//license contentissuedTime = prop.getProperty("issuedTime");notBefore = prop.getProperty("notBefore");notAfter = prop.getProperty("notAfter");consumerType = prop.getProperty("consumerType");consumerAmount = Integer.valueOf(prop.getProperty("consumerAmount"));info = prop.getProperty("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从哪个Class.getResource()获得密钥库;参数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 blocke.printStackTrace();}content.setConsumerType(consumerType);content.setConsumerAmount(consumerAmount);content.setInfo(info);// 扩展content.setExtra(new Object());return content;}}

取消

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值