一、功能流程
生成密钥:gpg --full-gen-key
创建时需要留意的地方:RAS加密、userId
导出公钥:gpg --output public-key --export [userId]
导出私钥:gpg --output public-key --export-secret-key [userId]
<dependency>
<groupId>com.verhas</groupId>
<artifactId>license3j</artifactId>
<version>2.0.0-JVM8</version>
</dependency>
OutputStream out;
try {
out = new FileOutputStream(licenseFile);
out.write(new License().setLicense(new File(originFile))
.loadKey(new File(privateKeyFile), userId)
.encodeLicense(privateProtectedPassword).getBytes("utf-8"));
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (PGPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SignatureException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
License license = new License();
if(license.loadKeyRing(this.getClass().getResource("/").getPath()+ "/license/public-key", null)
.setLicenseEncodedFromFile(this.getClass().getResource("/").getPath()+"/license/my.license")
.isVerified()) {
String auth = license.getFeature("auth");
if("true".equals(auth)) {
String authMac = license.getFeature("auth-mac");
String expireTime = license.getFeature("expire-time");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-dd-mm");
if(new Date().after(sdf.parse(expireTime))) {
resultDto.setMessage("Auth Fail(Expired)");
resultDto.setFlag(false);
return resultDto;
}
if(!authMac.equals(CommonUtil.getServerHardwareAddress())) {
resultDto.setMessage("Auth Fail(Information check error)");
resultDto.setFlag(false);
return resultDto;
}
}
}
public static String getServerHardwareAddress() {
try {
byte[] hardwareAddress = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).getHardwareAddress();
StringBuffer sb = new StringBuffer("");
if(hardwareAddress!=null) {
for(int i=0; i<hardwareAddress.length; i++) {
int temp = hardwareAddress[i]&0xff;
String tempStr = Integer.toHexString(temp);
if(tempStr.length()==1) {
sb.append("0"+tempStr);
}else {
sb.append(tempStr);
}
}
}
return sb.toString().toUpperCase();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}
实践过程中遇到的问题:(org.bouncycastle.asn1.ASN1Integer"'s signer information does not match)
引入的包和另一个封装的Jar包出现冲突,调整版本以及依赖顺序后问题得到解决
参考文章: