package com.wisedu.zzfw.crypto;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import com.wisedu.zzfw.crypto.exception.LicenseInvalidException;
/**
* 这个类,在打包的时候,不发布到现场。
* @author stzhang
*/
public class MyRSAEnCoderTools {
public final static String licenseFilePath = "D:\\license";
public final static String licenseSourceFileName = "ZZFWServer.txt";
public final static String licenseFileName = "ZZFWServer.lic";
public final static String PARAMETERMACHINDECODE = "DKDH8273DJJH839DKKSAYUWCBNXM283";
public final static String[] defaultFileContent = {
"LICENSEID=BAIDU-FOR-XZXY",
"LICENSENAME=xxx系统授权给XX大学",
"LICENSETYPE=1",
"EXPIREDAY=2017-09-30", //日期采用yyyy-MM-dd日期格式
"PRINTCLIENTCOUNT=100",
"CHECKMACHINECODE=NO", //正式发布到现场的证书中, 不包含这一行. 主要为了适应开发环境的调试和运行.
"MACHINECODE=DKDH8273DJJH839DKK,SAYUWCBNXM283"
};
// public static void main(String[] args) throws Exception {
// String filePath = licenseFilePath + File.separator + licenseFileName;
// genLicenseFile(licenseFilePath, licenseSourceFileName, licenseFileName, defaultFileContent);
// boolean f = checkLicenseFile(filePath, PARAMETERMACHINDECODE, new Date() );
// System.out.println("证书check结果:" + f);
// }
public static void initLicensePath() {
System.out.println("-----------开始初始化路径-----------");
try {
initLicensePath(licenseFilePath,licenseSourceFileName, defaultFileContent);
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("-----------初始化路径完成-----------");
}
public static void genLicenseFile() {
System.out.println("-----------开始生成证书-----------");
String filePath = licenseFilePath + File.separator + licenseFileName;
try {
genLicenseFile(licenseFilePath, licenseSourceFileName, licenseFileName, defaultFileContent);
boolean f = checkLicenseFile(filePath, PARAMETERMACHINDECODE, new Date() );
System.out.println("证书check结果:" + f);
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("-----------生成证书完成-----------");
}
/**
* 初始化目录
* @param licensePath
* @param sourceFileName
* @param defaultLicenseContent
* @throws Exception
*/
private static void initLicensePath(String licensePath, String sourceFileName, String[] defaultLicenseContent)throws Exception {
File file = new File(licensePath + File.separator + sourceFileName);
File filePath = new File(licensePath);
if(filePath.exists() ){
System.out.println("路径已经存在:" + filePath.getAbsolutePath());
}else{
filePath.mkdirs();
}
if(file.exists()){
System.out.println("License源文件已经存在:" + file.getAbsolutePath());
}else{
List<String> lines = new ArrayList<String>();
for(int j = 0, len = defaultLicenseContent.length; j< len; j++){
if(StringUtils.isNotEmpty(defaultLicenseContent[j])){
lines.add(defaultLicenseContent[j]);
}
}
FileUtils.writeLines(file, "UTF-8", lines);
System.out.println("初始化路径完成:" + file.getAbsolutePath());
}
}
/**
* 生成证书License文件
* @param licensePath: 证书文件和原始文件的路径
* @param sourceFileName: 原始文件的文件名
* @param licenseFileName: 证书文件的文件名
* @param defaultLicenseContent: 默认产生的内容
* @throws Exception
*/
private static void genLicenseFile(String licensePath, String sourceFileName, String licenseFileName, String[] defaultLicenseContent) throws Exception {
// 加载证书
PrivateKey privateKey = RSAEnCoder.getPrivateKey(CryptoPrivateKeys.KEYMODULUS, CryptoPrivateKeys.PRIVATEKEYEXPONENT);
File file = new File(licensePath + File.separator + sourceFileName);
File licenseFile = new File(licensePath + File.separator + licenseFileName);
File filePath = new File(licensePath);
if(!filePath.exists() ){
filePath.mkdirs();
}
if(!file.exists()){
initLicensePath(licensePath,sourceFileName, defaultLicenseContent);
}
if(!file.exists()){
throw new Exception("没有获取License加密的源文件");
}
if(licenseFile.exists()){
licenseFile.delete();
}
FileInputStream fis = new FileInputStream(file);
if(fis.available() == 0){
throw new FileNotFoundException("License加密的源文件为空");
}
fis.close();
byte[] sByte = FileUtils.readFileToByteArray(file);
if(sByte.length == 0){
throw new FileNotFoundException("License加密的源文件为空");
}
byte[] encoderData = RSAEnCoder.encryptRSA(sByte, privateKey);
String sign = RSAEnCoder.sign(encoderData, privateKey);
//把签名也写进证书, 进行License的自我校验。
byte[] signBytes = ("\n"+sign).getBytes();
byte[] raw = new byte[encoderData.length + signBytes.length];
//合并两个byte[]
System.arraycopy(encoderData,0,raw,0,encoderData.length);
System.arraycopy(signBytes,0,raw,encoderData.length,signBytes.length);
// 双重加密
byte[] encoderaw = RSAEnCoder.encryptRSA(raw, privateKey);
FileUtils.writeByteArrayToFile(licenseFile, encoderaw);
System.out.println("License生成完成,路径:" + licenseFile.getAbsolutePath());
}
private static boolean checkLicenseFile(String licenseFilePath, String machCode, Date currentDate) throws Exception {
// 加载证书
PublicKey publicKey = RSADeCoder.getPublicKey(CryptoPublicKeys.KEYMODULUS, CryptoPublicKeys.PUBLICKEYEXPONENT);
File licenseFile = new File(licenseFilePath);
if(!licenseFile.exists()){
throw new Exception("没有获取License文件");
}
FileInputStream fis = FileUtils.openInputStream(licenseFile);
if(fis.available() == 0){
throw new FileNotFoundException("License加密文件为空");
}
fis.close();
//首先读入License文件
byte[] encoderData = FileUtils.readFileToByteArray(licenseFile);
byte[] sbytes = RSADeCoder.decryptRSA(encoderData, publicKey);
byte[] firstEncoderData = null;
byte[] secondSignData = null;
int index =0 ;
for(int len = sbytes.length; index< len ; index ++ ){
if(sbytes[index] == '\n'){
firstEncoderData = new byte[index];
secondSignData = new byte[len - index - 1];
//合并两个byte[]
System.arraycopy(sbytes, 0, firstEncoderData,0, firstEncoderData.length);
System.arraycopy(sbytes,index + 1, secondSignData, 0 ,secondSignData.length);
//如果已经是base64编码, 则直接返回
if(isArrayByteBase64(secondSignData)){
break;
}
}
}
String sign = new String(secondSignData);
//System.out.println("sbytes:"+new String(sbytes) );
//System.out.println("sign:"+sign);
boolean verify = RSADeCoder.verify(firstEncoderData, sign, publicKey);
if(!verify){
throw new LicenseInvalidException("License签名校验无效,请确认这证书的有效性.");
}
//再次解密, 得到最终的授权内容
byte[] textBytes = RSADeCoder.decryptRSA(firstEncoderData, publicKey);
System.out.println("授权文件详情:\n"+new String(textBytes) );
HashMap<String, String> prop = genDataFromArr
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
License生成器(源码+客户端).zip (35个子文件)
License生成器(源码+客户端)
SRC
ZzfwLicense
.project 387B
src
com
wisedu
zzfw
crypto
CryptoPrivateKeys.java 869B
RSAEnCoder.java 5KB
exception
LicenseInvalidException.java 228B
swing
MainCrytoFrame.java 3KB
LoopedStreams.java 3KB
ConsoleTextArea.java 2KB
MyRSAEnCoderTools.java 10KB
RSADeCoder.java 2KB
CryptoPublicKeys.java 558B
lib
org.apache.commons.codec_1.3.0.v201101211617.jar 54KB
bcprov-jdk15-136.jar 1.35MB
commons-io-1.3.2.jar 86KB
commons-lang-2.3.jar 240KB
.settings
org.eclipse.jdt.core.prefs 598B
org.eclipse.core.resources.prefs 57B
.classpath 581B
bin
com
wisedu
zzfw
crypto
RSADeCoder.class 3KB
RSAEnCoder.class 5KB
MyRSAEnCoderTools.class 10KB
CryptoPrivateKeys.class 1KB
exception
LicenseInvalidException.class 504B
swing
MainCrytoFrame$3.class 1KB
LoopedStreams.class 2KB
LoopedStreams$3.class 2KB
MainCrytoFrame$1.class 959B
MainCrytoFrame.class 3KB
ConsoleTextArea$1.class 2KB
LoopedStreams$1.class 1KB
LoopedStreams$2.class 903B
MainCrytoFrame$2.class 961B
ConsoleTextArea.class 2KB
CryptoPublicKeys.class 747B
RUN
licenseRun.bat 214B
ZZFWLicense1.jar 1.53MB
共 35 条
- 1
walkingmanc
- 粉丝: 628
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论3