strings encryption and decryption

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 5232 Views - Last Post: 11 December 2011 - 06:42 PM Rate Topic: -----

#1 cjolejniczak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 11-December 11

strings encryption and decryption

Posted 11 December 2011 - 10:01 AM

I think this code is right to encrypt and decrypt a file it runs but will not open the file.
#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;
in.open(filename);       
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;
out.open(filename);
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;


This post has been edited by jimblumberg: 11 December 2011 - 10:02 AM
Reason for edit:: Fixed Code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: strings encryption and decryption

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,346
  • Joined: 25-December 09

Re: strings encryption and decryption

Posted 11 December 2011 - 10:07 AM

You need to find an indentation style you like and use it consistently. You code as presented is almost impossible to read and decipher. You say it will not open a file. Which file? Your input or output file? Are you sure you are entering the correct location and name of the file?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 cjolejniczak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 11-December 11

Re: strings encryption and decryption

Posted 11 December 2011 - 10:09 AM

View Postjimblumberg, on 11 December 2011 - 10:07 AM, said:

You need to find an indentation style you like and use it consistently. You code as presented is almost impossible to read and decipher. You say it will not open a file. Which file? Your input or output file? Are you sure you are entering the correct location and name of the file?

Jim

It wont open the input file. The file I am trying to use is a notepad document test.txt format
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,346
  • Joined: 25-December 09

Re: strings encryption and decryption

Posted 11 December 2011 - 10:14 AM

Post a sample of your input file. Along with a complete program. The program you posted is either missing parts of the code or you have code that is not in any functions. And before you post the correct code make sure you reformat the code so it is readable.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: strings encryption and decryption

Posted 11 December 2011 - 11:16 AM

If your program won't open a text file, you probably put the file in the wrong directory or you aren't supplying the correct filename (including the path if necessary).

Here's a shortened (runnable) version of your program with only a couple of lines commented out and 1 cout statement added, and it opens a file with no problems.

#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;
    in.open(filename);
    if (in.fail())
    {
        cout<<"input file did not open please check it\n";
//            system("pause");
        return 1;
    }
    cout << "Input file has been opened" << endl;
    in.close();
}



So, where (which directory/folder) is the program stored, where is the input file stored, what is the input filename, and exactly what is your input when you are prompted for the name?

This post has been edited by r.stiltskin: 11 December 2011 - 11:18 AM

Was This Post Helpful? 0
  • +
  • -

#6 cjolejniczak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 11-December 11

Re: strings encryption and decryption

Posted 11 December 2011 - 11:39 AM

the file is stored under C:\Users\clolejniczak\Documents\final.txt the program is stored under my microsoft visual studio 2010 under projects folder.
Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: strings encryption and decryption

Posted 11 December 2011 - 11:45 AM

Then try entering this when prompted for the filename:

C:\\Users\\clolejniczak\\Documents\\final.txt
Was This Post Helpful? 0
  • +
  • -

#8 cjolejniczak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 11-December 11

Re: strings encryption and decryption

Posted 11 December 2011 - 11:50 AM

I typed it exactly as you said and it still will not pull the file
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,346
  • Joined: 25-December 09

Re: strings encryption and decryption

Posted 11 December 2011 - 11:57 AM

You should not need the double "\\", C:\Users\clolejniczak\Documents\final.txt should work just fine. I suggest you replace the cin >> filename; with getline() to insure this is not a embedded space issue. Also try printing out the variable "filename" along with the strlen() of this C-string.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 cjolejniczak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 11-December 11

Re: strings encryption and decryption

Posted 11 December 2011 - 03:16 PM

I am not sure were to put strlen and the getline()
Was This Post Helpful? 0
  • +
  • -

#11 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: strings encryption and decryption

Posted 11 December 2011 - 03:22 PM

Are you sure you're spelling it correctly? I just noticed that the 2nd letter of your username is 'j' and the 2nd letter of the User's name in your file path is 'l'.
Was This Post Helpful? 0
  • +
  • -

#12 cjolejniczak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 11-December 11

Re: strings encryption and decryption

Posted 11 December 2011 - 03:25 PM

yeah my username is cj here but the computer user name is cl for me and my wifes first initials
Was This Post Helpful? 0
  • +
  • -

#13 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: strings encryption and decryption

Posted 11 December 2011 - 03:30 PM

Add
#include <cstring> at the top of your program and insert this in your code at line 29 (right after the filename input):

    cout << "filename is: " << filename << endl;
    cout << "length is: " << strlen(filename) << endl;



Let's see what it prints when you run it.
Was This Post Helpful? 0
  • +
  • -

#14 cjolejniczak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 11-December 11

Re: strings encryption and decryption

Posted 11 December 2011 - 03:38 PM

still getting the same error do you want me to use the shorter code or the original code
Was This Post Helpful? 0
  • +
  • -

#15 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: strings encryption and decryption

Posted 11 December 2011 - 03:43 PM

#include <cstring>
#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;
    cout << "filename is: " << filename << endl;
    cout << "length is: " << strlen(filename) << endl;

 
    in.open(filename);
    if (in.fail())
    {
        cout<<"input file did not open please check it\n";
//            system("pause");
        return 1;
    }
    cout << "Input file has been opened" << endl;
    in.close();
}



compile/run that & cut/paste/post the exact output.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3