日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

javaEE防盗版-License开发

發(fā)布時間:2023/12/15 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 javaEE防盗版-License开发 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言

開發(fā)的軟件產(chǎn)品在交付使用的時候,往往會授權(quán)一段時間的試用期,這個時候license就派上用場了。不同于在代碼中直接加上時間約束,需要重新授權(quán)的時候使用license可以避免修改源碼,改動部署,授權(quán)方直接生成一個新的license發(fā)送給使用方替換掉原來的license文件即可。下面將講述使用truelicense來實現(xiàn)license的生成和使用。Truelicense是一個開源的證書管理引擎,詳細介紹見https://truelicense.java.net/ license于加密技術(shù)一起使用效果更好。 接下來介紹一下license授權(quán)機制的原理:
1、生成密鑰對,方法有很多。
2、授權(quán)者保留私鑰,使用私鑰對包含授權(quán)信息(如使用截止日期,MAC地址等)的license進行數(shù)字簽名。
3、公鑰給使用者(放在驗證的代碼中使用),用于驗證license是否符合使用條件。

使用方式

1. 生成密鑰對

以下命令在dos命令行執(zhí)行,注意當前執(zhí)行目錄,最后生成的密鑰對即在該目錄下:
1、首先要用KeyTool工具來生成私匙庫:(-alias別名 –validity 3650表示10年有效)
keytool -genkey -alias privatekey -keystore privateKeys.store -validity 3650
?
這里口令使用了noryar123
?
這里的口令是:noryar456
這個時候,會在打開命令行的地方創(chuàng)建出一個文件,privateKeys.store


2、然后把私匙庫內(nèi)的證書導出到一個文件當中:
keytool -export -alias privatekey -file certfile.cer -keystore privateKeys.store
?
口令:noryar123


3、然后再把這個證書文件導入到公匙庫:
keytool -import -alias publiccert -file certfile.cer -keystore publicCerts.store
?
這里的口令,用了:noryar123


最后生成文件privateKeys.store、publicCerts.store拷貝出來備用。
文件位置在用戶目錄下
C:\Users\Administrator
從上面我們可以看到,密鑰一共有兩種:1. 密鑰庫,這個需要配置到服務(wù)器中。2. 密鑰,這個需要保護好,是創(chuàng)建私鑰的時候用的

2. Maven

<!-- https://mvnrepository.com/artifact/de.schlichtherle.truelicense/truelicense-core --> <dependency><groupId>de.schlichtherle.truelicense</groupId><artifactId>truelicense-core</artifactId><version>1.33</version> </dependency> <!-- https://mvnrepository.com/artifact/de.schlichtherle.truelicense/truelicense-xml --> <dependency><groupId>de.schlichtherle.truelicense</groupId><artifactId>truelicense-xml</artifactId><version>1.33</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec --> <dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.10</version> </dependency> <!-- https://mvnrepository.com/artifact/de.schlichtherle.truelicense/truelicense-swing --> <dependency><groupId>de.schlichtherle.truelicense</groupId><artifactId>truelicense-swing</artifactId><version>1.33</version> </dependency>

3. 生成證書 (該部分代碼由授權(quán)者獨立保管執(zhí)行)

