2009-10-17 03:14:47 +00:00
|
|
|
#include <qpdf/Pl_AES_PDF.hh>
|
|
|
|
#include <qpdf/Pl_StdioFile.hh>
|
2019-06-21 03:35:23 +00:00
|
|
|
#include <qpdf/QIntC.hh>
|
2013-02-28 21:45:11 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
2009-10-17 03:14:47 +00:00
|
|
|
|
2023-05-20 11:22:32 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2009-10-17 03:14:47 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage()
|
|
|
|
{
|
2012-12-28 02:37:57 +00:00
|
|
|
std::cerr << "Usage: aes options hex-key infile outfile" << std::endl
|
|
|
|
<< " -cbc -- disable CBC mode" << std::endl
|
|
|
|
<< " +cbc -- enable CBC mode" << std::endl
|
|
|
|
<< " -encrypt -- encrypt" << std::endl
|
|
|
|
<< " -decrypt -- decrypt CBC mode" << std::endl
|
|
|
|
<< " -zero-iv -- use zero initialization vector" << std::endl
|
|
|
|
<< " -static-iv -- use static initialization vector" << std::endl
|
|
|
|
<< " -no-padding -- disable padding" << std::endl
|
|
|
|
<< "Options must precede key and file names." << std::endl;
|
2009-10-17 03:14:47 +00:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
2012-12-28 02:37:57 +00:00
|
|
|
bool encrypt = true;
|
2009-10-17 23:58:59 +00:00
|
|
|
bool cbc_mode = true;
|
2023-05-20 11:46:50 +00:00
|
|
|
char* hexkey = nullptr;
|
|
|
|
char* infilename = nullptr;
|
|
|
|
char* outfilename = nullptr;
|
2012-12-28 02:37:57 +00:00
|
|
|
bool zero_iv = false;
|
|
|
|
bool static_iv = false;
|
|
|
|
bool disable_padding = false;
|
2009-10-17 03:14:47 +00:00
|
|
|
|
2012-12-28 02:37:57 +00:00
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
char* arg = argv[i];
|
|
|
|
if ((arg[0] == '-') || (arg[0] == '+')) {
|
|
|
|
if (strcmp(arg, "-cbc") == 0) {
|
|
|
|
cbc_mode = false;
|
|
|
|
} else if (strcmp(arg, "+cbc") == 0) {
|
|
|
|
cbc_mode = true;
|
|
|
|
} else if (strcmp(arg, "-decrypt") == 0) {
|
|
|
|
encrypt = false;
|
|
|
|
} else if (strcmp(arg, "-encrypt") == 0) {
|
|
|
|
encrypt = true;
|
|
|
|
} else if (strcmp(arg, "-zero-iv") == 0) {
|
|
|
|
zero_iv = true;
|
|
|
|
} else if (strcmp(arg, "-static-iv") == 0) {
|
|
|
|
static_iv = true;
|
|
|
|
} else if (strcmp(arg, "-no-padding") == 0) {
|
|
|
|
disable_padding = true;
|
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
} else if (argc == i + 3) {
|
|
|
|
hexkey = argv[i];
|
|
|
|
infilename = argv[i + 1];
|
|
|
|
outfilename = argv[i + 2];
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
}
|
2009-10-17 03:14:47 +00:00
|
|
|
}
|
2023-05-20 11:46:50 +00:00
|
|
|
if (outfilename == nullptr) {
|
2012-12-28 02:37:57 +00:00
|
|
|
usage();
|
2009-10-17 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
unsigned int hexkeylen = QIntC::to_uint(strlen(hexkey));
|
2009-10-17 03:14:47 +00:00
|
|
|
unsigned int keylen = hexkeylen / 2;
|
|
|
|
|
2013-02-28 21:45:11 +00:00
|
|
|
FILE* infile = QUtil::safe_fopen(infilename, "rb");
|
|
|
|
FILE* outfile = QUtil::safe_fopen(outfilename, "wb");
|
2023-05-20 12:24:10 +00:00
|
|
|
auto* key = new unsigned char[keylen];
|
2009-10-17 03:14:47 +00:00
|
|
|
for (unsigned int i = 0; i < strlen(hexkey); i += 2) {
|
2022-02-08 14:18:08 +00:00
|
|
|
char t[3];
|
|
|
|
t[0] = hexkey[i];
|
|
|
|
t[1] = hexkey[i + 1];
|
|
|
|
t[2] = '\0';
|
2009-10-17 03:14:47 +00:00
|
|
|
|
2023-05-20 11:46:50 +00:00
|
|
|
long val = strtol(t, nullptr, 16);
|
2022-02-08 14:18:08 +00:00
|
|
|
key[i / 2] = static_cast<unsigned char>(val);
|
2009-10-17 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 12:24:10 +00:00
|
|
|
auto* out = new Pl_StdioFile("stdout", outfile);
|
|
|
|
auto* aes = new Pl_AES_PDF("aes_128_cbc", out, encrypt, key, keylen);
|
2012-12-28 02:37:57 +00:00
|
|
|
delete[] key;
|
2023-05-20 11:46:50 +00:00
|
|
|
key = nullptr;
|
2009-10-17 23:58:59 +00:00
|
|
|
if (!cbc_mode) {
|
2022-02-08 14:18:08 +00:00
|
|
|
aes->disableCBC();
|
2009-10-17 23:58:59 +00:00
|
|
|
}
|
2012-12-28 02:37:57 +00:00
|
|
|
if (zero_iv) {
|
|
|
|
aes->useZeroIV();
|
|
|
|
} else if (static_iv) {
|
|
|
|
aes->useStaticIV();
|
|
|
|
}
|
|
|
|
if (disable_padding) {
|
|
|
|
aes->disablePadding();
|
|
|
|
}
|
2009-10-17 03:14:47 +00:00
|
|
|
|
|
|
|
// 16 < buffer size, buffer_size is not a multiple of 8 for testing
|
|
|
|
unsigned char buf[83];
|
|
|
|
bool done = false;
|
|
|
|
while (!done) {
|
2022-02-08 14:18:08 +00:00
|
|
|
size_t len = fread(buf, 1, sizeof(buf), infile);
|
|
|
|
if (len <= 0) {
|
|
|
|
done = true;
|
|
|
|
} else {
|
|
|
|
aes->write(buf, len);
|
|
|
|
}
|
2009-10-17 03:14:47 +00:00
|
|
|
}
|
|
|
|
aes->finish();
|
|
|
|
delete aes;
|
|
|
|
delete out;
|
|
|
|
fclose(infile);
|
|
|
|
fclose(outfile);
|
|
|
|
return 0;
|
|
|
|
}
|