使用TrueLicense来保护我们的JAVA软件产品
我们的JAVA软件产品有时需要限制非法用户,只有购买了LICENSE的用户才能使用,因此,我们可以通过TrueLicense来实现。
首先要用KeyTool工具来生成私匙库:
keytool -genkey -alias privatekey -keystore privateKeys.store
然后把私匙库内的公匙导出到一个文件当中:
keytool -export -alias privatekey -file certfile.cer -keystore privateKeys.store
然后再把这个证书文件导入到公匙库:
keytool -import -alias publiccert -file certfile.cer -keystore publicCerts.store
服务器端利用私匙库来创建license的代码例子:
客户端程序安装license以及检验license合法性的代码:
安全警告
不要把私匙库拷贝到客户端,而只拷贝公匙库,要不然黑客就可以用你的私匙库来生成许许多多的license了package com.sourceware.cmvp.license;import de.schlichtherle.license.*;import java.io.*;import java.util.*;import javax.security.auth.x500.X500Principal;import java.util.prefs.Preferences;/** * <p>Title: CMVP通用媒体增值业务平台</p> * * <p>Description: CMVP通用媒体增值业务平台</p> * * <p>Copyright: Copyright (c) 2005</p> * * <p>Company: source-ware.com inc.</p> * * @author 黑山 * @version 2.0 */public class CMVPLicenseManager { public CMVPLicenseManager() { } /** The product id of your software */ public static final String PRODUCT_ID = "cmvp20"; // CUSTOMIZE /** * The subject for the license manager and also the alias of the private * key entry in the keystore. */ public static final String SUBJECT = "别名"; // CUSTOMIZE /** The resource name of your private keystore file. */ public static final String KEYSTORE_RESOURCE = "公匙库文件名"; // CUSTOMIZE /** The password for the keystore. */ public static final String KEYSTORE_STORE_PWD = "公匙库密码"; // CUSTOMIZE /** The password to encrypt the generated license key file. */ public static final String CIPHER_KEY_PWD = "license文件密码"; // CUSTOMIZE protected static final LicenseManager manager = new LicenseManager( new DefaultLicenseParam( SUBJECT, Preferences.userNodeForPackage(CMVPLicenseManager.class), new DefaultKeyStoreParam( CMVPLicenseManager.class, // CUSTOMIZE KEYSTORE_RESOURCE, SUBJECT, KEYSTORE_STORE_PWD, null),//这里一定要是null new DefaultCipherParam(CIPHER_KEY_PWD))); /** * NOTE: This main() method is never called by the actual key server. It is * just useful for debugging the key generator. */ public static final void main(String args[]) { try { manager.install(new java.io.File("license.lic")); String subject = manager.verify().getSubject(); System.out.println("subject========"+subject); } catch (Exception ex) { ex.printStackTrace(); } }}