package com.noryar.license; import de.schlichtherle.license.LicenseManager; import de.schlichtherle.license.LicenseParam; /*** 單例模式下的證書管理器* @author Leon Lee.*/ public class LicenseManagerHolder {private static LicenseManager licenseManager;private LicenseManagerHolder(){}public static synchronized LicenseManager getLicenseManager(LicenseParam param){if(licenseManager==null){licenseManager=new LicenseManager(param);}return licenseManager;} }
package com.noryar.license;import java.io.File; import java.io.IOException; import java.io.InputStream; 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.LicenseManager; import de.schlichtherle.license.LicenseParam;/*** 生成證書* @author Leon Lee.*/ public class LicenseMake {private String licPath;private String issued ;private String notBefore ;private String notAfter ;private String consumerType;private int consumerAmount ;private String info ;/*** 私鑰的別名*/private String priAlias ;/*** 該密碼生成密鑰對的密碼*/private String privateKeyPwd ;/*** 使用keytool生成密鑰對時設(shè)置的密鑰庫的訪問密碼*/private String keyStorePwd ;private String subject ;private String priPath ;// X500Princal是一個證書文件的固有格式,詳見APIprivate final static X500Principal DEFAULTHOLDERANDISSUER = new X500Principal("CN=noryar, OU=noryar, O=noryar, L=china, ST=dalian, C=china");public LicenseMake(){}public LicenseMake(String confPath){initParam(confPath);}/*** 讀取屬性文件* @param propertiesPath 屬性文件路徑*/public void initParam(String confPath) {// 獲取參數(shù)Properties prop = new Properties();InputStream in = getClass().getResourceAsStream(confPath);try{prop.load(in);}catch (IOException e){e.printStackTrace();}//common parampriAlias = prop.getProperty("private.key.alias");privateKeyPwd = prop.getProperty("private.key.pwd");keyStorePwd= prop.getProperty("key.store.pwd");subject = prop.getProperty("subject");priPath = prop.getProperty("priPath");licPath = prop.getProperty("licPath");// license contentissued = 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");}/*** 初始化證書的相關(guān)參數(shù)* @return*/private LicenseParam initLicenseParams(){Class<LicenseMake> clazz=LicenseMake.class;Preferences pre = Preferences.userNodeForPackage(clazz);// 設(shè)置對證書內(nèi)容加密的對稱密碼CipherParam cipherParam = new DefaultCipherParam(keyStorePwd);// 參數(shù)1,2從哪個Class.getResource()獲得密鑰庫;//參數(shù)3密鑰庫的別名;//參數(shù)4密鑰庫存儲密碼;//參數(shù)5密鑰庫密碼KeyStoreParam privateStoreParam = new DefaultKeyStoreParam(clazz, priPath, priAlias, keyStorePwd, privateKeyPwd);// 返回生成證書時需要的參數(shù)LicenseParam licenseParam = new DefaultLicenseParam(subject,pre, privateStoreParam, cipherParam);return licenseParam;}/*** 通過外部配置文件構(gòu)建證書的的相關(guān)信息* @return* @throws ParseException*/public LicenseContent buildLicenseContent() throws ParseException{LicenseContent content=new LicenseContent();SimpleDateFormat formate=new SimpleDateFormat("yyyy-MM-dd");content.setConsumerAmount(consumerAmount);content.setConsumerType(consumerType);content.setHolder(DEFAULTHOLDERANDISSUER);content.setIssuer(DEFAULTHOLDERANDISSUER);content.setIssued(formate.parse(issued));content.setNotBefore(formate.parse(notBefore));content.setNotAfter(formate.parse(notAfter));content.setInfo(info);content.setExtra(new Object());return content;}/*** 生成證書,在證書發(fā)布者端執(zhí)行* @throws Exception */public void create() throws Exception{LicenseManager licenseManager=LicenseManagerHolder.getLicenseManager(initLicenseParams());LicenseContent content=buildLicenseContent();licenseManager.store(content, new File(licPath));System.out.println("證書發(fā)布成功");} }
licenseMakeConf.properties

##########common parameters########### #私鑰的別名 private.key.alias=privatekey #privateKeyPwd(該密碼生成密鑰對的密碼,需要妥善保管,不能讓使用者知道) private.key.pwd=noryar456 #keyStorePwd(該密碼是在使用keytool生成密鑰對時設(shè)置的密鑰庫的訪問密碼) key.store.pwd=noryar123 #項目的唯一識別碼 subject=noryar #生成證書的地址 licPath=license.lic #密鑰庫的地址 priPath=privateKeys.store ##########license content########### #發(fā)布日期 issuedTime=2016-07-25 #有效開始日期 notBefore=2016-07-25 #有效截止日期 notAfter=2016-08-30 #consumerType consumerType=user #ConsumerAmount consumerAmount=1 #info info=this is a license
測試方法

public static void main(String[] args) throws Exception{LicenseMake clicense=new LicenseMake("/licenseMakeConf.properties");clicense.create();} 以上就可以創(chuàng)建出一個license文件了。然后開發(fā)者將license文件傳遞給購買方。購買方在系統(tǒng)中上傳該文件進行驗證即可。


4. 驗證證書

package com.noryar.license;import de.schlichtherle.license.LicenseManager; import de.schlichtherle.license.LicenseParam; /*** 單例模式下的證書管理器* @author Leon Lee*/ public class LicenseManagerHolder {private static LicenseManager licenseManager;private LicenseManagerHolder(){}public static synchronized LicenseManager getLicenseManager(LicenseParam param){if(licenseManager==null){licenseManager=new LicenseManager(param);}return licenseManager;} }
認證核心方法

package com.noryar.license;import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.prefs.Preferences;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.LicenseContentException; import de.schlichtherle.license.LicenseManager; import de.schlichtherle.license.LicenseParam;public class LicenseVertify {/*** 公鑰別名*/private String pubAlias;/*** 該密碼是在使用keytool生成密鑰對時設(shè)置的密鑰庫的訪問密碼 */private String keyStorePwd;/*** 系統(tǒng)的統(tǒng)一識別碼*/private String onlykey;/*** 證書路徑 */private String licName;/*** 公鑰庫路徑*/private String pubPath;private String confPath="/licenseVertifyConf.properties";public LicenseVertify(String onlykey){setConf(confPath,onlykey);}public LicenseVertify(String confPath,String onlykey){setConf(confPath,onlykey);}/*** 通過外部配置文件獲取配置信息* @param confPath 配置文件路徑* @param onlykey 系統(tǒng)的統(tǒng)一識別碼*/public void setConf(String confPath,String onlykey){// 獲取參數(shù)Properties prop = new Properties();InputStream in = getClass().getResourceAsStream(confPath);try{prop.load(in);}catch (IOException e){e.printStackTrace();}this.onlykey=onlykey;pubAlias = prop.getProperty("public.alias");keyStorePwd = prop.getProperty("key.store.pwd");licName = prop.getProperty("license.name");pubPath = prop.getProperty("public.store.path");}/*** 初始化證書的相關(guān)參數(shù)* @param 系統(tǒng)的統(tǒng)一識別碼* @return*/private LicenseParam initLicenseParams(){Class<LicenseVertify> clazz=LicenseVertify.class;Preferences pre=Preferences.userNodeForPackage(clazz);CipherParam cipherParam=new DefaultCipherParam(storePwd);KeyStoreParam pubStoreParam=new DefaultKeyStoreParam(clazz, pubPath, pubAlias, keyStorePwd, null);LicenseParam licenseParam=new DefaultLicenseParam(onlykey, pre, pubStoreParam, cipherParam);return licenseParam;}private LicenseManager getLicenseManager(){return LicenseManagerHolder.getLicenseManager(initLicenseParams());}/*** 安裝證書證書* @param 存放證書的路徑* @return*/public void install(String licdir){try{LicenseManager licenseManager=getLicenseManager();licenseManager.install(new File(licdir+File.separator+licName));System.out.println("安裝證書成功!");}catch (Exception e){System.out.println("安裝證書失敗!");e.printStackTrace();System.exit(0);}}/*** 驗證證書的合法性* @return 0、合法,1、證書過期,2、證書錯誤*/public int vertify(){try{LicenseManager licenseManager=getLicenseManager();licenseManager.verify();System.out.println("驗證證書成功!");return 0;}catch(LicenseContentException ex){System.out.println("證書已經(jīng)過期!");ex.printStackTrace();return 1;}catch (Exception e){System.out.println("驗證證書失敗!");e.printStackTrace();return 2;}} }
測試

public static void main(String[] args) throws Exception{LicenseVertify vlicense=new LicenseVertify("noryar"); // 項目唯一識別碼,對應(yīng)生成配置文件的subjectvlicense.install(System.getProperty("user.dir"));vlicense.vertify();} licenseVertifyConf.properties

##########common parameters########### #公鑰別名 public.alias=publiccert #使用keytool生成密鑰對時設(shè)置的密鑰庫的訪問密碼 key.store.pwd= noryar123 #證書路徑 license.name=license.lic #公共庫路徑 public.store.path=publicCerts.store

總結(jié)

以上就是license開發(fā)的常規(guī)手段。對于web項目來說,只需要提供license認證模塊的開發(fā)即可。對于開發(fā)者來說,可以將license生成單獨開發(fā)一個小程序。以后就可以通過這個程序來給不同的企業(yè)生成license。

此外,為了更好的保護代碼。license認證模塊可以進行混淆加密,解密部分使用C語言來編寫,同時針對不同的平臺進行編譯。增強破解難度。

總結(jié)

以上是生活随笔為你收集整理的javaEE防盗版-License开发的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。