#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;

int main()
{
system("Color 1A");
	

    char clear[200];
    char cipher[200];
    int x,i;
    
        cout << "what do you want to do with your password " << endl;
        cout << "1 Encrypt your password " << endl;
        cout << "2 Decrypt your password " << endl;
        cout<<" Enter a string:";
        cin.getline(clear,sizeof(clear));
        
        x = strlen(clear);
          // Encryption
              for(i=0; i<=x-1; i++)
                {
                if (isspace(clear[i]))
                {
                cipher[i] = clear[i];
                }
                else
                {
                cipher[i] = clear[i]+3;
                }
                }
                
          // Decryption
                for(i=0; i<=x+2; i++)
                {
                if (isspace(cipher[i]))
                {
                clear[i] = cipher[i];
                }
                else
                {
                clear[i] = cipher[i]+4;
                }
            }
            
        cout<<" Encrypted:" << cipher << endl;
        cout<<" Decrypted:" << cipher << endl;

    system("pause");
    return 0;
}

i don't know how to finish that need help
plz
i dont know how to do this in caesar cipher
help plz

Recommended Answers

All 9 Replies

For a rotating cipher you would reverse the operation to decrypt. So add 3 for encryption and subtract 3 for decryption:

#include <iostream>
#include <string>

using namespace std;

string Encrypt(string const& s)
{
    string result;

    for (int i = 0; i < s.size(); i++)
    {
        result.push_back(s[i] + 3);
    }

    return result;
}

string Decrypt(string const& s)
{
    string result;

    for (int i = 0; i < s.size(); i++)
    {
        result.push_back(s[i] - 3);
    }

    return result;
}

int main()
{
    string cipher = Encrypt("testing 1...2...3...");

    cout << cipher << endl;
    cout << Decrypt(cipher) << endl;
}

The above example is broken, by the way. If the char type is signed and adding or subtracting 3 from s would overflow, the program invokes undefined behavior. But working around that is a lesson for another day. :) Best it keep things simple at first.

thanks for that

any one ells how to do it
plz help

#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;

int main()
{
system("Color 1A");
	

    char clear[200];
    char cipher[200];
    int x,i;
    
        cout << "what do you want to do with your password " << endl;
        cout << "1 Encrypt your password " << endl;
        cout << "2 Decrypt your password " << endl;
        cout<<" Enter a string:";
        cin.getline(clear,sizeof(clear));
        
        x = strlen(clear);
          // Encryption
              for(i=0; i<=x-1; i++)
                {
                if (isspace(clear[i]))
                {
                cipher[i] = clear[i];
                }
                else
                {
                cipher[i] = clear[i]+3;
                }
                }
                
          // Decryption
                for(i=0; i<=x+2; i++)
                {
                if (isspace(cipher[i]))
                {
                clear[i] = cipher[i];
                }
                else
                {
                clear[i] = cipher[i]+4;
                }
            }
            
        cout<<" Encrypted:" << cipher << endl;
        cout<<" Decrypted:" << cipher << endl;

    system("pause");
    return 0;
}

i don't know how to finish that need help
plz
i dont know how to do this in caesar cipher
help plz

Why are you using (isspaces) in decryption??? not needed.

Why are you using (isspaces) in decryption??? not needed.

Encryption and reverse for Decryption good luck.
for(i=0;i<=x-1;i++)
{
cipher = clear+3;
}
cipher[x] = '\0';

thanks for that

any one ells how to do it
plz help

Was my post not clear enough? Please point out what's not clear to you and I'll be happy to explain further.

hy your past it's grand but i dont get that at all cause i never done that before so i dont know how to do it
the code that i put up was guessing how to do it

hy your past it's grand but i dont get that at all cause i never done that before so i dont know how to do it
the code that i put up was guessing how to do it

The takeaway lesson is that decryption is a mirror image of encryption, and your code should reflect that (pun intended):

#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;

int main()
{
    system("Color 1A");

    char clear[200] = {0};
    char cipher[200] = {0};
    int x,i;

    cout << "what do you want to do with your password " << endl;
    cout << "1 Encrypt your password " << endl;
    cout << "2 Decrypt your password " << endl;
    cout<<" Enter a string:";
    cin.getline(clear,sizeof(clear));

    x = strlen(clear);
    // Encryption
    for(i=0; i<=x-1; i++)
    {
        if (isspace(clear[i]))
        {
            cipher[i] = clear[i];
        }
        else
        {
            cipher[i] = clear[i]+3;
        }
    }

    // Decryption
    for(i=0; i<=x-1; i++)
    {
        if (isspace(cipher[i]))
        {
            clear[i] = cipher[i];
        }
        else
        {
            clear[i] = cipher[i]-3;
        }
    }

    cout<<" Encrypted:" << cipher << endl;
    cout<<" Decrypted:" << clear << endl;

    return 0;
}

thanks mate for all your help it's working now
cheers

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.