2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/Pl_RC4.hh>
|
|
|
|
#include <qpdf/Pl_StdioFile.hh>
|
2013-02-28 21:45:11 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
if (argc != 4)
|
|
|
|
{
|
|
|
|
std::cerr << "Usage: rc4 hex-key infile outfile" << std::endl;
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* hexkey = argv[1];
|
|
|
|
char* infilename = argv[2];
|
|
|
|
char* outfilename = argv[3];
|
2013-02-24 02:46:21 +00:00
|
|
|
unsigned int hexkeylen = strlen(hexkey);
|
2012-06-20 15:20:57 +00:00
|
|
|
unsigned int keylen = hexkeylen / 2;
|
2008-04-29 12:55:25 +00:00
|
|
|
unsigned char* key = new unsigned char[keylen + 1];
|
|
|
|
key[keylen] = '\0';
|
|
|
|
|
2013-02-28 21:45:11 +00:00
|
|
|
FILE* infile = QUtil::safe_fopen(infilename, "rb");
|
2008-04-29 12:55:25 +00:00
|
|
|
for (unsigned int i = 0; i < strlen(hexkey); i += 2)
|
|
|
|
{
|
|
|
|
char t[3];
|
|
|
|
t[0] = hexkey[i];
|
|
|
|
t[1] = hexkey[i + 1];
|
|
|
|
t[2] = '\0';
|
|
|
|
|
|
|
|
long val = strtol(t, 0, 16);
|
2013-02-24 02:46:21 +00:00
|
|
|
key[i/2] = static_cast<unsigned char>(val);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 21:45:11 +00:00
|
|
|
FILE* outfile = QUtil::safe_fopen(outfilename, "wb");
|
2008-04-29 12:55:25 +00:00
|
|
|
Pl_StdioFile* out = new Pl_StdioFile("stdout", outfile);
|
|
|
|
// Use a small buffer size (64) for testing
|
|
|
|
Pl_RC4* rc4 = new Pl_RC4("rc4", out, key, keylen, 64);
|
|
|
|
delete [] key;
|
|
|
|
|
|
|
|
// 64 < buffer size < 512, buffer_size is not a power of 2 for testing
|
|
|
|
unsigned char buf[100];
|
|
|
|
bool done = false;
|
|
|
|
while (! done)
|
|
|
|
{
|
2012-06-20 15:20:57 +00:00
|
|
|
size_t len = fread(buf, 1, sizeof(buf), infile);
|
2008-04-29 12:55:25 +00:00
|
|
|
if (len <= 0)
|
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rc4->write(buf, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rc4->finish();
|
|
|
|
delete rc4;
|
|
|
|
delete out;
|
|
|
|
fclose(infile);
|
|
|
|
fclose(outfile);
|
|
|
|
return 0;
|
|
|
|
}
|