/
decrypter.cpp
254 lines (214 loc) · 9.03 KB
/
decrypter.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/****************************************************************************
** Pokemon Gen VI Save Decrypter
** Spiffy
** Special thanks to Monsi Terdex, and his program demonstrating XOR encryption:
** http://monsiterdex.wordpress.com/2013/05/17/simple-file-encryption-in-c-rivest-cipher-4-rc4-byte-inversion-cycling-and-xor/
****************************************************************************/
#include <QtGui>
#include "decrypter.h"
//typedef char BYTE;
//! [0]
Decrypter::Decrypter()
{
keystreamfilename="";
savefilename="";
QDesktopWidget desk;
qreal px_width = desk.width() / 2;
//qreal px_height = desk.height() / 2;
QVBoxLayout *layout = new QVBoxLayout;
QHBoxLayout *versionBox = new QHBoxLayout;
QButtonGroup *versiongroup = new QButtonGroup(this);
xysave = new QRadioButton( "XY" );
orassave = new QRadioButton( "ORAS" );
versiongroup->addButton(xysave);
versiongroup->addButton(orassave);
versionBox->addWidget(xysave);
versionBox->addWidget(orassave);
QHBoxLayout *keyBox = new QHBoxLayout;
QPushButton *keystreamButton=new QPushButton("Load Keystream");
connect(keystreamButton, SIGNAL(clicked()), this, SLOT(on_keystreamButton_clicked()));
keystreamPathDisplay=new QLineEdit;
keystreamPathDisplay->setReadOnly(true);
keystreamPathDisplay->setMinimumWidth(px_width);
keyBox->addWidget(keystreamPathDisplay);
keyBox->addWidget(keystreamButton);
QHBoxLayout *saveBox = new QHBoxLayout;
QPushButton *saveButton=new QPushButton("Load Save");
connect(saveButton, SIGNAL(clicked()), this, SLOT(on_saveButton_clicked()));
savePathDisplay=new QLineEdit;
savePathDisplay->setReadOnly(true);
savePathDisplay->setMinimumWidth(px_width);
saveBox->addWidget(savePathDisplay);
saveBox->addWidget(saveButton);
//disable this until we have both a keystream and save path
decryptButton=new QPushButton("Decrypt Save");
connect(decryptButton, SIGNAL(clicked()), this, SLOT(on_decryptButton_clicked()));
decryptButton->setEnabled(false);
QPushButton *quitButton=new QPushButton("Quit");
connect(quitButton, SIGNAL(clicked()), this, SLOT(on_quitButton_clicked()));
layout->addLayout(versionBox);
layout->addLayout(keyBox);
layout->addLayout(saveBox);
layout->addWidget(decryptButton);
layout->addWidget(quitButton);
this->setLayout(layout);
//! [2]
//! [3] //! [4]
//mainLayout->addWidget(verticalGroupBox);
//! [4] //! [5]
//setLayout(mainLayout);
setWindowTitle(tr("Pokemon Gen VI Save Decrypter"));
}
//! [5]
//! [7]
void Decrypter::on_keystreamButton_clicked()
{
QString temp=openBin();
if (temp.length()){
keystreamfilename=temp.toUtf8().constData();
std::cout << keystreamfilename << std::endl;
keystreamPathDisplay->setText(temp);
if (savefilename!="" && keystreamfilename!=""){
this->decryptButton->setEnabled(true);
//if we got both, ungray
}
}
}
void Decrypter::on_saveButton_clicked(){
QString temp=openBin();
if (temp.length()){
savefilename=temp.toUtf8().constData();
savePathDisplay->setText(temp);
if (savefilename!="" && keystreamfilename!=""){
this->decryptButton->setEnabled(true);
//if we got both, ungray
}
}
}
void Decrypter::on_decryptButton_clicked(){
if (savefilename!="" && keystreamfilename!=""){
int keystreamSize=0;
int saveSize=0;
std::ifstream saveStream; // using fstream for binary i/o for target file
std::ifstream keyStream; // using fstream for binary i/o for key file
std::ofstream decryptedsaveStream; // using fstream for binary i/o for key file
saveStream.open (savefilename.c_str(), std::ios::binary);
keyStream.open(keystreamfilename.c_str(), std::ios::binary);
std::string decryptedsavefilename;
time_t rawtime;
struct tm * timeinfo;
char buffer2 [100];
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strftime (buffer2,100,"Decrypted Save (%Y-%m-%d %H%M%S).bin",timeinfo);
decryptedsavefilename= buffer2;
std::cout << decryptedsavefilename<< std::endl;
QString filename = QFileDialog::getSaveFileName(
this,
tr("Save Save"),
tr(decryptedsavefilename.c_str()),
tr("Save files (*.bin)") );
if( !filename.isNull() ){
qDebug( filename.toUtf8() );
decryptedsavefilename=filename.toUtf8().constData();
decryptedsaveStream.open (decryptedsavefilename.c_str(), std::ios::binary);
// setting the reading position of the target file stream to the end
saveStream.seekg (0, std::ios::end);
// getting the position of the reading pointer, thereby retrieving target file size
saveSize = saveStream.tellg();
saveStream.seekg (0, std::ios::beg);
keyStream.seekg (0, std::ios::end);
keystreamSize = keyStream.tellg();
keyStream.seekg (0, std::ios::beg);
if (keystreamSize==saveSize && saveSize==1048732 && xysave->isChecked()){
char fruitBuffer[415232];
char buffer[415232];
char keyBuffer[415232];
saveStream.seekg (21660, std::ios::beg);
saveStream.read (buffer, 415232);
keyStream.seekg (21660, std::ios::beg);
keyStream.read (keyBuffer, 415232);
//don't write, unless savePointer is between 21504 and 436736
for (int i = 0; i <=415232; i++){
// inverting every byte in the buffer
fruitBuffer[i]=buffer[i] ^ keyBuffer [i];
}
// incrementing current file pointer by the amount of buffer
// adjusting interface
// setting writing pointer to the current location
// writing the inverted file
decryptedsaveStream.write (fruitBuffer, 415232);
saveStream.close();
keyStream.close();
decryptedsaveStream.close();
QMessageBox::information(
this,
tr("Pokemon Gen VI Save Decrypter"),
tr("Save decrypted!") );
}
else if (keystreamSize==saveSize && saveSize==1048732 && orassave->isChecked()){
char fruitBuffer[483328];
char buffer[483328];
char keyBuffer[483328];
saveStream.seekg (21660, std::ios::beg);
saveStream.read (buffer, 483328);
keyStream.seekg (21660, std::ios::beg);
keyStream.read (keyBuffer, 483328);
//don't write, unless savePointer is between 21504 and 504832
for (int i = 0; i <=483328; i++){
// inverting every byte in the buffer
fruitBuffer[i]=buffer[i] ^ keyBuffer [i];
}
// incrementing current file pointer by the amount of buffer
// adjusting interface
// setting writing pointer to the current location
// writing the inverted file
decryptedsaveStream.write (fruitBuffer, 483328);
saveStream.close();
keyStream.close();
decryptedsaveStream.close();
QMessageBox::information(
this,
tr("Pokemon Gen VI Save Decrypter"),
tr("Save decrypted!") );
}
}
}
}
//HEAP[PokemonXYSaveDecrypter.exe]:
//Heap block at 13957FE0 modified at 139BD5E8 past requested size of 65600
//so I dropped pointers and picked up vectors, until they made my outputs double the size
/*The inferior stopped because it received a signal from the Operating System.
Signal name :
SIGSEGV
Signal meaning :
Segmentation fault
*/
QString Decrypter::openBin()
{
QString filename = QFileDialog::getOpenFileName(
this,
tr("Open File"),
QDir::currentPath(),
"Binary files (*.bin);;All files (*.*)" );
if( !filename.isNull() )
{
qDebug( filename.toUtf8() );
}
return filename;//.toUtf8().constData();
}
void Decrypter::on_quitButton_clicked(){
//ask for confirmation, then exit
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Quit?", "Do you really want to quit?",
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes) {
//qDebug() << "Yes was clicked";
// QApplication::quit();
close();
qApp->quit();
}
/*else {
qDebug() << "Yes was *not* clicked";
}*/
}