00001 #ifndef SECRET_STRING_IMPLEMENTATION_FILE 00002 #define SECRET_STRING_IMPLEMENTATION_FILE 00003 00004 /*****************************************************************************\ 00005 * * 00006 * Name : secret_string * 00007 * Author : Chris Koeritz * 00008 * * 00009 ******************************************************************************* 00010 * Copyright (c) 2004-$now By Author. This program is free software; you can * 00011 * redistribute it and/or modify it under the terms of the GNU General Public * 00012 * License as published by the Free Software Foundation; either version 2 of * 00013 * the License or (at your option) any later version. This is online at: * 00014 * http://www.fsf.org/copyleft/gpl.html * 00015 * Please send any updates to: fred@gruntose.com * 00016 \*****************************************************************************/ 00017 00018 #include "ice_key.h" 00019 #include "secret_string.h" 00020 00021 #include <basis/byte_array.h> 00022 #include <basis/function.h> 00023 #include <basis/log_base.h> 00024 #include <textual/byte_format.h> 00025 00026 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger(), s) 00027 00028 secret_string::secret_string(const byte_array &key) 00029 : _encryptor(new ice_key(key.length() / 8)), 00030 _valid(false) 00031 { 00032 FUNCDEF("constructor"); 00033 if (!_encryptor->set(key)) { 00034 LOG("failed to set the key, probably due to erroneous size."); 00035 return; 00036 } 00037 _valid = true; 00038 } 00039 00040 secret_string::~secret_string() 00041 { 00042 _valid = false; 00043 WHACK(_encryptor); 00044 } 00045 00046 istring secret_string::encrypt_string(const istring &to_encrypt) 00047 { 00048 FUNCDEF("encrypt_string"); 00049 if (!valid()) return ""; 00050 // first encrypt the string as bytes. 00051 byte_array clear_bytes(to_encrypt.length() + 1, (byte *)to_encrypt.s()); 00052 byte_array crypt_bytes; 00053 if (!_encryptor->encrypt(clear_bytes, crypt_bytes)) return ""; 00054 // then turn the bytes into a readable string. 00055 istring encrypted; 00056 byte_format::bytes_to_string(crypt_bytes, encrypted, false); 00057 return encrypted; 00058 } 00059 00060 istring secret_string::decrypt_string(const istring &to_decrypt) 00061 { 00062 FUNCDEF("decrypt_string"); 00063 if (!valid()) return ""; 00064 // get byte_array back from the string. 00065 byte_array enc_bytes; 00066 byte_format::string_to_bytes(to_decrypt, enc_bytes); 00067 // now decrypt the byte form to get back clear bytes. 00068 byte_array clear_bytes; 00069 if (!_encryptor->decrypt(enc_bytes, clear_bytes)) return ""; 00070 return istring((char *)clear_bytes.observe()); 00071 } 00072 00073 00074 #endif //SECRET_STRING_IMPLEMENTATION_FILE 00075
1.5.1