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
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;
#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;
}
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?
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.
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,
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