#include "baobo.hpp"
using namespace std;
int main(int argc,char** argv) {
if (argc < 3) { return baobo::showhelp(); }
string commd = argv[1];
if (commd == "c" || commd == "create") { return baobo::createpassfile(argv, argc, 2); }
else if (commd == "b" || commd == "baopo") { return baobo::baoporarfile(argv, argc, 2); }
return baobo::showhelp();
}
————————————————————————————————————————————
/*baobo.hpp*/
#pragma once
#include "util.hpp"
#include "MyConsole.hpp"
#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>
#include <filesystem>
#include <assert.h>
using namespace std;
namespace baobo {
class passdic {
const string dic = "`1234567890-=~!@#$%^&*()_+qwertyuiop[]\\asdfghjkl;'zxcvbnm,./QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?";
public:
bool newpassfile(string filepath, int minlen, int maxlen) {
if (!(minlen <= maxlen)) { return false; }
if (ofs.is_open()) { ofs.close(); }
ofs.open(filepath);
if (!ofs.is_open()) { return false; }
for (int i = minlen; i <= maxlen; i++) {
cbuff.clear();
writeoutfile(i);
}
ofs.close();
return true;
}
bool usingpassfile(string filepath) {
if (ofs.is_open()) { ofs.close(); }
if (ifs.is_open()) { ifs.close(); }
using namespace filesystem;
path p(filepath);
if (!exists(p)) {return false;}
logpath = p.parent_path();
logpath.append(p.filename().string().append(".log"));
if (!exists(logpath)) {
ofs.open(logpath);
ofs.write("0\n", 2);
ofs.close();
}
ifs.open(logpath);
string line;
getline(ifs, line);
ifs.close();
prevline = line.size() <= 0 ? 0 : atoi(line.c_str());
ifs.open(p);
if (!ifsshiftline(prevline)) { closepassfile(); return false; }
return true;
}
bool next(string &string) {
char buff[2048];
if (!ifs.getline(buff, 2048)) { return false; }
string.clear();
string.append(buff);
prevline += 1;
return true;
}
bool closepassfile() {
if (!ifs.is_open()) { return false; }
ofs.open(logpath);
assert(ofs.is_open());
auto&& line = to_string(prevline);
line.append("\n");
ofs.write(line.c_str(), line.size());
ofs.close();
ifs.close();
return true;
}
private:
ifstream ifs;
int prevline;
filesystem::path logpath;
private:
ofstream ofs;
vector<int> cbuff;
void writeoutfile(int len) {
if (len <= 0) {
string result;
for (auto& v : cbuff) {
result.push_back(dic[v]);
}
result.push_back('\n');
ofs.write(result.c_str(), result.size());
cbuff.pop_back();
return;
}
for (int i = 0; i < dic.size(); i++) {
cbuff.push_back(i);
writeoutfile(len - 1);
}
if (cbuff.size() > 0) { cbuff.pop_back(); }
}
private:
bool ifsshiftline(int shiftline) {
int i = 0;
char buff[1024];
while (i < shiftline && ifs.getline(buff, 1024))i++;
if (i < shiftline) { return false; }
return true;
}
};
void writeOneLinetoFile(string filepath, string line) {
ofstream ofs;
ofs.open(filepath);
assert(ofs.is_open());
line.append("\n");
ofs << line << endl;
ofs.close();
}
int showhelp() {
cout << " arg erro!" << endl;
cout << " c | create 'filepath' 'minlen' 'maxlen'" << endl;
cout << " b | baopo 'rarfilepath' 'passfilepath'" << endl;
return 1;
}
int createpassfile(char** argv, int argc, int beg) {
if (argc - beg + 1 < 3) { return showhelp(); }
string filepath = argv[beg];
string minlen = argv[beg + 1];
string maxlen = argv[beg + 2];
int ni = atoi(minlen.c_str());
int mx = atoi(maxlen.c_str());
baobo::passdic dic;
auto r = dic.newpassfile(filepath, ni, mx);
return r ? 0 : 1;
}
int baoporarfile(char** argv, int argc, int beg) {
if (argc - beg + 1 < 3) { return showhelp(); }
using namespace filesystem;
string str = argv[beg];
path rar(str);
if (!exists(rar)) {
cout << "rar file '" << str << "' not exists!!!" << endl;
return 1;
}
str.clear();
str.append(argv[beg + 1]);
path pass(str);
if (!exists(pass)) {
cout << "pass file '" << str << "' not exists!!!" << endl;
return 1;
}
string rarcmd = "rar t -p ";
baobo::passdic dic;
if (!dic.usingpassfile(pass.string())) {
cout << "pass file '" << pass.string() << "' can not use!!" << endl;
return 2;
}
util::MyConsole console("input");
string p;
while (dic.next(p)) {
string cmd;
cmd.append(rarcmd).append(rar.string());
cout << "using pass: " << p << endl;
console.writeline(p);
console.writeline(p);
int result = system(cmd.c_str());
if (result == 0) {
dic.closepassfile();
cout << "baopo success!! :)" << endl;
cout << " password is " << p << endl;
string log;
log.append(rar.string()).append(".password");
baobo::writeOneLinetoFile(log, p);
cout << " password had output to file :'" << log << "' !" << endl;
return 0;
}
}
cout << "baopo failse :(" << endl;
return 3;
}
}
__________________________________________________________________________________________________________________________
/* util.hpp */
#pragma once
#include <Windows.h>
#include <iostream>
#include<string>
using namespace std;
namespace util {
wstring str2wstr(const string& str) {
int num = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
wchar_t* wide = new wchar_t[num];
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, wide, num);
std::wstring w_str(wide);
delete[] wide;
return w_str;
}
}
__________________________________________________________________________________________________________
/*MyConsole.hpp*/
#pragma once
#include "util.hpp"
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
namespace util {
class MyConsole {
DWORD stdWhat;
HANDLE hand;
public:
MyConsole() :stdWhat(0), hand(INVALID_HANDLE_VALUE) {}
explicit MyConsole(string str) {
setType(str);
}
bool setType(string str) {
if (str == "input") {
stdWhat = STD_INPUT_HANDLE;
} else if (str == "output") {
stdWhat = STD_OUTPUT_HANDLE;
} else {
stdWhat = STD_ERROR_HANDLE;
}
return init();
}
void write(string str) {
DWORD n;
auto wstr = str2wstr(str);
for (auto& c : str) {
auto&& vec = createKeyEventRecord(c);
for (auto in : vec) {
WriteConsoleInput(hand, &in, 1, &n);
}
}
}
void writeline(string str) {
DWORD n;
str.append("\r");
write(str);
/*auto&& r = createReturnKey();
for (auto c : r) {
WriteConsoleInput(hand, &c, 1, &n);
}*/
}
private:
bool init() {
hand = GetStdHandle(stdWhat);
if (INVALID_HANDLE_VALUE == hand) { return false; }
}
vector<INPUT_RECORD> createKeyEventRecord(wchar_t c) {
vector<INPUT_RECORD> result;
INPUT_RECORD in;
in.EventType = KEY_EVENT;
KEY_EVENT_RECORD ker;
ker.bKeyDown = true;
ker.dwControlKeyState = 0;
ker.uChar.UnicodeChar = c;
ker.wVirtualKeyCode = 0;
ker.wVirtualScanCode = 0;
in.Event.KeyEvent = ker;
result.push_back(in);
ker.bKeyDown = false;
in.Event.KeyEvent = ker;
result.push_back(in);
return result;
}
vector<INPUT_RECORD> createReturnKey() {
vector<INPUT_RECORD> result;
INPUT_RECORD in;
in.EventType = KEY_EVENT;
KEY_EVENT_RECORD ker;
ker.bKeyDown = true;
ker.dwControlKeyState = 0;
ker.uChar.UnicodeChar = '\r';
ker.wVirtualKeyCode = VK_RETURN;
ker.wVirtualScanCode = 0;
in.Event.KeyEvent = ker;
result.push_back(in);
ker.bKeyDown = false;
in.Event.KeyEvent = ker;
result.push_back(in);
return result;
}
};
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/109377.html
標籤:其他技術專區
