Basic Encryption

I am making a basic encryption program for strings and it wont encrypt the first character. any ideas, I've been looking at it for a while and can't find it.
my code is below. thanks in advance.

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string phrase, encrypted, decrypted;
char i, key, another = 'y';
int choice, k = 0, number;
cout << "enter your phrase: \n";
getline(cin, phrase);


do {
cout << "enter a number: \n";
cin >> number;
cout << "What encryption key do you want to use? (one letter ONLY)";
cin >> key;
cout << "what would you like to do?\n 1]Encrypt\n2]Decrypted\n3] Quit\n ";
cin >> choice;
switch (choice)
{
case 1: cout << "You've choosen to Encrypt......\n";
encrypted += phrase[0] ^ (int(key)*number % number);
for (i = 0; i < phrase.length(); i++)
encrypted += phrase[i] ^ (int(key)*i % number);
cout << "your encrypted phrase is below: \n";
for (i = 0; i < encrypted.length(); i++) {
cout << encrypted[i];
k++;
}
break;
case 2:cout << "You've choosen to Decrypt......\n";
decrypted += encrypted[0] ^ (int(key)*number % number);
for (i = 0; i < encrypted.length(); i++)
decrypted += encrypted[i] ^ (int(key)*i % number);
cout << "your decrypted phrase is below: \n";
for (i = 0; i < decrypted.length(); i++) {
cout << decrypted[i];
k++;
}
case 3: another = 'y';
break;
default: cout << "Please enter 1, 2, or 3\n"; break;

}
cout << "\nanother? \n";
cin >> another;
} while (another == 'y');
system("pause");
return 0;
}
You've multiplied the key by 0 at position 0?
Last edited on
I just changed that after seeing your comment, it seemed a little unnecessary for me, now that you pointed it out. so now it is just int(key) % number for the encrypt and decrypt.also I've looked at my for loops and changed things in there trying to get it to encrypted the the first character but it hasn't been a success.
print the value of the number you xor the first byte with. A^0 == A.

also, A^K = Z then Z^K = A, so you can actually encrypt and decrypt with the same loop/code. I think you can simplify the code, but that isn't the problem.

closed account (SECMoG1T)
so i thought i could upgrade your code somehow:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>
#include <string>

enum class Mode{Encrypt,Decrypt,KeyReset};

class DataObject
{
   public:
       DataObject(const std::string& data,const std::string& encdata = {}, const bool encrypted = false);
       std::string getKey();
       std::string EncryptDecrypt(const Mode& _execMode = Mode::Encrypt);

   private:
       bool execute(const std::string& key, const Mode& _execMode = Mode::Encrypt);

       std::string _OrigData;
       std::string _EncrData;
       bool        _Encrypted;
};


int main()
{
    std::string data{},key;
    char choice;
    Mode runMode;

    std::cout<<"Enter your Data: ";
    std::getline(std::cin,data,'\n');

    DataObject myData(data);

    while(true)
    {
        std::cout<<"============================================\n";


        std::cout<<"\nwhat would you like to do?\n 1] Encrypt\n 2] Decrypted\n 3] change key\n 4] Quit\n\t  : ";
        std::cin>>choice;
        std::cout<<"\n*******************************************\n";


        if(choice == '1')
            runMode = Mode::Encrypt;

        else if(choice == '2')
            runMode = Mode::Decrypt;

        else if(choice == '3')
            runMode = Mode::KeyReset;

        else
            break;

        std::cout<<myData.EncryptDecrypt(runMode)<<"\n";;
        std::cout<<"\n============================================\n";
    }

    std::cout<<"\n\nGoodbye...";

}

DataObject::DataObject(const std::string& data,const std::string& encdata, const bool encrypted)
 :_OrigData(data), _EncrData(encdata), _Encrypted(encrypted) {}

std::string DataObject::getKey()
{
    std::string key;

    std::cout<<"\nEnter your key (arbitrary length): ";
    std::cin>>key;

    return key;
}

bool DataObject::execute(const std::string& key, const Mode& _execMode)
{
    std::string _runData{};
    int         _origDataIndex = 0, _keyIndex = 0;
    int         _dataSize = _OrigData.size(), _keySize = key.size();
    char        _processedChar{};

    if(_execMode == Mode::Decrypt && !_Encrypted)
        return false;

    while(_origDataIndex < _dataSize)
    {
        if(_execMode == Mode::Encrypt)
        {
            if(!_Encrypted)
                _processedChar = (char)(_OrigData[_origDataIndex] ^ key[_keyIndex]);

            else
                return false;
        }

        else if(_execMode == Mode::Decrypt)
            _processedChar = (char)(_EncrData[_origDataIndex] ^ key[_keyIndex]);

       _runData += _processedChar;

       ++_origDataIndex;
       ++_keyIndex;

       if(_keyIndex == _keySize)
         _keyIndex = 0;
    }


    if(_execMode == Mode::Encrypt)
    {
        _EncrData  = _runData;
        _Encrypted = true;
    }

    if(_execMode == Mode::Decrypt && _runData != _OrigData)
        return false;///decryption failed: bad key

    return true; ///operation completed successfully
}

std::string DataObject::EncryptDecrypt(const Mode& _execMode)
{
    std::string key{};

    if(_execMode != Mode::KeyReset)
    {
        key = getKey();
        bool status = execute(key,_execMode);

        if(_execMode == Mode::Encrypt)
        {
            if(status == false) ///failed
                return "\n<Already Encrypted>";

            else
                return "\n<successfully Encrypted>";
        }

        else if(_execMode == Mode::Decrypt)
        {
            if(status == false && !_Encrypted)
                return "\n<Can't Decrypt unencrypted data>";

            else if(status == false)
                return "\n<Decryption failed>: bad key <"+key+">";

            else
                return "\n<successfully Decrypted>: "+_OrigData;
        }
    }

    else
    {
        if(!_Encrypted)
          return "\n<FAILED: No previous key found>";

        int attempts = 1;
        bool correct = false;



        while(attempts < 4 && !correct)
        {
          std::cout<<"\nAttempt "<<attempts++<<"] Enter your original Key: ";
          std::cin>>key;

          if(execute(key,Mode::Decrypt) == true)
            correct = true;
        }

        if(correct)
        {
            std::cout<<"\nEnter your new key: ";
            std::cin>>key;

            _EncrData  = {};
            _Encrypted = false;

            if(execute(key,Mode::Encrypt) == true)
              return "\n<Key changed successfully>";
        }

        return "\n<FAILED: you entered the wrong key for 3 times>";
    }
}
Last edited on
Topic archived. No new replies allowed.