This repository was archived by the owner on Jul 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRSACipher.cpp
139 lines (130 loc) · 4.91 KB
/
RSACipher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#define _CRT_SECURE_NO_WARNINGS
#include"RSACipher.h"
#include"Tube.h"
#include"Logger.h"
#include"HTTPToolkit.h"
#include<regex>
#include<vector>
#include<unordered_map>
using namespace std;
//pub MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdP8cq736Eu69XH3KniBozD0dyrafW5D+8KcWhrEpM+NJ5wx2uWRTHTxzgHUMqfTwveBNaWM7ayQbUhatqZYDeJQGrpexp+N46C9nYV3kUmi9SFe8w3JOy6VBFw6s/zP2OGWwGMeZ5AcxIprevnwVHAwdqjYKGm9/s2zIOaOPA7QIDAQAB
static BigInteger convertOpensslNum(string num) {
while (!isalnum(num[0]))num = num.substr(1);
string build; int i = 0; bool isHex = false;
while (num[i] == '0' || num[i] == ':')i++;
for (int i = 0; i < num.size(); i++) {
if (num[i] == ':')isHex = true;
else if (num[i] == ' ' || num[i] == '\n')
continue;
else if (num[i] == '(')break;
else build += num[i];
}
while (build[0] == '0')build = build.substr(1);
if (isHex)return BigInteger(build, 16);
else return BigInteger(build, 10);
}
static unordered_map<string, BigInteger> parseObj(string str) {
unordered_map<string, BigInteger>obj_map;
smatch result; pair<string, BigInteger>obj;
regex_search(str, result, regex("(?:\\n)\\w+"));
string bits_source = result.prefix().str(); int bits_ext;
bits_source = bits_source.substr(bits_source.find("("));
sscanf(bits_source.c_str(), "(%dbit)", &bits_ext);
obj_map["bits"] = bits_ext;
str = result[0].str() + result.suffix().str();
regex_search(str, result, regex("(?:\\n)\\w+"));
obj.first = result[0].str().substr(1);
str = result.suffix();
while (regex_search(str, result, regex("(?:\\n)\\w+"))) {
obj.second = convertOpensslNum(result.prefix().str().substr(1));
obj_map[obj.first] = obj.second;
obj.first = result[0].str().substr(1);
str = result.suffix();
}
obj.second = convertOpensslNum(str);
obj_map[obj.first] = obj.second;
return obj_map;
}
#include<iostream>
//please erase the line breaks before calling this function
PUBKEY parsePublicKey(string pubkey) {
Logger tlog; tlog.debug("requesting PUBKEY parse\n");
RemoteSession a = remote("service.std-frank.club", 7456);
a.sendline(string("pub ") + pubkey);
tlog.debug("request sent...recving...\n");
string ret_str = a.recvall();
if (ret_str.find("unable") != string::npos)return PUBKEY();
PUBKEY ret;
unordered_map<string, BigInteger>obj_map = parseObj(ret_str);
if (obj_map.find("Modulus") == obj_map.end() ||
obj_map.find("bits") == obj_map.end())
return PUBKEY();
ret.bits = obj_map["bits"].toInt();
ret.modulus = obj_map["Modulus"];
ret.publicExponent = obj_map["Exponent"];
return ret;
}
//please erase the line breaks before calling this function
PRIVKEY parsePrivateKey(std::string privkey) {
Logger tlog; tlog.debug("requesting PIRVKEY parse\n");
RemoteSession a = remote("service.std-frank.club", 7456);
a.sendline(string("priv ") + privkey);
tlog.debug("request sent...recving...\n");
string ret_str = a.recvall();
if (ret_str.find("unable") != string::npos)return PRIVKEY();
PRIVKEY ret;
unordered_map<string, BigInteger>obj_map = parseObj(ret_str);
if (obj_map.find("modulus") == obj_map.end() ||
obj_map.find("bits") == obj_map.end())
return PRIVKEY();
ret.bits = obj_map["bits"].toInt();
ret.modulus = obj_map["modulus"];
ret.publicExponent = obj_map["publicExponent"];
ret.privateExponent = obj_map["privateExponent"];
ret.prime1 = obj_map["prime1"];
ret.prime2 = obj_map["prime2"];
ret.exponent1 = obj_map["exponent1"];
ret.exponent2 = obj_map["exponent2"];
ret.coefficent = obj_map["coefficent"];
return ret;
}
pair<BigInteger, BigInteger> factorize(BigInteger a) {
Logger tlog; tlog.debug("requesting n factorization\n");
string pq[2];
string url = "www.factordb.com";
RemoteSession factordb = remote(url, 80);
while (factordb.isClosed())factordb = remote(url, 80);
HTTPRequest *req = new GETRequest("factordb.com", "/index.php?query=" + a.toString());
(*req)["Connection"] = "keep-alive";
factordb.send(req->toString());
string meta = factordb.recv();
//factordb.close();
HTTPResponse res = parseResponse(meta);
meta = res.body;
smatch result; vector<string>num;
while (regex_search(meta, result, regex("index\\.php\\?id\\=([0-9]+)"))) {
num.push_back(result[1].str());
meta = result.suffix().str();
}
for (int i = 1; i <= 2; i++) {
//RemoteSession factordb = remote(url, 80);
//while (factordb.isClosed())factordb = remote(url, 80);
HTTPRequest *req = new GETRequest("factordb.com", "/index.php?id=" + num[i]);
(*req)["Connection"] = "keep-alive";
factordb.send(req->toString());
meta = factordb.recv();
HTTPResponse res = parseResponse(meta);
meta = res.body;
regex_search(meta, result, regex("value=\\\"([0-9]+)\\\""));
pq[i - 1] = result[1].str();
//factordb.close();
}
if (pq[0] == pq[1])return pair<BigInteger, BigInteger>(0, 0);
tlog.debug("success\n");
return std::pair<BigInteger, BigInteger>(pq[0], pq[1]);
}
void RSAencode(FILE * file, PUBKEY key) {
char buffer[2048]; fscanf(file, "%s", buffer);
RemoteSession encoder = remote("service.std-frank.club", 7456);
encoder.sendline(buffer);
}