Skip to content

Commit

Permalink
加入日志系统
Browse files Browse the repository at this point in the history
  • Loading branch information
F-ca7 committed Apr 10, 2019
1 parent 4d17915 commit 695d73e
Show file tree
Hide file tree
Showing 15 changed files with 329 additions and 134 deletions.
4 changes: 2 additions & 2 deletions WMtest/src/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
### 设置###
## log4j.rootLogger=LEVEL,appenderName1,appenderName2,...
## 级别 trace<debug<info<warn<error<fatal
## 级别 trace< debug< info< warn< error< fatal
# log4j.rootLogger = debug,stdout,D,E,I
log4j.rootLogger = debug,stdout
log4j.rootLogger = debug, stdout

### 输出信息到控制台 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
Expand Down
143 changes: 86 additions & 57 deletions WMtest/src/team/aster/Simulator.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package team.aster;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import team.aster.database.MainDbController;
import team.aster.database.SecretKeyDbController;
import team.aster.model.ColumnDataConstraint;
import team.aster.model.ConstraintType;
import team.aster.model.DatasetWithPK;
import team.aster.model.StoredKey;
import team.aster.processor.*;
Expand All @@ -22,8 +26,9 @@ enum Attack {

public class Simulator {
//日志输出
//private static Logger logger = LoggerFactory.getLogger(Simulator.class);
private static Logger logger = LoggerFactory.getLogger(Simulator.class);

//配置信息
private final static String DB_NAME = MysqlDbConfig.EMBED_DB_NAME;
private final static String EMBED_TABLE_NAME = EmbedDbInfo.EMBED_TABLE_NAME;
private final static String PUBLISHED_TABLE_NAME = PublishDbInfo.PUBLISH_TABLE_NAME;
Expand All @@ -40,77 +45,98 @@ public class Simulator {
private static ArrayList<String> targetList = new ArrayList<>(
Arrays.asList("Tencent", "Alibaba", "Google", "Apple", "Banana", "Cherry", "Pineapple"));

private final static String TARGET_NAME = getRandomTargetName();

public static void main(String[] args){
//初始化数据库
// 初始化数据库
MainDbController dbController = initDatabase();
//初始化水印处理器
// 初始化水印处理器
WatermarkProcessor wmProcessor = initProcessor();

// 初始化水印处理器配置
initEncoderConfig(wmProcessor.getEncoder());

//嵌入水印
// 嵌入水印
embedWatermark(dbController, wmProcessor.getEncoder());
//导出数据集到文件
// 导出数据集到文件
exportEmbeddedDataset(dbController);
//通过csv文件导入数据库
publishTableFromFile(dbController);
//发布数据集
// 通过csv文件导入数据库
//publishTableFromFile(dbController);
// 发布数据集
//publishTable(dbController);
//模拟攻击
// 模拟攻击
//simulateAttack(dbController, Attack.DELETION);
//提取水印
// 提取水印
//String extractedWatermark = extraWatermark(dbController, wmProcessor.getDecoder());
String extractedWatermark = extraWatermark(dbController, wmProcessor.getDecoder(), dbController.getDatasetWithPK());

//对提取水印溯源
String target = identifyOrigin(getDbTableName(), extractedWatermark);
System.out.println("溯源得到的目标为" + target);
logger.info("溯源得到的目标为 {}", target);

}

// 初始化水印处理器配置
private static void initEncoderConfig(IEncoderNumericImpl encoder) {
// 初始化秘钥信息
StoredKey.Builder storedKeyBuilder = generateStoredKey();
encoder.setStoredKeyBuilder(storedKeyBuilder);

// 初始化约束条件, 约束条件由客户自定义
ColumnDataConstraint dataConstraint = new ColumnDataConstraint(ConstraintType.DOUBLE, -300, 300, -2);
encoder.setDataConstraint(dataConstraint);
}

// 模拟生成秘钥信息
private static StoredKey.Builder generateStoredKey() {
StoredKey.Builder storedKeyBuilder = new StoredKey.Builder()
.setDbTable(getDbTableName()).setMinLength(StoredKeyDbInfo.PARTITION_MIN_LENGTH)
.setSecretKey(StoredKeyDbInfo.SECRET_KEY)
.setTarget(TARGET_NAME).setPartitionCount(EmbedDbInfo.PARTITION_COUNT);
return storedKeyBuilder;
}


//从文件导入到数据库
// 从文件导入到数据库
private static void publishTableFromFile(MainDbController dbController) {
startTime = System.currentTimeMillis();
boolean publishSuccess = dbController.publiahDatasetFromFile(getAbsoluteProjPath() +"\\"+ getExportCSVFileName());
boolean publishSuccess = dbController.publishDatasetFromFile(getAbsoluteProjPath() +"\\"+ getExportCSVFileName());
if (publishSuccess){
endTime = System.currentTimeMillis();
System.out.printf("从csv文件导入%d条数据集到数据库耗时: %d ms%n", dbController.getFetchCount(), (endTime - startTime));
logger.info("从csv文件导入{}条数据集到数据库耗时: {} ms", dbController.getFetchCount(), (endTime - startTime));
} else {
System.out.println("从csv文件导入到数据库失败");
logger.error("从csv文件导入到数据库失败");
}

}

//获取项目绝对路径
// 获取项目绝对路径
private static String getAbsoluteProjPath() {
File directory = new File("");// 参数为空
String projPath = directory.getAbsolutePath();
//System.out.println(projPath);
return projPath;
}

//导出已嵌入水印的数据集
// 导出已嵌入水印的数据集
private static void exportEmbeddedDataset(MainDbController dbController) {
startTime = System.currentTimeMillis();
File file = new File(getExportCSVFileName());
try {
file.createNewFile();
boolean flag = dbController.getDatasetWithPK().exportToCSV(file);
if (flag){
System.out.println("导出到csv文件成功");
endTime = System.currentTimeMillis();
logger.info("导出到csv文件成功");
logger.info("导出{}条数据集到csv文件耗时: {} ms", dbController.getFetchCount(), (endTime - startTime));
} else {
System.out.println("导出到csv文件失败");
logger.error("导出到csv文件失败");
}
} catch (IOException e) {
logger.error("导出到csv文件失败");
e.printStackTrace();
}
endTime = System.currentTimeMillis();

System.out.printf("导出%d条数据集到csv文件耗时: %d ms%n", dbController.getFetchCount(), (endTime - startTime));
}

private static String getExportCSVFileName() {
return EMBED_TABLE_NAME+"_publish.csv";
}


Expand All @@ -132,13 +158,13 @@ private static String extraWatermark(MainDbController dbController, IDecoder dec
OptimDecoder optimDecoder = (OptimDecoder) decoder;
optimDecoder.setStoredKeyParams(storedKey);
return optimDecoder.decode(datasetWithPK);

}


private static WatermarkProcessor initProcessor() {
WatermarkFactory factory = new WatermarkFactory();
WatermarkProcessor wmProcessor = factory.getWatermarkProcessor(WATERMARK_PROCESSOR_TYPE);
System.out.printf("初始化%s完成%n", wmProcessor.toString());
logger.info("初始化{}完成", wmProcessor.toString());
return wmProcessor;
}

Expand All @@ -148,40 +174,42 @@ private static void publishTable(MainDbController dbController) {
startTime = System.currentTimeMillis();
dbController.publishDataset();
endTime = System.currentTimeMillis();
System.out.println("数据表发布成功!");
System.out.printf("发布%d条数据所用耗时: %d ms%n", dbController.getFetchCount(),(endTime - startTime) );
logger.info("数据表发布成功!");
logger.info("发布{}条数据所用耗时: {} ms", dbController.getFetchCount(),(endTime - startTime) );
}


private static MainDbController initDatabase(){
System.out.println("开始初始化数据库...");
logger.info("开始初始化数据库连接...");
//初始化数据库
MainDbController dbController = new MainDbController(DB_NAME, EMBED_TABLE_NAME);
logger.info("设置元组数为 {}", FETCH_COUNT);
dbController.setFetchCount(FETCH_COUNT); //设置获取元组数
logger.info("设置发布表名为 {}", PUBLISHED_TABLE_NAME);
dbController.setPublishTableName(PUBLISHED_TABLE_NAME); //设置发布表名
System.out.println("连接数据库成功");
logger.info("连接数据库成功");

System.out.println("开始获取数据集");
logger.info("开始获取数据集");
startTime = System.currentTimeMillis();
dbController.fetchDataset();
endTime = System.currentTimeMillis();

System.out.printf("获取%d条数据集耗时: %d ms%n", dbController.getFetchCount(), (endTime - startTime));
//dbController.printDatasetWithPK();
logger.info("获取{}条数据集耗时: {}ms", dbController.getFetchCount(), (endTime - startTime));
return dbController;
}


private static void embedWatermark(MainDbController dbController, IEncoderImpl encoder){
encoder.setTarget(getRandomTargetName());
encoder.setDbTable(getDbTableName());
System.out.println("开始嵌入水印...");
private static void embedWatermark(MainDbController dbController, IEncoderNumericImpl encoder){

logger.info("开始嵌入水印...");

startTime = System.currentTimeMillis();
//向带有主键的数据嵌入水印
// 向带有主键的数据嵌入水印
encoder.encode(dbController.getDatasetWithPK(), SecretKeyDbController.getInstance().getWatermarkListByDbTable(getDbTableName()));
endTime = System.currentTimeMillis();
System.out.println("嵌入水印完成");
System.out.printf("嵌入水印耗时:%d ms%n", (endTime - startTime));

logger.info("嵌入水印完成");
logger.info("嵌入水印耗时:{} ms", (endTime - startTime));
}

private static String getDbTableName() {
Expand All @@ -206,30 +234,25 @@ private static void performLSBAlternation(MainDbController dbController) {
}

private static void performDeletion(MainDbController dbController, double deletionPercent) {
System.out.println("开始模拟删除攻击...");
logger.info("开始模拟删除攻击...");

startTime = System.currentTimeMillis();
dbController.randomDeletion(deletionPercent);
endTime = System.currentTimeMillis();

System.out.println("删除完毕");
System.out.printf("删除攻击耗时:%d%n", (endTime - startTime));
logger.info("删除完毕");
logger.info("删除攻击耗时: {}", (endTime - startTime));
}


/**
* @Description 从发布数据库中得到发布的数据集
* 并提取水印
* @author Fcat
* @date 2019/4/7 10:41
* @param dbController
* @param decoder
* @return java.lang.String
*/
// 从发布数据库中
// 得到发布的数据集并提取水印
private static String extraWatermark(MainDbController dbController, IDecoder decoder){
System.out.println("开始提取水印...");
logger.info("开始提取水印...");

//获取对应的各个秘钥、秘参
StoredKey storedKey = SecretKeyDbController.getInstance().getStoredKeyByDbTable(getDbTableName());

if (decoder instanceof OptimDecoder){
//设置解码时的参数
OptimDecoder optimDecoder = (OptimDecoder) decoder;
Expand All @@ -243,21 +266,21 @@ private static String extraWatermark(MainDbController dbController, IDecoder dec
return primLSBDecoder.decode(dbController.getPublishedDatasetWithPK());

}
logger.error("提取水印为空!");
return "empty!";
}

private static String identifyOrigin(String dbTable, String watermark){
System.out.println("开始溯源...");
logger.info("开始溯源...");

startTime = System.currentTimeMillis();
String orign = SecretKeyDbController.getInstance().getMostLikelyTarget(dbTable, watermark);
endTime = System.currentTimeMillis();

System.out.printf("溯源耗时%d%n", (endTime-startTime));
logger.info("溯源耗时: {}", (endTime-startTime));
return orign;
}


/*
* 随机返回一个共享目标方的名字
*/
Expand All @@ -267,4 +290,10 @@ private static String getRandomTargetName(){
return targetList.get(index);
}


private static String getExportCSVFileName() {
return EMBED_TABLE_NAME+"_publish.csv";
}


}
2 changes: 1 addition & 1 deletion WMtest/src/team/aster/database/MainDbController.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public DatasetWithPK getPublishedDatasetWithPK(){
}


public boolean publiahDatasetFromFile(String filepath){
public boolean publishDatasetFromFile(String filepath){
boolean isSuccess;
String uuid = UUID.randomUUID().toString().replaceAll("-","").substring(0,10);
String publishName = publishTableName + "_"+uuid;
Expand Down
19 changes: 11 additions & 8 deletions WMtest/src/team/aster/database/SecretKeyDbController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package team.aster.database;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import team.aster.model.StoredKey;
import team.aster.utils.BinaryUtils;

Expand All @@ -22,6 +24,7 @@
* @date 2019/4/2 10:54
*/
public class SecretKeyDbController {
private static Logger logger = LoggerFactory.getLogger(SecretKeyDbController.class);

private final static String DRIVER_NAME = MysqlDbConfig.DRIVER_NAME;
private final static String DB_NAME = MysqlDbConfig.STORED_KEY_DB_NAME;
Expand Down Expand Up @@ -90,8 +93,7 @@ public ArrayList<String> getWatermarkListByDbTable(String dbTable){
}
pstmt.close();
rs.close();
System.out.println("已存在水印集为");
System.out.println(wmList);
logger.info("已存在水印集为 {}", wmList);
return wmList;
} catch (SQLException e) {
System.out.println("SQL错误");
Expand Down Expand Up @@ -124,12 +126,12 @@ public String getMostLikelyTarget(String dbTable, String watermark){
}
pstmt.close();
rs.close();
System.out.println("已存在水印集为");
System.out.println(wmListMap);
logger.info("已存在水印集为 {}", wmListMap);
} catch (SQLException e) {
System.out.println("SQL错误");
logger.error("SQL错误 {}", e.getSQLState());
e.printStackTrace();
} catch (Exception e){
logger.error(e.getMessage());
e.printStackTrace();
}
AtomicReference<Double> similarity = new AtomicReference<>(0.0);
Expand All @@ -141,7 +143,7 @@ public String getMostLikelyTarget(String dbTable, String watermark){
result.set(v);
}
});
System.out.println("相似度为"+similarity+", 结果为"+result);
logger.info("相似度为 {}, 结果为 {}", similarity, result);
return result.get();
}

Expand All @@ -164,12 +166,13 @@ public void saveStoredKeysToDB(StoredKey storedKey){
//System.out.println(pstmt);
pstmt.executeUpdate();

System.out.println("保存水印参数数据成功!");
logger.info("保存水印参数数据成功!");
pstmt.close();
} catch (SQLException e) {
System.out.println("SQL错误");
logger.error("SQL错误 {}", e.getSQLState());
e.printStackTrace();
} catch (Exception e){
logger.error(e.getMessage());
e.printStackTrace();
}
}
Expand Down
Loading

0 comments on commit 695d73e

Please sign in to comment.