|
|
@@ -28,17 +28,59 @@
|
|
|
#endif
|
|
|
#endif
|
|
|
|
|
|
+string password;
|
|
|
+bool got_password = false;
|
|
|
+string algorithm;
|
|
|
+bool got_algorithm = false;
|
|
|
+int key_length = 0;
|
|
|
+bool got_key_length = false;
|
|
|
+int iteration_count = 0;
|
|
|
+bool got_iteration_count = false;
|
|
|
+
|
|
|
+bool
|
|
|
+do_encrypt(istream &read_stream, ostream &write_stream) {
|
|
|
+ OEncryptStream encrypt;
|
|
|
+ if (got_algorithm) {
|
|
|
+ encrypt.set_algorithm(algorithm);
|
|
|
+ }
|
|
|
+ if (got_key_length) {
|
|
|
+ encrypt.set_key_length(key_length);
|
|
|
+ }
|
|
|
+ if (got_iteration_count) {
|
|
|
+ encrypt.set_iteration_count(iteration_count);
|
|
|
+ }
|
|
|
+ encrypt.open(&write_stream, false, password);
|
|
|
+
|
|
|
+ static const size_t buffer_size = 1024;
|
|
|
+ char buffer[buffer_size];
|
|
|
+
|
|
|
+ read_stream.read(buffer, buffer_size);
|
|
|
+ size_t count = read_stream.gcount();
|
|
|
+ while (count != 0) {
|
|
|
+ encrypt.write(buffer, count);
|
|
|
+ read_stream.read(buffer, buffer_size);
|
|
|
+ count = read_stream.gcount();
|
|
|
+ }
|
|
|
+ encrypt.close();
|
|
|
+
|
|
|
+ return !read_stream.fail() || read_stream.eof() &&
|
|
|
+ (!encrypt.fail() || encrypt.eof());
|
|
|
+}
|
|
|
+
|
|
|
void
|
|
|
usage() {
|
|
|
cerr
|
|
|
<< "\n"
|
|
|
- << "Usage: pencrypt [opts] <file> [<dest_file>]\n\n"
|
|
|
+ << "Usage: pencrypt [opts] file [file2 file3 ...]\n\n"
|
|
|
|
|
|
- << "This program will apply an encryption algorithm to a file, creating an\n"
|
|
|
- << "encrypted version of the file which can only be recovered using pdecrypt and\n"
|
|
|
- << "the same password that was supplied to pencrypt. If the dest_file name is\n"
|
|
|
- << "not specified, a default output name is generated by appending .pe to the\n"
|
|
|
- << "input file name.\n\n"
|
|
|
+ << "This program will apply an encryption algorithm to a file (or multiple files),\n"
|
|
|
+ << "creating an encrypted version of each file which can only be recovered using\n"
|
|
|
+ << "pdecrypt and the same password that was supplied to pencrypt. For each input\n"
|
|
|
+ << "file, an output name is generated by appending .pe to the input file name.\n\n"
|
|
|
+
|
|
|
+ << "Note that if you are adding files to a Panda multifile (.mf file) with\n"
|
|
|
+ << "the multify command, it is not necessary to encrypt them separately;\n"
|
|
|
+ << "multify has an inline encryption option.\n\n"
|
|
|
|
|
|
<< "Options:\n\n"
|
|
|
|
|
|
@@ -76,15 +118,6 @@ main(int argc, char *argv[]) {
|
|
|
extern int optind;
|
|
|
const char *optstr = "p:a:k:i:h";
|
|
|
|
|
|
- string password;
|
|
|
- bool got_password = false;
|
|
|
- string algorithm;
|
|
|
- bool got_algorithm = false;
|
|
|
- int key_length = 0;
|
|
|
- bool got_key_length = false;
|
|
|
- int iteration_count = 0;
|
|
|
- bool got_iteration_count = false;
|
|
|
-
|
|
|
int flag = getopt(argc, argv, optstr);
|
|
|
|
|
|
while (flag != EOF) {
|
|
|
@@ -126,73 +159,59 @@ main(int argc, char *argv[]) {
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
- bool implicit_dest_file;
|
|
|
- Filename source_file = Filename::from_os_specific(argv[1]);
|
|
|
- Filename dest_file;
|
|
|
- if (argc < 3) {
|
|
|
- dest_file = source_file.get_fullpath() + ".pe";
|
|
|
- implicit_dest_file = true;
|
|
|
- } else {
|
|
|
- dest_file = Filename::from_os_specific(argv[2]);
|
|
|
- implicit_dest_file = false;
|
|
|
- }
|
|
|
-
|
|
|
- // Open source file
|
|
|
- ifstream read_stream;
|
|
|
- source_file.set_binary();
|
|
|
- if (!source_file.open_read(read_stream)) {
|
|
|
- cerr << "failed to open: " << source_file << endl;
|
|
|
- return 1;
|
|
|
- }
|
|
|
-
|
|
|
- // Open destination file
|
|
|
- ofstream write_stream;
|
|
|
- dest_file.set_binary();
|
|
|
- if (!dest_file.open_write(write_stream, true)) {
|
|
|
- cerr << "failed to open: " << dest_file << endl;
|
|
|
- return 1;
|
|
|
- }
|
|
|
-
|
|
|
- // Prompt for password.
|
|
|
- if (!got_password) {
|
|
|
- cerr << "Enter password: ";
|
|
|
- getline(cin, password);
|
|
|
- }
|
|
|
-
|
|
|
- bool fail = false;
|
|
|
- {
|
|
|
- OEncryptStream encrypt;
|
|
|
- if (got_algorithm) {
|
|
|
- encrypt.set_algorithm(algorithm);
|
|
|
- }
|
|
|
- if (got_key_length) {
|
|
|
- encrypt.set_key_length(key_length);
|
|
|
+ bool all_ok = true;
|
|
|
+ for (int i = 1; i < argc; i++) {
|
|
|
+ Filename source_file = Filename::from_os_specific(argv[i]);
|
|
|
+ if (source_file.get_extension() == "pe") {
|
|
|
+ cerr << source_file << " already ends .pe; skipping.\n";
|
|
|
+ } else {
|
|
|
+ Filename dest_file = source_file.get_fullpath() + ".pe";
|
|
|
+
|
|
|
+ // Open source file
|
|
|
+ ifstream read_stream;
|
|
|
+ source_file.set_binary();
|
|
|
+ if (!source_file.open_read(read_stream)) {
|
|
|
+ cerr << "Couldn't read: " << source_file << endl;
|
|
|
+ all_ok = false;
|
|
|
+
|
|
|
+ } else {
|
|
|
+ // Open destination file
|
|
|
+ ofstream write_stream;
|
|
|
+ dest_file.set_binary();
|
|
|
+ if (!dest_file.open_write(write_stream, true)) {
|
|
|
+ cerr << "Failed to open: " << dest_file << endl;
|
|
|
+ all_ok = false;
|
|
|
+
|
|
|
+ } else {
|
|
|
+ // Prompt for password.
|
|
|
+ if (!got_password) {
|
|
|
+ cerr << "Enter password: ";
|
|
|
+ getline(cin, password);
|
|
|
+ got_password = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ cerr << dest_file << "\n";
|
|
|
+ bool success = do_encrypt(read_stream, write_stream);
|
|
|
+
|
|
|
+ read_stream.close();
|
|
|
+ write_stream.close();
|
|
|
+
|
|
|
+ if (!success) {
|
|
|
+ cerr << "Failure writing " << dest_file << "\n";
|
|
|
+ all_ok = false;
|
|
|
+ dest_file.unlink();
|
|
|
+
|
|
|
+ } else {
|
|
|
+ bool ok = source_file.unlink();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- if (got_iteration_count) {
|
|
|
- encrypt.set_iteration_count(iteration_count);
|
|
|
- }
|
|
|
- encrypt.open(&write_stream, false, password);
|
|
|
-
|
|
|
- int ch = read_stream.get();
|
|
|
- while (!read_stream.eof() && !read_stream.fail()) {
|
|
|
- encrypt.put(ch);
|
|
|
- ch = read_stream.get();
|
|
|
- }
|
|
|
-
|
|
|
- fail = encrypt.fail() && !encrypt.eof();
|
|
|
}
|
|
|
|
|
|
- read_stream.close();
|
|
|
- write_stream.close();
|
|
|
-
|
|
|
- if (fail) {
|
|
|
- dest_file.unlink();
|
|
|
-
|
|
|
+ if (all_ok) {
|
|
|
+ return 0;
|
|
|
} else {
|
|
|
- if (implicit_dest_file) {
|
|
|
- source_file.unlink();
|
|
|
- }
|
|
|
+ return 1;
|
|
|
}
|
|
|
-
|
|
|
- return 0;
|
|
|
}
|