decryption program - error

I'm writing a program to implement a simple encryption(shift of 3 units). The encrypt function works perfectly , however the decrypt is not decrypting the encrypted string but instead decrypts the entered(original) string itself.

what is it that I could change?

Here is the program:

---
//Program to design and implement an encryptstring class that encrypts and decrypts a string

#include<string>
#include<iostream>
#include<cstring>


using namespace std;


class encryptstring
{
private:
string string1; //the string that user enters

public:
encryptstring(){string1="";}
string s1,enc[50],dec[50],temp[5]; //string that holds the encrypted and decrypted strings
encryptstring(string s1){string1 = s1;} //constructor

string encrypt(string s2) // code to encrypt the string
{ int i;
for(i=0;i<s2.length();i++)
{
enc[i]+=s2[i]+3;
cout<<enc[i];
}
return enc[i];

}

string decrypt(string s1) //code to decrypt the string
{ int i;
for(i=0;i<s1.length();i++)
{
temp[i] = s1[i]-3;
dec[i]+= temp[i];
}
return dec[i];

}
string getst(string){ return string1;}
string setst(string s1){ string1=s1;}

}; //class ends


int main()
{
encryptstring object1;
string enter;
cout<<"Enter a string: ";
getline(cin,enter);
object1.getst(enter);
object1.setst(enter);
cout<<"\nThis is the string that you entered: "<<enter<<endl;
cout<<"\nYour string in encrypted form is ";
object1.encrypt(enter);
cin.get();
cout<<"\nYour string in decrypted form is ";
object1.decrypt(enter);
// object1.decrypt(object1.enc[50]);
cin.get();
return 0;

}
-----------

Please suggest..
here is a much more simple program that works


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

int main()
{
cout << "enter a password you want to encrypt,it has to be only letters: ";
cout << endl;
string c;
getline(cin, c);
for (int i = 0; i<c.size();i++)
{
if( c[i] != ' ' )
{
c[i] = tolower(c[i]);
c[i] = c[i]-3;
if (c[i]<97)
c[i] = c[i]+26;
}

}

cout << c << endl;
system("pause");
return 0;
}

tell me how it goes
Last edited on
ohhh you want to decrypt it tooo
thank you moot.
yes i'm actually having problem with the decryption. the encryption part of it is super easy.
You are throwing away the string returned from the following call: object1.encrypt(enter);

The value of 'enter' is not being changed, and then you're passing it to decrypt().

Jim
jim thanks!
Yes I figured something similar but I'm not able to understand the value/parameter that goes to decrypt function? so do I change the parameters then?
I have now changed the program a bit, to retain the value of first function and pass it to the second

This is the portion of the code that I have changed:

----
temp = object1.encrypt(enter);
cin.get();
cout<<"\nYour string in decrypted form is ";
object1.decrypt(object1.encrypt(temp)); cin.get();
return 0;

-----------
now the function works fine but only for the first character and not the whole string.
You seem to have two conflicting solutions going on here.

Your class has members enc and dec to store the encrypted and decrypted strings, plus s1 to store the working string.

But encrypt() and decrypt() have s1 parameters that override the class's s1 member.

If you're going to use the class members, then decrypt() should not take a parameter; it should decrypt the already set enc member string.

Also, the reason your current code is only working for the 1st character is that when you declared string s1,enc[50],dec[50],temp[5];, you're creating fifty strings, not a string of fifty characters.

When you then say enc[i]+=s2[i]+3;, you're setting the i'th enc string to contain a single encrypted character. The s2[i] bit works as you intended because s2 is a single string, not an array of strings as enc is.

You just want a single string for enc and dec:

string s1,enc,dec,temp[5];

and change your encrypt/decrypt code which uses them accordingly.

Come back if you need more help!

Cheers,
Jim
Last edited on
ok got it now! but still i'm unable to implement this correctly.
i have corrected the multiple string issues(s1,s2 etc). i'm just using 2 strings now.
also, thanks for explaining the enc[50] to me. i thought it is a string containing 50 characters! now i know the difference. corrected this issue too.

here's the changed code for the functions:
-----------------------------------------------------------------
string encrypt(string enter) // code to encrypt the string
{ int i;
for(i=0;i<enter.length();i++) //encrypt the string user enters
{
enc+=enter[i]+3;
cout<<enc[i];
}
return enc;

}

string decrypt(string enc) //code to decrypt the string
{ int i;
for(i=0;i<enc.length();i++)
//decrypts the encrypted string by using it as an argument.this function does not work correctly :(
{
temp = enc[i]-3;
dec+= temp;
cout<<dec[i];
}
}
When you say it doesn't work, do you mean it doesn't compile?

You need to return a string from decrypt(), and if temp is still defined as string temp[5], I should think that's probably causing some issues. Also, you don't need temp as a class member - you're only using it within decrypt, so just declare it locally in there as char temp;

Jim
Jim,
Sorry for not replying earlier. I was away for the holidays.

I have good news - the program now compiles and runs correctly. The issue was some things such as returning a string from decrypt(as you mentioned) and some other function header errors.
Thanks for your help
No worries!

Jim
Topic archived. No new replies allowed.