00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "encrypt_string.h"
00016
00017 #ifdef HAVE_OPENSSL
00018 #include "encryptStream.h"
00019 #include "virtualFileSystem.h"
00020 #include "config_express.h"
00021
00022
00023
00024
00025
00026
00027
00028
00029 string
00030 encrypt_string(const string &source, const string &password,
00031 const string &algorithm, int key_length, int iteration_count) {
00032 ostringstream dest;
00033
00034 {
00035 OEncryptStream encrypt;
00036 if (!algorithm.empty()) {
00037 encrypt.set_algorithm(algorithm);
00038 }
00039 if (key_length > 0) {
00040 encrypt.set_key_length(key_length);
00041 }
00042 if (iteration_count >= 0) {
00043 encrypt.set_iteration_count(iteration_count);
00044 }
00045 encrypt.open(&dest, false, password);
00046 encrypt.write(source.data(), source.length());
00047
00048 if (encrypt.fail()) {
00049 return string();
00050 }
00051 }
00052
00053 return dest.str();
00054 }
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 string
00069 decrypt_string(const string &source, const string &password) {
00070 istringstream source_stream(source);
00071 ostringstream dest_stream;
00072
00073 if (!decrypt_stream(source_stream, dest_stream, password)) {
00074 return string();
00075 }
00076
00077 return dest_stream.str();
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 EXPCL_PANDAEXPRESS bool
00090 encrypt_file(const Filename &source, const Filename &dest, const string &password,
00091 const string &algorithm, int key_length, int iteration_count) {
00092 VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
00093 Filename source_filename = source;
00094 if (!source_filename.is_binary_or_text()) {
00095
00096 source_filename.set_binary();
00097 }
00098 istream *source_stream = vfs->open_read_file(source_filename, true);
00099 if (source_stream == NULL) {
00100 express_cat.info() << "Couldn't open file " << source_filename << "\n";
00101 return false;
00102 }
00103
00104 Filename dest_filename = Filename::binary_filename(dest);
00105 ostream *dest_stream = vfs->open_write_file(dest_filename, true, true);
00106 if (dest_stream == NULL) {
00107 express_cat.info() << "Couldn't open file " << dest_filename << "\n";
00108 vfs->close_read_file(source_stream);
00109 return false;
00110 }
00111
00112 bool result = encrypt_stream(*source_stream, *dest_stream, password,
00113 algorithm, key_length, iteration_count);
00114 vfs->close_read_file(source_stream);
00115 vfs->close_write_file(dest_stream);
00116 return result;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 EXPCL_PANDAEXPRESS bool
00134 decrypt_file(const Filename &source, const Filename &dest, const string &password) {
00135 Filename source_filename = Filename::binary_filename(source);
00136 VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
00137 istream *source_stream = vfs->open_read_file(source_filename, false);
00138 if (source_stream == NULL) {
00139 express_cat.info() << "Couldn't open file " << source_filename << "\n";
00140 return false;
00141 }
00142
00143 Filename dest_filename = dest;
00144 if (!dest_filename.is_binary_or_text()) {
00145
00146 dest_filename.set_binary();
00147 }
00148 ostream *dest_stream = vfs->open_write_file(dest_filename, true, true);
00149 if (dest_stream == NULL) {
00150 express_cat.info() << "Couldn't open file " << dest_filename << "\n";
00151 vfs->close_read_file(source_stream);
00152 return false;
00153 }
00154
00155 bool result = decrypt_stream(*source_stream, *dest_stream, password);
00156 vfs->close_read_file(source_stream);
00157 vfs->close_write_file(dest_stream);
00158 return result;
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 bool
00172 encrypt_stream(istream &source, ostream &dest, const string &password,
00173 const string &algorithm, int key_length, int iteration_count) {
00174 OEncryptStream encrypt;
00175 if (!algorithm.empty()) {
00176 encrypt.set_algorithm(algorithm);
00177 }
00178 if (key_length > 0) {
00179 encrypt.set_key_length(key_length);
00180 }
00181 if (iteration_count >= 0) {
00182 encrypt.set_iteration_count(iteration_count);
00183 }
00184 encrypt.open(&dest, false, password);
00185
00186 static const size_t buffer_size = 4096;
00187 char buffer[buffer_size];
00188
00189 source.read(buffer, buffer_size);
00190 size_t count = source.gcount();
00191 while (count != 0) {
00192 encrypt.write(buffer, count);
00193 source.read(buffer, buffer_size);
00194 count = source.gcount();
00195 }
00196 encrypt.close();
00197
00198 return (!source.fail() || source.eof()) && (!encrypt.fail());
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 bool
00217 decrypt_stream(istream &source, ostream &dest, const string &password) {
00218 IDecryptStream decrypt(&source, false, password);
00219
00220 static const size_t buffer_size = 4096;
00221 char buffer[buffer_size];
00222
00223 decrypt.read(buffer, buffer_size);
00224 size_t count = decrypt.gcount();
00225 while (count != 0) {
00226 dest.write(buffer, count);
00227 decrypt.read(buffer, buffer_size);
00228 count = decrypt.gcount();
00229 }
00230
00231 return (!decrypt.fail() || decrypt.eof()) && (!dest.fail());
00232 }
00233
00234 #endif // HAVE_OPENSSL