Working with a File
The TEAgentFile class represents a file. It is used to encrypt, decrypt a file given the file path. The following sequence describes how to encrypt and decrypt a file using the TEAgentFile class.
- Create a TEAgentFile object
- Set the path of the file to be encrypted or decrypted and set the path of the output file
- Attach a connection
- Encrypt or decrypt the file
- Detach the connection
The following example demonstrates file encryption and decryption using this class.
Code Sample
See the C++ environment settings for details on setting the project environment.
<cpp_file.cpp>
/*****************************************************************************************
* cpp_file.cpp
*
*
******************************************************************************************/
#include <iostream>
#include "teconnection.h"
#include "teagent.h"
#include "teexception.h"
using namespace ERUCES;
using namespace std;
int main()
{
string strServerName = "localhost"; // server name
long lPort = 5050; // port number
string strUsername = "ls"; // username
string strPassword = "password"; // password
try
{
auto_ptr<TEConnection> conn =
TEConnection::createInstance(TEConnection::SecureConnection);
// Open connection to the server
conn->open(strServerName, lPort, strUsername, strPassword);
cout << "Connected to " << strServerName << " at " << lPort
<< " as " << strUsername << "\n" << endl;
// Create a TEAgentFile object
TEAgentFile file;
// Set original data file and encrypted output file
file.setInputFileName(".\\Test.txt");
file.setOutputFileName(".\\Test.txt.eru");
// Encrypt file
file.attachConnection(conn.get());
file.encrypt();
file.detachConnection();
cout << "File " << file.getInputFileName() << " was encrypted, the output file is " <<
file.getOutputFileName() << endl;
file.clear();
// Set encrypted data file and decrypted output file
file.setInputFileName(".\\Test.txt.eru");
file.setOutputFileName(".\\Test.txt.out");
// Decrypt file
file.attachConnection(conn.get());
file.decrypt();
file.detachConnection();
cout << "File " << file.getInputFileName() << " was decrypted, the output file is " <<
file.getOutputFileName() << endl;
// Close conneciton
conn->close();
cout << "Disconnected from server." << endl;
}
catch (TEException &e)
{
cout << e.getAllMessages() << endl;
}
catch (...)
{
cout << "Unexpected error" << endl;
}
return 0;
}
Output
Connected to localhost at 8888 as ls
File .\Test.txt was encrypted, the output file is .\Test.txt.eru
File .\Test.txt.eru was decrypted, the output file is .\Test.txt.out
Disconnected from server.