8 Replies - 635 Views - Last Post: 24 June 2013 - 08:34 AM Rate Topic: -----

#1 treetard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-June 13

how do I encrypt and decrypt it using notepad

Posted 23 June 2013 - 07:55 PM

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

void mapitE(string,char[]);
void mapitD(string,char[]);
void encryptdecrypt(const string,const char[],int,string&);

int main()

{
	string ende[]={"encrypt","decrypt"},buffer,key,newbuffer="";

	char filename[80],enmap[128],demap[128],again;

	int i,maplength;

	ofstream out;

	ifstream in;

	int choice;


	do

{

	cout<<"Enter 1 to encrypt, 2 to decrypt: ";

	cin>>choice;

	while(choice<1||choice>2)

{
	cout<<"invalid choice\n";

	cout<<"Enter 1 to encrypt, 2 to decrypt: ";

	cin>>choice;

}


	cout<<"Enter name of your file to "<<ende[choice-1]<<": ";

	cin>>filename;

	if (filename=="input")
		in.open("input.txt");


	if(in.fail())

{
	cout<<"input file did not open please check it\n";

	system("pause");

	return 1;

}

	cout<<"Enter name of your output file: ";

	cin>>filename;

	if (filename=="output")
	{
		out.open("output.txt");
	}
	cout<<"Enter your encryption key (max 128 characters): ";

	cin>>key;

	if(key.length()>128)

{
	cout<<"key too long\n";

	cout<<"Enter your encryption key (max 128 characters): ";

	cin>>key;

}

	maplength=key.length();

 

	if(choice==1)

	mapitE(key,enmap);

	else

	mapitD(key,demap);

	getline(in,buffer);

	while(in)
{

	if(choice==1)

	encryptdecrypt(buffer,enmap,maplength,newbuffer);

	else

	encryptdecrypt(buffer,demap,maplength,newbuffer);

	out<<newbuffer;

	newbuffer.erase(0);

	getline(in,buffer);

 

}

	out.close();

	in.close();

	in.clear();

	out.clear();

	newbuffer.erase(0);

	cout<<"do it again(y/n)? ";

	cin>>again;

}
	while(toupper(again)=='Y');

return 0;

}
	void encryptdecrypt(const string buffer,const char map[],int len,string& newbuffer)

{
	int i=0;

	char t,code;

	for(i=0;i<buffer.length();i++)

{ 
	cout<<buffer[i]<<" "<<i<<endl;

	if(isalpha(buffer[i]))

{
	if(islower(buffer[i]))

	code='a';

	else

	code='A';

	t=buffer[i]-code;

	

	t=t+map[i%len];

	t=t%26;

	t=t+code;

	newbuffer.push_back(t);

}

	else

	newbuffer.push_back(buffer[i]);

}

	newbuffer.push_back('\n');

}

	void mapitE(string key,char map[])

{

	int i;

	for(i=0;i<key.length();i++)

	map[i]=key[i]-'a';

}

	void mapitD(string key,char map[])

{
	int i;

	for(i=0;i<key.length();i++)

	map[i]=26-(key[i]-'a');

}



how in the world do I encrypt and decrypt it using notepad? I am using Visual Studio C++ program but I am not sure what to do after I debug my solution and it opens up the window for me to select either encrypt of decrypt...it keeps asking me for a file name i give it my file name then it asks for my output name and i give it but the encryption doesnt get sent to it. I can't find where it goes too.

This post has been edited by jimblumberg: 23 June 2013 - 08:19 PM
Reason for edit:: Added missing code tags. Please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: how do I encrypt and decrypt it using notepad

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: how do I encrypt and decrypt it using notepad

Posted 23 June 2013 - 08:22 PM

Why are you using all the C-strings instead of std::string? You can't use the comparison operator== with C-strings. I suggest you use std::string instead.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,578
  • Joined: 05-May 12

Re: how do I encrypt and decrypt it using notepad

Posted 23 June 2013 - 09:19 PM

Also, what does notepad have to do with this problem?
Was This Post Helpful? 1
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: how do I encrypt and decrypt it using notepad

Posted 23 June 2013 - 10:13 PM

As said above, using the == operator on C strings will only compare the memory addresses, not the string contents. With that said, what is the purpose of validating the user input here?

if (filename=="input")
		in.open("input.txt");



Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,219
  • Joined: 23-August 08

Re: how do I encrypt and decrypt it using notepad

Posted 24 June 2013 - 03:22 AM

Hmmm....remarkably similar, but from 11 months ago?

Question here:

Quote

how in the world do I encrypt and decrypt it using notepad? I am using Visual Studio C++ program but I am not sure what to do after I debug my solution and it opens up the window for me to select either encrypt of decrypt...it keeps asking me for a file name i give it my file name then it asks for my output name and i give it but the encryption doesnt get sent to it. I can't find where it goes too.


Question there:

Quote

I have the code.. but how in the world do I encrypt and decrypt it using notepad? I am using Visual Studio C++ program but I am not sure what to do after I debug my solution and it opens up the window for me to select either encrypt of decrypt...it keeps asking me for a file name? Please Help!


:dubious:
Was This Post Helpful? 1
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,578
  • Joined: 05-May 12

Re: how do I encrypt and decrypt it using notepad

Posted 24 June 2013 - 05:57 AM

Check out this thread from last year right here in DIC. It even has the same mistake for string comparison. :)
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,219
  • Joined: 23-August 08

Re: how do I encrypt and decrypt it using notepad

Posted 24 June 2013 - 06:28 AM

Yeah, this is all very confusing... :dontgetit:

Further Google research suggests this is a DeVry assignment.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,578
  • Joined: 05-May 12

Re: how do I encrypt and decrypt it using notepad

Posted 24 June 2013 - 06:49 AM

Ahhh... So although it's called a "lab", it looks like it is being run as an assignment -- so there is no teacher, teacher's assistant, or lab assistant to ask questions from.
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: how do I encrypt and decrypt it using notepad

Posted 24 June 2013 - 08:34 AM

Look at the following snippet:
      cout << "Enter name of your file to " << ende[choice - 1] << ": ";

      cin >> filename;

      if(filename == "input")
         in.open("input.txt");


      if(in.fail())



As already stated you can't use the operator== with C-strings, you need to use strcmp(). But once you take care of that issue, what happens if you enter something other than "input"? If you don't open a file because you enter something else the stream will not be in a fail state so the following if will not be executed.

Jim

This post has been edited by jimblumberg: 24 June 2013 - 08:35 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1