215 lines
5.3 KiB
C++
215 lines
5.3 KiB
C++
#include <iostream>
|
|||
|
|
#include <fstream>
|
||
|
|
#include <sstream>
|
||
|
|
#include <set>
|
||
|
|
#include <map>
|
||
|
|
#include <vector>
|
||
|
|
#include <string>
|
||
|
|
#include <direct.h>
|
||
|
|
#include <stdio.h>
|
||
|
|
#include "BitanswerCpp.h"
|
||
|
|
#include "boost\random.hpp"
|
||
|
|
#include "boost\random\random_device.hpp"
|
||
|
|
|
||
|
|
boost::random::mt19937 g_gen;
|
||
|
|
|
||
|
|
/*
|
||
|
|
共5组 验证 Feature 供日常验证
|
||
|
|
|
||
|
|
共2组 时间验证 Feature 供特定日期使用
|
||
|
|
|
||
|
|
为每一组生成 365 个 数据应答对
|
||
|
|
|
||
|
|
*/
|
||
|
|
|
||
|
|
#define VERIFY1 3
|
||
|
|
#define VERIFY2 4
|
||
|
|
#define VERIFY3 5
|
||
|
|
#define VERIFY4 6
|
||
|
|
#define VERIFY5 7
|
||
|
|
#define VERIFY6 8
|
||
|
|
#define VERIFY7 9
|
||
|
|
#define VERIFY8 10
|
||
|
|
#define VERIFY9 11
|
||
|
|
#define VERIFY10 12
|
||
|
|
|
||
|
|
struct STEncrypPair
|
||
|
|
{
|
||
|
|
BIT_UINT32 ask1;
|
||
|
|
BIT_UINT32 ask2;
|
||
|
|
BIT_UINT32 ask3;
|
||
|
|
BIT_UINT32 ask4;
|
||
|
|
BIT_UINT32 result;
|
||
|
|
STEncrypPair(): ask1(0), ask2(0), ask3(0), ask4(0), result(0) {}
|
||
|
|
STEncrypPair(const BIT_UINT32 &a1
|
||
|
|
, const BIT_UINT32 &a2
|
||
|
|
, const BIT_UINT32 &a3
|
||
|
|
, const BIT_UINT32 &a4)
|
||
|
|
: ask1(a1)
|
||
|
|
, ask2(a2)
|
||
|
|
, ask3(a3)
|
||
|
|
, ask4(a4)
|
||
|
|
, result(0) {}
|
||
|
|
BIT_STATUS calc(CBitAnswer &ba, const BIT_UINT32 &feature)
|
||
|
|
{
|
||
|
|
return ba.ConvertFeature(feature, ask1, ask2, ask3, ask4, &result);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
int main()
|
||
|
|
{
|
||
|
|
BIT_STATUS bs(BIT_SUCCESS);
|
||
|
|
CBitAnswer bitanswer;
|
||
|
|
|
||
|
|
bs = bitanswer.Login(NULL, BIT_MODE_AUTO);
|
||
|
|
|
||
|
|
std::vector<int> features;
|
||
|
|
features.push_back(VERIFY1);
|
||
|
|
features.push_back(VERIFY2);
|
||
|
|
features.push_back(VERIFY3);
|
||
|
|
features.push_back(VERIFY4);
|
||
|
|
features.push_back(VERIFY5);
|
||
|
|
features.push_back(VERIFY6);
|
||
|
|
features.push_back(VERIFY7);
|
||
|
|
features.push_back(VERIFY8);
|
||
|
|
features.push_back(VERIFY9);
|
||
|
|
features.push_back(VERIFY10);
|
||
|
|
|
||
|
|
// 取 7 * 365 * 4 个随机数,不可以重复
|
||
|
|
int numOfFeature(10); // 需验证的特征数
|
||
|
|
int numOfYear(365); // 一年中的天数
|
||
|
|
int numOfFactor(4); // 一个询问加密过程需要的因子数量
|
||
|
|
int len(numOfYear * numOfFeature * numOfFactor);
|
||
|
|
|
||
|
|
std::set<BIT_UINT32> ss;
|
||
|
|
|
||
|
|
// 得到 len 个 加密因子
|
||
|
|
while (ss.size() < len)
|
||
|
|
{
|
||
|
|
boost::uniform_int<unsigned long> rnd(1, ULONG_MAX);
|
||
|
|
ss.insert(rnd(g_gen));
|
||
|
|
}
|
||
|
|
|
||
|
|
if (ss.size() != len)
|
||
|
|
{
|
||
|
|
system("pause");
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 将加密因子分成 numOfFeature 份 每份 numOfYear 个
|
||
|
|
|
||
|
|
std::map<BIT_UINT32, std::vector<STEncrypPair>> vOfFeature;
|
||
|
|
std::vector<BIT_UINT32> vFactorTemp;
|
||
|
|
std::vector<STEncrypPair> vEncrypTemp;
|
||
|
|
int s(0), e(0), n = 365;
|
||
|
|
for (int i = 0; i < numOfFeature; ++i)
|
||
|
|
{
|
||
|
|
|
||
|
|
if (i >= features.size())
|
||
|
|
{
|
||
|
|
std::cout << " numOfFeature wrong!" << endl;
|
||
|
|
system("pause");
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
BIT_UINT32 feature(features[i]);
|
||
|
|
|
||
|
|
s = i * (numOfYear * numOfFactor);
|
||
|
|
e = (i + 1) * (numOfYear * numOfFactor);
|
||
|
|
if (s >= e) continue;
|
||
|
|
if (s >= len || e > len) continue;
|
||
|
|
|
||
|
|
std::set<BIT_UINT32>::iterator sIt(ss.begin());
|
||
|
|
std::set<BIT_UINT32>::iterator eIt(ss.begin());
|
||
|
|
|
||
|
|
std::advance(sIt, s);
|
||
|
|
std::advance(eIt, e);
|
||
|
|
|
||
|
|
vEncrypTemp.clear();
|
||
|
|
vFactorTemp.clear();
|
||
|
|
vFactorTemp.assign(sIt, eIt);
|
||
|
|
|
||
|
|
for (size_t j = 0; j < vFactorTemp.size() - 3; j += 4)
|
||
|
|
{
|
||
|
|
vEncrypTemp.push_back(STEncrypPair(vFactorTemp[j], vFactorTemp[j + 1], vFactorTemp[j + 2], vFactorTemp[j + 3]));
|
||
|
|
bs = vEncrypTemp.rbegin()->calc(bitanswer, feature);
|
||
|
|
if (bs)
|
||
|
|
{
|
||
|
|
std::cout << hex << features[i] << '\t' << vEncrypTemp.size() - 1 << '\t' << (int)bs << endl;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
vOfFeature.insert(std::make_pair(feature, vEncrypTemp));
|
||
|
|
}
|
||
|
|
bitanswer.Logout();
|
||
|
|
|
||
|
|
char buffer[255];
|
||
|
|
::memset(buffer, 0, sizeof(char) * 255);
|
||
|
|
_getcwd(buffer, 255);
|
||
|
|
|
||
|
|
std::map<BIT_UINT32, std::vector<STEncrypPair>>::const_iterator it(vOfFeature.begin());
|
||
|
|
std::stringstream askPath(""), answerPath("");
|
||
|
|
askPath << buffer << '\\' << "ask.txt";
|
||
|
|
answerPath << buffer << '\\' << "answer.txt";
|
||
|
|
std::ofstream askFile(askPath.str());
|
||
|
|
std::ofstream answerFile(answerPath.str());
|
||
|
|
for (; it != vOfFeature.end(); ++it)
|
||
|
|
{
|
||
|
|
answerPath.str("");
|
||
|
|
askPath.str("");
|
||
|
|
|
||
|
|
askFile << "class Ask" << dec << it->first << ": public Ask \n"
|
||
|
|
<< "{\n"
|
||
|
|
<< "public:\n"
|
||
|
|
<< "\tAsk" << it->first << "() {}"
|
||
|
|
<< "\t~Ask" << it->first << "() {}"
|
||
|
|
<< "\tvirtual bool get(const int &idx, BIT_UINT32 &a1, BIT_UINT32 &a2, BIT_UINT32 &a3, BIT_UINT32 &a4)\n"
|
||
|
|
<< "\t{\n"
|
||
|
|
<< "\t\tif (idx >= 365) return false;\n"
|
||
|
|
<< "\t\tBIT_UINT32 ask" << it->first << "[" << numOfYear << "][" << numOfFactor << "] = {" << endl;
|
||
|
|
|
||
|
|
answerFile << "class Answer" <<dec << it->first << ": public Answer\n"
|
||
|
|
<< "{\n"
|
||
|
|
<< "public:\n"
|
||
|
|
<< "\tAnswer" << it->first << "() {}"
|
||
|
|
<< "\t~Answer" << it->first << "() {}"
|
||
|
|
<< "\tvirtual bool get(const int &idx, BIT_UINT32 &result)\n"
|
||
|
|
<< "\t{\n"
|
||
|
|
<< "\t\tif (idx >= 365) return false;\n"
|
||
|
|
<< "\t\tBIT_UINT32 answer" << it->first << "[" << numOfYear << "] = {" << endl;
|
||
|
|
|
||
|
|
for (size_t i = 0; i < it->second.size(); ++i)
|
||
|
|
{
|
||
|
|
const STEncrypPair &en(it->second.at(i));
|
||
|
|
askFile << hex << "\t\t\t{"
|
||
|
|
<< "0X" << en.ask1 << ", "
|
||
|
|
<< "0X" << en.ask2 << ", "
|
||
|
|
<< "0X" << en.ask3 << ", "
|
||
|
|
<< "0X" << en.ask4
|
||
|
|
<< "}," << endl;
|
||
|
|
answerFile << hex << "\t\t\t0X" << en.result << "," <<endl;
|
||
|
|
}
|
||
|
|
|
||
|
|
askFile << dec << "\t\t};\n"
|
||
|
|
<< "\t\ta1 = ask" << it->first << "[idx][0];\n"
|
||
|
|
<< "\t\ta2 = ask" << it->first << "[idx][1];\n"
|
||
|
|
<< "\t\ta3 = ask" << it->first << "[idx][2];\n"
|
||
|
|
<< "\t\ta4 = ask" << it->first << "[idx][3];\n"
|
||
|
|
<< "\treturn true;\n"
|
||
|
|
<< "\t}\n"
|
||
|
|
<< "};\n\n";
|
||
|
|
|
||
|
|
answerFile << dec << "\t\t};\n"
|
||
|
|
<< "\t\tresult = answer" << it->first << "[idx];\n"
|
||
|
|
<< "\t\treturn true;\n"
|
||
|
|
<< "\t}\n"
|
||
|
|
<< "};\n\n";
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
askFile.close();
|
||
|
|
answerFile.close();
|
||
|
|
|
||
|
|
system("pause");
|
||
|
|
return 1;
|
||
|
|
}
|