Logo Search packages:      
Sourcecode: yapet version File versions  Download package

void File::setNewKey ( const Key oldkey,
const Key newkey 
) throw (YAPETException)

Sets a new encryption key for the current file.

Uses a new key to encrypt the records in the file. The records stored in the file are decrypted using the old key and then encrypted using the new key.

Before performing this operation, it renames the file encrypted with the old key to 'filename + ".bak"'. It then reads the data from this file and writes it to the newly created file named 'filename'.

oldkey the old key used to encrypt the records
newkey the new key used to encrypt the records
secure refer to File::save for explanation of this parameter.

Definition at line 768 of file file.cc.

References YAPET::Crypt::decrypt(), YAPET::Crypt::encrypt(), fd, File(), filename, initFile(), openCreate(), openNoCreate(), read(), and write().

    std::string backupfilename(filename + ".bak");
    int retval = rename(filename.c_str(), backupfilename.c_str());
    if (retval == -1) {
      // Reopen the old file
      throw YAPETException(strerror(errno));

    File* oldfile = NULL;
    try {
      // Reopen the old (backup) file
      oldfile = new File(backupfilename, oldkey, false, false);
      // Initialize the (this) file with the new key

      // Retrieve the records encrypted with the old key
      std::list<PartDec> entries = oldfile->read(oldkey);
      std::list<PartDec>::iterator it = entries.begin();
      Crypt oldcrypt(oldkey);
      Crypt newcrypt(newkey);
      while (it != entries.end() ) {
          Record<PasswordRecord>* dec_rec_ptr = NULL;
          BDBuffer* new_enc_rec = NULL;
          try {
            // Decrypt with the old key
            const BDBuffer old_enc_rec = (*it).getEncRecord();
            dec_rec_ptr =
            new_enc_rec =
            delete dec_rec_ptr;
            delete new_enc_rec;
          } catch (YAPETException& ex) {
            if (dec_rec_ptr != NULL)
                delete dec_rec_ptr;
            if (new_enc_rec != NULL)
                delete new_enc_rec;
    } catch (YAPETException& ex) {
      if (oldfile != NULL)
          delete oldfile;
    delete oldfile;

Generated by  Doxygen 1.6.0   Back to index