Hi,

Thank you for helping with this. I basically have to encrypt a file in where the user enters the file name & enters a name for the output file. The encryption is based on the password the user enters. It is a case situation, that being said, if the user decides to decrypt, the names output file must be used. I already have the code ready to go for the encryption & decryption, I just can't figure out how to pull the file from just a name the user enters (I'm thinking pointers will have to be used somehow...). Likewise, I will need to be able to save an output file that is encrypted & if the user desires, to decrypt that as well.

Below are the two functions I have created, one for encrypting, one for decrypting. I can post the whole program but figured it wasn't necessary (is available upon request though!). The encryption works now, my last feat is to get the files working. Any criticism/help is appreciated.

void encrypt(int choice)
{
	//declare variables//
	char pw[20], name[20];
	int a,b,x,y;
	
		
	cout<<"Please enter your first name:\n";
	cin>>name;
	//prompt for password//
	cout<<"Please enter your password:\n";
	cin>>pw;

	x=strlen(name);
	y=strlen(pw);
	
	for(a=0,b=0; a<x; a++,b++){
		if (b>=y)
			b=0;
		name[a]=name[a]+pw[b];
	}
	cout<<endl<<name;
	cout<<endl;
	cout<<"Encryption is finished!\n";

	exit (0);
}


void decrypt(int choice)
{
	//declare variables//
	char pw[20], name[20];
	int a,b,x,y;
		
	cout<<"Please enter your first name:\n";
	cin>>name;
	//prompt for password//
	cout<<"Please enter your password:\n";
	cin>>pw;

	x=strlen(name);
	y=strlen(pw);
	
	for(a=0,b=0; a<x; a++,b++){
		if (b>=y)
			b=0;
		name[a]=name[a]-pw[b];
	}
	cout<<endl<<name;
	cout<<endl;
	cout<<"Decryption is finished!\n";
	
	exit (0);
}

Recommended Answers

All 4 Replies

Okay, so you have functions that encrypt and decrypt. Both of these functions take a parameter called choice, which isn't used. You talk about files, but I see no ofstreams or ifstreams.

There is a password that I assume is not encrypted and which functions as the encryption/decryption key. Presumably there is a plaintext message that is to be encrypted and the encrypted message is what is to be stored in the file. The filename itself is to be encrypted too? I wasn't too sure about that.

So it seems that to encrypt, three things must be specified. One, a filename (again, not sure whether the filename is to be encrypted). Two, the plain-text message (will definitely be encrypted and stored in the file). Three, the password (not stored in any file).

To decrypt, the filename and password are needed. Therefore, I think you should have these functions (I used C++-style-strings, but C-strings could be used instead):

void encrypt (string plaintext, string password, string filename)
// encrypts and stores in filename using password as key.

string decrypt (string password, string filename)
// returns plain-text message of what is stored in filename using password as key

Is that the idea? And again, you mention encryption of the filename itself. Is that accurate?

Thank You,

I didn't enter any ifstreams or ofstreams - I know I have to do that to read & create files. I will enter that. My problem is that I know how to use the ifstream if there is already a filename in a specific location (C:\\filename.txt for example). I just can't understand how to use the user-specified name for the file. Also, what extension it is, etc, etc. This is what I had been using previously:

const char* filename="c:\\test.txt";

How do I replace test.txt with the user-defined filename?

It was not mentioned to encrypt the filename so I do not believe that would be a requirement. I can tackle that last in hopes of extra credit :)

The choice parameter is the choice the user entered from the main function:
1. encrypt
2. decrypt
3. exit

I'm back & forth with work so I will make some revisions tonight & post after I have class. Thanks for your help so far.

My problem is that I know how to use the ifstream if there is already a filename in a specific location (C:\\filename.txt for example). I just can't understand how to use the user-specified name for the file. Also, what extension it is, etc, etc. This is what I had been using previously:

const char* filename="c:\\test.txt";

How do I replace test.txt with the user-defined filename?

Right now you specify the filename, but you can have the user specify it too. Here's a sample:

#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
    char filenameWithPath[40];
    cout << "Enter filename : ";
    cin >> filenameWithPath;
    
    ofstream file;
    file.open (filenameWithPath);
    cout << "Writing to file " << filenameWithPath << endl;
    file << "Writing to the file.";
    file.close ();
    return 0;
}

So you could enter something like:

c:\somedirectory\encrypt.txt

and that'll be the name of the file. You may want to change it to a getline instead of using >> in case the directory has spaces.

It was not mentioned to encrypt the filename so I do not believe that would be a requirement. I can tackle that last in hopes of extra credit :)

Okay, I was a little confused.

ah I see.....Let me play around with it a bit to see what I can get...thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.